Elastic Search检索所有记录



我使用弹性搜索作为具有数百万条记录的数据库。我使用下面的代码来检索数据,但它没有给我完整的数据。

response = requests.get(http://localhost:9200/cityindex/_search?q=:&size=10000)

这只给了我10000条记录。

当我将大小扩展到doc count的大小(即784234)时,它会抛出错误。

'结果窗口太大,from + size必须小于或等于从[10000]到[100000]。查看scroll API以获得更有效的请求大型数据集的方法。可以设置此限制(索引。Max_result_window]索引级别设置。'}]

上下文我想做什么。我想提取特定索引的所有数据,然后对其进行分析(我希望以JSON格式获得整个数据)。我使用python为我的项目。有人能帮我一下吗?

您需要滚动ES返回给您的页面并将它们存储到列表/数组中。您可以使用弹性搜索库进行相同的搜索示例python代码

from elasticsearch import Elasticsearch
es = Elasticsearch(hosts="localhost", port=9200, timeout=30)
page = es.search(
index = 'index_name',
scroll = '5m',
search_type = 'scan',
size = 5000)
sid = page['_scroll_id']
scroll_size = page['hits']['total']
print scroll_size
records = []
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'])
for rec in page['hits']['hits']:
ele = rec['_source']
records.append(ele)

最新更新