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

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

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


当前回答

使用kibana控制台和my_index作为搜索索引,可以提供以下内容。要求索引只返回索引的4个字段,您还可以添加大小来指示您希望由索引返回多少文档。从ES 7.6开始,你应该使用_source而不是filter,它会响应更快。

GET /address/_search
 {
   "_source": ["streetaddress","city","state","postcode"],
   "size": 100,
   "query":{
   "match_all":{ }
    }   
 }

其他回答

你实际上不需要传递一个body给match_all,它可以通过一个GET请求到下面的URL来完成。这是最简单的形式。

http://localhost:9200/foo/_search

curl -XGET '{{IP/localhost}}:9200/{{Index name}}/{{type}}/_search?scroll=10m&pretty' -d '{
"query": {
"filtered": {
"query": {
"match_all": {}
}}'

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

我认为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提供)

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