zoukankan      html  css  js  c++  java
  • 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());
    
  • 相关阅读:
    [bzoj1263]整数划分
    [bzoj3171]循环格
    [bzoj3551]Peaks加强版
    [bzoj3307]雨天的尾巴
    [bzoj2756]奇怪的游戏
    [bzoj4025]二分图
    人比人该死啊
    数据挖掘十大经典算法[0]-K-Means算法
    ISODATA算法
    CodeForces Round 197 Div2
  • 原文地址:https://www.cnblogs.com/pinked/p/13936893.html
Copyright © 2011-2022 走看看