其他分享
首页 > 其他分享> > es 滚动查询二

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);
    }

 

标签:滚动,getHits,tableName,查询,totalCount,client,query,es,scrollResp
来源: https://www.cnblogs.com/luweiweicode/p/14071983.html