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

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

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


当前回答

官方文档提供了这个问题的答案!你可以在这里找到它。

{
  "query": { "match_all": {} },
  "size": 1
}

您只需将size(1)替换为您想要看到的结果的数量!

其他回答

默认情况下Elasticsearch返回10条记录,因此应该显式提供大小。

添加大小与请求,以获得所需的记录数量。

http://{host}:9200/{index_name}/_search?pretty=true&size=(number的记录)

注意: 最大页面大小不能超过索引。Max_result_window索引设置,默认值为10,000。

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

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

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

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

使用python包elasticsearch-dsl的简单解决方案:

from elasticsearch_dsl import Search
from elasticsearch_dsl import connections

connections.create_connection(hosts=['localhost'])

s = Search(index="foo")
response = s.scan()

count = 0
for hit in response:
    # print(hit.to_dict())  # be careful, it will printout every hit in your index
    count += 1

print(count)

参见https://elasticsearch-dsl.readthedocs.io/en/latest/api.html#elasticsearch_dsl.Search.scan。

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

http://localhost:9200/foo/_search

来自Kibana DevTools的:

GET my_index_name/_search
{
  "query": {
    "match_all": {}
  }
}