RestHighLevelClient.scroll()实现滚屏

在ES中,分页查询当页数非常大之后,会很耗费集群的内存资源,可能会导致内存溢出等问题;所以当我们需要查询很多也数据的时候,选择使用scroll方式,下面是一个简单的demo:

引入依赖:

    
        org.elasticsearch
        elasticsearch
        6.5.4
    
    
        org.elasticsearch.client
        elasticsearch-rest-high-level-client
        6.5.4
    
    BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
    boolQuery.should(QueryBuilders.termQuery("age","30"));
    // 存活时间,当索引数据量特别大时,出现超时可能性大,此值适当调大
    String index = "index";
    Scroll scroll = new Scroll(TimeValue.timeValueMinutes(10L));
    SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
    searchSourceBuilder.query(boolQuery);
    searchSourceBuilder.size(500);
    SearchRequest searchRequest = new SearchRequest()
            // ES7已经去掉type,查询时加type
            .indices(index)
            .scroll(scroll)
            .source(searchSourceBuilder);
    SearchResponse searchResponse = null;
    try {
        searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
    } catch (IOException e) {
            e.printStackTrace();
    }
    String scrollId = searchResponse.getScrollId();
    SearchHit[] searchHits = searchResponse.getHits().getHits();
    for (SearchHit searchHit : searchHits) {
        System.out.println(searchHit.getSourceAsString());
    }
    //遍历搜索命中的数据,直到没有数据
    while (searchHits != null && searchHits.length > 0) {
        SearchScrollRequest scrollRequest = new SearchScrollRequest(scrollId);
        scrollRequest.scroll(scroll);
        try {
            searchResponse = restHighLevelClient.scroll(scrollRequest, RequestOptions.DEFAULT);
        } catch (IOException e) {
            e.printStackTrace();
        }
        scrollId = searchResponse.getScrollId();
        searchHits = searchResponse.getHits().getHits();
        if (searchHits != null && searchHits.length > 0) {
            for (SearchHit searchHit : searchHits) {
                System.out.println(searchHit.getSourceAsString());
            }
        }
    }
    //clean scroll
    ClearScrollRequest clearScrollRequest = new ClearScrollRequest();
    clearScrollRequest.addScrollId(scrollId);
    ClearScrollResponse clearScrollResponse = null;
    try {
        clearScrollResponse = restHighLevelClient.clearScroll(clearScrollRequest,RequestOptions.DEFAULT);
    } catch (IOException e) {
        log.error("clear-scroll-error:{}",e);
    }
    boolean succeeded = clearScrollResponse.isSucceeded();
    System.out.println(succeeded);

 

你可能感兴趣的:(java)