SpringBoot3.2.10+Elasticsearch8.12.1中如何实现CompositeAggregationSource
SpringBoot 3.2.10 + Elasticsearch 8.12.1 替换CompositeAggregationSource的terms方法
在Elasticsearch Java Client 8.12.x版本中,CompositeAggregationSource.Builder的terms()方法已被移除,可通过以下两种方式实现原有按字段聚合的功能:
方式一:简洁写法(推荐)
直接使用CompositeAggregationSource.terms()静态工厂方法完成构建:
CompositeAggregationSource compositeAggregationSource = CompositeAggregationSource.terms( t -> t.field(Constants.name) );
方式二:灵活配置写法
如果需要添加排序、缺失值处理等额外配置,可通过source()方法传入TermsCompositeAggregation实例:
CompositeAggregationSource compositeAggregationSource = CompositeAggregationSource.of( ca -> ca.source( TermsCompositeAggregation.of(t -> t.field(Constants.name)) ) );
带额外配置的示例
比如设置聚合结果排序方向、缺失值默认值:
CompositeAggregationSource compositeAggregationSource = CompositeAggregationSource.terms( t -> t.field(Constants.name) .order(SortOrder.Asc) .missing("unknown") );
内容的提问来源于stack exchange,提问作者Amulya M
相关产品推荐
相关产品推荐

