Elasticsearch 7.x 去重查询并返回去重后的总数
作者:互联网
mysql去重操作
select distinct age from user;
如果在es中如何去重呢
需要用到Elasticsearch 中的 collapse 可以实现该需求
java API
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.collapse(new CollapseBuilder("name.keyword"));
但是有个问题,就是hits的total value不对,对应的还是未去重的数量,其实想要的是去重后的总数
可以借助 Aggregation 中的 cardinality 来实现
java API
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
AggregationBuilder aggregation = AggregationBuilders.cardinality(DISTINCT_TOTAL_COUNT).field("name.keyword");
searchSourceBuilder.aggregation(aggregation);
获取去重后的数量
Aggregations aggregations = searchResponse.getAggregations();
Cardinality cardinality = aggregations.get(DISTINCT_TOTAL_COUNT);
System.out.println(cardinality.getValue());
需要注意的是,字段类型得是keyword类型。DISTINCT_TOTAL_COUNT是自定义的属性
tips: 持续输出,坚持!
标签:COUNT,searchSourceBuilder,SearchSourceBuilder,重后,查询,Elasticsearch,new,cardinalit 来源: https://www.cnblogs.com/chywx/p/14388015.html