我有这样的循环...
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个结果,但我需要它在任何一种情况下工作。不确定我是否可能错误地尝试迭代或什么。感谢您的反馈。
答案 0 :(得分:1)
我相信你可以这样做:
// 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();
}
诀窍是首先从滚动中获取实际结果的第一页,因为第一个搜索响应不包含命中。