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

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

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


当前回答

如果仍然有人像我一样寻找从Elasticsearch中检索的所有数据,下面是我所做的。此外,所有的数据意味着,所有的索引和所有的文档类型。我使用的是Elasticsearch 6.3

curl -X GET "localhost:9200/_search?pretty=true" -H 'Content-Type: application/json' -d'
{
    "query": {
        "match_all": {}
    }
}
'

Elasticsearch参考

其他回答

如果它是一个小数据集(例如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…

注意:答案与Elasticsearch 0.90的旧版本有关。此后发布的版本有了更新的语法。请参考其他可能为您正在寻找的最新答案提供更准确答案的答案。

下面的查询将返回您希望返回的NO_OF_RESULTS ..

curl -XGET 'localhost:9200/foo/_search?size=NO_OF_RESULTS' -d '
{
"query" : {
    "match_all" : {}
  }
}'

现在,这里的问题是您希望返回所有记录。因此,在编写查询之前,您自然不会知道NO_OF_RESULTS的值。

我们如何知道文档中有多少条记录?只需键入下面的查询

curl -XGET 'localhost:9200/foo/_search' -d '

这会给你一个如下图所示的结果

 {
hits" : {
  "total" :       2357,
  "hits" : [
    {
      ..................

结果总数告诉您文档中有多少条记录可用。这是知道NO_OF RESULTS值的好方法

curl -XGET 'localhost:9200/_search' -d ' 

搜索所有索引中的所有类型

curl -XGET 'localhost:9200/foo/_search' -d '

搜索foo索引中的所有类型

curl -XGET 'localhost:9200/foo1,foo2/_search' -d '

搜索foo1和foo2索引中的所有类型

curl -XGET 'localhost:9200/f*/_search

搜索以f开头的索引中的所有类型

curl -XGET 'localhost:9200/_all/type1,type2/_search' -d '

在所有索引中搜索user和tweet类型

如果仍然有人像我一样寻找从Elasticsearch中检索的所有数据,下面是我所做的。此外,所有的数据意味着,所有的索引和所有的文档类型。我使用的是Elasticsearch 6.3

curl -X GET "localhost:9200/_search?pretty=true" -H 'Content-Type: application/json' -d'
{
    "query": {
        "match_all": {}
    }
}
'

Elasticsearch参考

使用Elasticsearch 7.5.1

http://${HOST}:9200/${INDEX}/_search?pretty=true&q=*:*&scroll=10m&size=5000

如果你也可以用&size=${number}来指定数组的大小

以防你不知道你的索引

http://${HOST}:9200/_cat/indices?v

这是我使用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