4

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

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 个结果,但我需要它在任何一种情况下都能正常工作。不确定我是否可能错误地尝试迭代或什么。反馈表示赞赏。

4

1 回答 1

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();
    }

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

于 2013-10-03T21:35:43.940 回答