ES中对应的SQL的count(distinct 列名) java实现
一、需求:select count(distinct city_name) from tableA;
二、代码:用的是cardinality基数统计,高版本API
AggregationBuilders.cardinality(field).field(field); // 第一个field是聚合的别名,别名就是下面根据这个别名获取聚合结果 第二个是ES里的字段
1 /** 2 * 查询去重后总量 3 * @param req 4 * @param field es里字段名称 5 * @return 6 */ 7 private Integer countDistinct(String field){ 8 // 创建查询请求对象 9 SearchRequest searchRequest = new SearchRequest(INDEX_NAME); 10 // 创建查询资源对象 11 SearchSourceBuilder sourceBuilder = new SearchSourceBuilder(); 12 15 // 查询条件-分组 16 CardinalityAggregationBuilder cardinalityAggregationBuilder = AggregationBuilders.cardinality(field).field(field); 17 sourceBuilder.aggregation(cardinalityAggregationBuilder); 18 sourceBuilder.size(0); 19 searchRequest.source(sourceBuilder); 20 21 SearchResponse searchResponse = null; 22 int size = 0; 23 try { 24 searchResponse = client.search(searchRequest, RequestOptions.DEFAULT); 25 if(null == searchResponse){ return 0; } 26 ParsedCardinality parsedCardinality = searchResponse.getAggregations().get(field); // 这个field就是上面起的别名 27 size = (int) parsedCardinality.getValue(); 28 } catch (IOException e) { 29 log.debug("ES查询分组条数有误!"); 30 } 31 return size; 32 }