pinked

导航

ElasticSearch中通过Scroll查询所有数据

ElasticSearch中通过Scroll查询所有数据

SearchRequest request = new SearchRequest(index);
//设置超时时间
request.scroll(TimeValue.timeValueMinutes(1L));
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
//单次查询的量
sourceBuilder.size(1000);
request.source(sourceBuilder);
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
String scrollId = response.getScrollId();
for (SearchHit hit : response.getHits().getHits()) {
    System.out.println(hit.getSourceAsString());
}

while (true) {
    SearchScrollRequest scrollRequest = new SearchScrollRequest(scrollId);
    scrollRequest.scroll(TimeValue.timeValueMinutes(1L));
    SearchResponse scrollResponse = client.scroll(scrollRequest, RequestOptions.DEFAULT);
    SearchHit[] hits = scrollResponse.getHits().getHits();
    if (hits != null && hits.length > 0) {
        for (SearchHit hit : hits) {
            System.out.println(hit.getSourceAsString());
        }
    }else {
        break;
    }
}

ClearScrollRequest scrollRequest = new ClearScrollRequest();
scrollRequest.addScrollId(scrollId);
ClearScrollResponse scrollResponse = client.clearScroll(scrollRequest, RequestOptions.DEFAULT);
System.out.println(scrollResponse.status());

posted on 2020-11-06 14:55  pinked  阅读(743)  评论(0编辑  收藏  举报