gpt4 book ai didi

elasticsearch - 当结果少于 scrollSize 设置时,Scroll SearchResponse 不可迭代

转载 作者:行者123 更新时间:2023-11-29 02:53:21 25 4
gpt4 key购买 nike

我有一个这样的循环...

while (true) {
scrollResp = client.prepareSearchScroll(scrollResp.getScrollId()).setScroll(new TimeValue(600000)).execute().actionGet();

for (SearchHit hit : scrollResp.getHits()){
// does this when totalHits > scrollSize, skips it otherwise
}

//Break condition: No hits are returned
if (scrollResp.hits().hits().length == 0) {
break;
}
}

查询的 scrollSize 设置为 500。当 scrollResp.getHits().totalHits() < 500 时,永远不会进入 for 循环。如果我将 scrollSize 修改为 < totalHits,将进入 for 循环。

一般来说,我期望每个查询有 > 500 个结果,但我需要它在任何一种情况下都能正常工作。不确定我是否可能错误地尝试了迭代或者什么。感谢反馈。

最佳答案

我相信你可以做这样的事情:

    // tested also with pageSize = 1
final int pageSize = 100;
SearchResponse firstScrollResponse = client
.prepareSearch("db")
.setTypes("collection")
.setSearchType(SearchType.SCAN)
.setScroll(TimeValue.timeValueMinutes(1))
.setSize(pageSize)
.execute()
.actionGet();

//get the first page of actual results
firstScrollResponse = client.prepareSearchScroll(firstScrollResponse.getScrollId())
.setScroll(TimeValue.timeValueMinutes(1))
.execute()
.actionGet();

while (firstScrollResponse.getHits().hits().length > 0) {

for (final SearchHit hit : firstScrollResponse.getHits().hits()) {
// do smth with it
}

// update the scroll response to get more entries from the old index
firstScrollResponse = client.prepareSearchScroll(firstScrollResponse.getScrollId())
.setScroll(TimeValue.timeValueMinutes(1))
.execute()
.actionGet();
}

诀窍是首先从滚动中获取实际结果的第一页,因为第一个搜索响应不包含任何匹配项。

关于elasticsearch - 当结果少于 scrollSize 设置时,Scroll SearchResponse 不可迭代,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18239537/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com