Jest scroll 조회

7749 단어 Elasticsearch
@Test
public void testSerialScroll() throws IOException {
    BoolQueryBuilder builder = QueryBuilders.boolQuery();

    SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder().size(SCROLL_SEARCH_SIZE);
    searchSourceBuilder.query(builder);

    Search search = new Search
        .Builder(searchSourceBuilder.toString())
        .addIndex(indexName)
        .addType(typeName)
        .addSourceIncludePattern("open_id")
        .setParameter(Parameters.SCROLL, "10m")
        .build();

    int sum = 0, loop = 0;
    JestResult result = jestClient.execute(search);
    while(result.isSucceeded()
            && result.getSourceAsObjectList(Map.class).size() > 0) {
        List<Map> record = result.getSourceAsObjectList(Map.class);
        sum += record.size();
        System.out.println("currentLoopCount:" + (++loop) + ", queryResult:" + record.size() + ", total:" + sum);
        String scrollId = result.getJsonObject().get("_scroll_id").getAsString();
        SearchScroll scroll = new SearchScroll.Builder(scrollId, "10m").build();
        result = jestClient.execute(scroll);
    }
}

좋은 웹페이지 즐겨찾기