Elasticsearch 7.x 去重查询并返回去重后的总数

mysql去重操作

select distinct age from user;

如果在es中如何去重呢

需要用到Elasticsearch 中的 collapse 可以实现该需求

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: 持续输出,坚持!

posted @ 2021-02-25 10:57  陈灬大灬海  阅读(2480)  评论(0编辑  收藏  举报