我在Elasticsearch中有一个小数据库,出于测试目的,我想把所有记录拉回来。我正在尝试使用表单的URL…

http://localhost:9200/foo/_search?pretty=true&q={'matchAll':{''}}

有人能给我你要用来完成这个的URL吗?


当前回答

我认为lucene语法是受支持的,所以:

http://localhost: 9200 / foo / _search ?漂亮=真的,q = *: *

size默认为10,因此您可能还需要&size=BIGNUMBER来获取超过10个项目。(其中BIGNUMBER等于一个你认为比你的数据集大的数字)

但是,elasticsearch文档建议对于较大的结果集,使用扫描搜索类型。

EG:

curl -XGET 'localhost:9200/foo/_search?search_type=scan&scroll=10m&size=50' -d '
{
    "query" : {
        "match_all" : {}
    }
}'

然后按照上面建议的文档链接继续请求。

EDIT: scan 2.1.0中已弃用。

与按_doc排序的常规滚动请求相比,Scan并没有提供任何好处。弹性文档链接(由@christophe-roussy提供)

其他回答

这是我使用python客户端发现的最佳解决方案

  # Initialize the scroll
  page = es.search(
  index = 'yourIndex',
  doc_type = 'yourType',
  scroll = '2m',
  search_type = 'scan',
  size = 1000,
  body = {
    # Your query's body
    })
  sid = page['_scroll_id']
  scroll_size = page['hits']['total']

  # Start scrolling
  while (scroll_size > 0):
    print "Scrolling..."
    page = es.scroll(scroll_id = sid, scroll = '2m')
    # Update the scroll ID
    sid = page['_scroll_id']
    # Get the number of results that we returned in the last scroll
    scroll_size = len(page['hits']['hits'])
    print "scroll size: " + str(scroll_size)
    # Do something with the obtained page

https://gist.github.com/drorata/146ce50807d16fd4a6aa

使用java客户端

import static org.elasticsearch.index.query.QueryBuilders.*;

QueryBuilder qb = termQuery("multi", "test");

SearchResponse scrollResp = client.prepareSearch(test)
        .addSort(FieldSortBuilder.DOC_FIELD_NAME, SortOrder.ASC)
        .setScroll(new TimeValue(60000))
        .setQuery(qb)
        .setSize(100).execute().actionGet(); //100 hits per shard will be returned for each scroll
//Scroll until no hits are returned
do {
    for (SearchHit hit : scrollResp.getHits().getHits()) {
        //Handle the hit...
    }

    scrollResp = client.prepareSearchScroll(scrollResp.getScrollId()).setScroll(new TimeValue(60000)).execute().actionGet();
} while(scrollResp.getHits().getHits().length != 0); // Zero hits mark the end of the scroll and the while loop.

https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/java-search-scrolling.html

elasticsearch(ES)既支持GET请求,也支持POST请求,以便从ES集群索引中获取数据。

当我们执行GET操作时:

http://localhost:9200/[your index name]/_search?size=[no of records you want]&q=*:*

当我们做POST时:

http://localhost:9200/[your_index_name]/_search
{
  "size": [your value] //default 10
  "from": [your start index] //default 0
  "query":
   {
    "match_all": {}
   }
}   

我建议使用elasticsearch http://mobz.github.io/elasticsearch-head/的UI插件 这将帮助您更好地了解您创建的索引,并测试您的索引。

curl -X GET 'localhost:9200/foo/_search?q=*&pretty' 

你可以使用_count API来获取size参数的值:

http://localhost:9200/foo/_count?q=<your query>

返回{count:X,…}。提取值'X',然后执行实际查询:

http://localhost:9200/foo/_search?q=<your query>&size=X

如果它是一个小数据集(例如1K记录),你可以简单地指定大小:

curl localhost:9200/foo_index/_search?size=1000

不需要match all查询,因为它是隐式的。

如果你有一个中等规模的数据集,比如1M的记录,你可能没有足够的内存来加载它,所以你需要滚动。

滚动就像数据库中的游标。在Elasticsearch中,它会记住你离开的地方,并保持相同的索引视图(即防止搜索器随着刷新而离开,防止段合并)。

api方面,你必须添加一个滚动参数到第一个请求:

curl 'localhost:9200/foo_index/_search?size=100&scroll=1m&pretty'

你会得到第一页和一个滚动ID:

{
  "_scroll_id" : "DXF1ZXJ5QW5kRmV0Y2gBAAAAAAAAADEWbmJlSmxjb2hSU0tMZk12aEx2c0EzUQ==",
  "took" : 0,
...

请记住,您获得的滚动ID和超时都对下一页有效。这里一个常见的错误是指定了一个非常大的超时(scroll的值),这将涵盖处理整个数据集(例如1M条记录)而不是一个页面(例如100条记录)。

要获取下一页,请填写最后一个滚动ID和一个超时,该超时应该持续到获取以下页面:

curl -XPOST -H 'Content-Type: application/json' 'localhost:9200/_search/scroll' -d '{
  "scroll": "1m",
  "scroll_id": "DXF1ZXJ5QW5kRmV0Y2gBAAAAAAAAADAWbmJlSmxjb2hSU0tMZk12aEx2c0EzUQ=="
}'

如果你有很多东西要导出(例如1B文档),你会想要并行化。这可以通过切片滚动来实现。假设您想在10个线程上导出。第一个线程会发出这样的请求:

curl -XPOST -H 'Content-Type: application/json' 'localhost:9200/test/_search?scroll=1m&size=100' -d '{
  "slice": {
    "id": 0, 
    "max": 10 
  }
}'

您将返回第一页和一个滚动ID,与正常的滚动请求完全相同。你会像普通的滚动一样使用它,除了你得到的数据只有十分之一。

其他线程也会做同样的事情,除了id是1,2,3…