You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.17 21:33:10