es 滚动查询二

  /**
     * es 游标获取总数
     *
     * @param tableName 表名
     * @param query     查询条件
     */
    public void esScrollDataHelp(String tableName, QueryBuilder query) {
        TransportClient client = ElasticSearchUtil.getClient();
        SearchResponse scrollResp = client.prepareSearch(tableName)
                .setTypes(tableName)
                .setScroll(new TimeValue(60000))
                .setQuery(query)
                .addSort(SortBuilders.fieldSort("_doc"))
                .setSize(100).get();
        long totalCount = scrollResp.getHits().getTotalHits();// 获取总数量
        System.out.println("totalCount:" + totalCount);
        do {
            for (SearchHit hit : scrollResp.getHits().getHits()) {
                String id = hit.getId();
                System.out.println(id);
            }
            scrollResp = client.prepareSearchScroll(scrollResp.getScrollId()).setScroll(new TimeValue(60000)).execute().actionGet();
        } while (scrollResp.getHits().getHits().length != 0);
    }
故乡明
原文地址:https://www.cnblogs.com/luweiweicode/p/14071983.html