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 に答える