Java实现es的scroll滚动查询

    public void selectData() throws IOException {
        int i=1,size=2;
        SearchRequest searchRequest = new SearchRequest("index");
        Scroll scroll = new Scroll(TimeValue.timeValueMinutes(5L));
        searchRequest.scroll(scroll);
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();

        MatchAllQueryBuilder matchAllQueryBuilder = QueryBuilders.matchAllQuery();
        searchSourceBuilder.query(matchAllQueryBuilder);
        searchSourceBuilder.size(size);

        searchRequest.source(searchSourceBuilder);
        SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
        String scrollId = response.getScrollId();
        SearchHit[] searchHits = response.getHits().getHits();

        System.out.println(response.getHits().getTotalHits());
        for (SearchHit searchHit : searchHits) {
            System.out.println(searchHit.getSourceAsString());
        }
        while (searchHits != null && searchHits.length > 0) {
            SearchScrollRequest scrollRequest = new SearchScrollRequest(scrollId);
            scrollRequest.scroll(scroll);
            response = client.scroll(scrollRequest, RequestOptions.DEFAULT);
            scrollId = response.getScrollId();
            searchHits = response.getHits().getHits();

            for (SearchHit searchHit : searchHits) {
                i++;
                System.out.println(searchHit.getSourceAsString());
            }
            if (i > 10) {
                break;
            }
        }
    }

scroll滚动查询,es默认是存储500条scroll_id,如果超过500条继续使用滚动查询时,会报错,下面代码是对screll_id进行删除操作。

public static boolean clearScrollIds(RestHighLevelClient client,String... scrollIds){
    List sIds = new ArrayList<>();
    for (String scrollId : scrollIds) {
        sIds.add(scrollId);
    }
    ClearScrollRequest clearScrollRequest = new ClearScrollRequest();
    //添加单个id
    clearScrollRequest.addScrollId("滚动id");
    //添加多个id
    clearScrollRequest.setScrollIds(sIds);
    try {
        client.clearScroll(clearScrollRequest,RequestOptions.DEFAULT);
        return true;
    } catch (IOException e) {
        return false;
    }

}

 

你可能感兴趣的:(es,java,elasticsearch,jar,大数据)