Python Elasticsearch 无限制查询:返回所有数据
在Elasticsearch中,查询返回的数据量是有限制的,一般默认为10条,可以通过设置size参数来控制返回的数据量。如果想要返回所有数据,可以通过设置size参数为0或者使用scroll API来实现。
使用size参数为0:
from elasticsearch import Elasticsearch
es = Elasticsearch()
query = {
'query': {
'match_all': {}
},
'size': 0
}
result = es.search(index='your_index', body=query)
使用scroll API:
from elasticsearch import Elasticsearch
es = Elasticsearch()
query = {
'query': {
'match_all': {}
}
}
scroll_size = 1000
result = es.search(index='your_index', body=query, scroll='1m', size=scroll_size)
total_hits = result['hits']['total']['value']
scroll_id = result['_scroll_id']
hits = result['hits']['hits']
while len(hits) < total_hits:
result = es.scroll(scroll_id=scroll_id, scroll='1m')
scroll_id = result['_scroll_id']
hits += result['hits']['hits']
原文地址: https://www.cveoy.top/t/topic/f2mo 著作权归作者所有。请勿转载和采集!