page = es.search(
index = 'yourIndex',
doc_type = 'yourType',
scroll = '2m',
search_type = 'query_then_fetch',
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
您应该尝试使用ScrollAPI,它可以帮助您检索大量的结果(甚至是所有的结果,就像您的案例一样)。你知道吗
此功能类似于传统数据库中的游标。你知道吗
您只需在Python客户机中将scroll param添加到您的请求中。最简单可行的例子如下所示:
示例取自此处-https://gist.github.com/drorata/146ce50807d16fd4a6aa
Python客户端文档引用-https://elasticsearch-py.readthedocs.io/en/master/api.html
相关问题 更多 >
编程相关推荐