Elasticsearch-Scroll分页-Java示例

相关文章链接:

  • from size 分页-Java示例
  • Scroll 分页-Java示例
  • Search After分页-Java示例

ES Scroll分页查询

Scroll分页与from-size分页的比较可参考:分页查询From&Size VS scroll
Scroll分页特点:

  • 模拟数据游标,可用于深分页查询大量数据场景
  • 基于历史快照和上一页的ScrollId,查询下一页;
  • 不适合实时搜索和跳页需求;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.search.SearchHit;

import java.util.function.Consumer;


public class EsScrollHandler {

    private static final int DEFAULT_SCROLL_TIME = 30000;

    /**
     * Search for hit.
     *
     * @param requestBuilder the request builder
     * @param consumer       the consumer
     */
    public static void searchForHit(TransportClient esClient, SearchRequestBuilder requestBuilder,
                                    Consumer<SearchHit> consumer) {
        searchForResponse(esClient, requestBuilder, searchResponse -> {
            SearchHit[] searchHits = searchResponse.getHits().getHits();
            for (SearchHit searchHit : searchHits) {
                consumer.accept(searchHit);
            }
        });
    }

    /**
     * Search for response.
     *
     * @param esClient       the es client
     * @param requestBuilder the request builder
     * @param consumer       the consumer
     */
    public static void searchForResponse(TransportClient esClient, SearchRequestBuilder requestBuilder,
                                         Consumer<SearchResponse> consumer) {
        searchForResponse(esClient, requestBuilder, consumer, new TimeValue(DEFAULT_SCROLL_TIME));
    }

    /**
     * Search for response.
     *
     * @param esClient       the es client
     * @param requestBuilder the request builder
     * @param consumer       the consumer
     * @param keepAlive      the keep alive
     */
    public static void searchForResponse(TransportClient esClient, SearchRequestBuilder requestBuilder,
                                         Consumer<SearchResponse> consumer, TimeValue keepAlive) {
        if (esClient == null || requestBuilder == null || consumer == null) {
            return;
        }
        SearchRequestBuilder builder = requestBuilder.setScroll(keepAlive);
        SearchResponse searchResponse = builder.get();
        long length = searchResponse.getHits().getHits().length;
        String scrollId = null;
        while (length > 0) {
            consumer.accept(searchResponse);
            scrollId = searchResponse.getScrollId();
            searchResponse = esClient.prepareSearchScroll(scrollId)
                    .setScroll(keepAlive)
                    .get();
            length = searchResponse.getHits().getHits().length;
        }
        // 可手动清空快照记录,避免内存占用        
        if (scrollId != null) {
            esClient.prepareClearScroll().addScrollId(scrollId).get();
        }
    }
}

你可能感兴趣的:(ES,Java)