es7.16.3版本
指定字段去重计数
RestHighLevelClient client = RestHighLevelClientPoolUtil.getClient();SearchRequest request = new SearchRequest(WebServerdocumentSettings.index);// 构建查询器SearchSourceBuilder builder = new SearchSourceBuilder();// 指定字段进行折叠,这个字段相同的多条数据只显示其中一条CollapseBuilder collapseBuilder = new CollapseBuilder("需要去重的字段名");CardinalityAggregationBuilder agg = AggregationBuilders.cardinality("计数自定义名字").field("指定需要计数的字段");// 复合查询器BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();boolQuery.must(QueryBuilders.matchQuery("field","value"); // 组装查询条件builder.collapse(collapseBuilder);builder.sort(WebServerdocumentSettings.Mappings.createTime, SortOrder.DESC);builder.query(boolQuery);builder.aggregation(agg);builder.from(1);builder.size(10);// 获取计数信息ParsedCardinality fileCount = response.getAggregations().get("计数自定义名字");long total = NumberUtil.parseLong(fileCount.getValueAsString());for (SearchHit hit : response.getHits().getHits()) { Map
获取指定索引总数
@NotNullprivate Long getFieldCounts(String index) throws IOException { RestHighLevelClient client = RestHighLevelClientPoolUtil.getClient(); CountRequest countRequest = new CountRequest(); countRequest.indices(index); CountResponse response = client.count(countRequest, RequestOptions.DEFAULT); RestHighLevelClientPoolUtil.returnClient(client); return response.getCount();}