等同于在Elasticsearch中恢复搜索和滚动的操作

问题描述

可以说,我在索引中有1000 documents,并且我可以一次使用fetch 20 docssearch and scroll,直到基于iteration_count的最后一个文档或更早的文档为止

较新的数据(say 500)可能会及时插入到同一索引中,但是我希望从上次停止的位置搜索和滚动文档。我碰到了search_after,但无法与我想像的滚动条一起使用。

有没有办法恢复搜索和滚动?

PS:不能是普通搜索,必须是滚动查询

#search and scroll in batches of 20
index = "demo"
batch_size = 20 
scroll_interval = "5m"

#to ignore the newer records inserted,if any,after the first search query
count = es.count(index='demo',body={})['count']
iteration_count = count//batch_size 

data = []

result = es.search(
    index=index,body={},size=batch_size,scroll=scroll_interval)

for hit in result["hits"]["hits"]:
    data.append(hit['_source'])

scroll_id = result['_scroll_id']
scroll_size = result["hits"]["total"]["value"]

i = 0
while((scroll_size > 0) & (i < iteration_count)):

    print("\n\n","Scrolling ({})...".format(i),",scroll_size,i,iteration_count)

    result = es.scroll(scroll_id=scroll_id,scroll="5m")
    scroll_id = result["_scroll_id"]
    scroll_size = len(result['hits']['hits'])
    
    for hit in result["hits"]["hits"]:
        data.append(hit['_source'],ignore_index=True)
    
    i += 1

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)