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

 

上一篇:【高并发】Redis如何助力高并发秒杀系统,看完这篇我彻底懂了!!


下一篇:从Java线程到kotlin协程之线程的同步 synchronized关键字