Aggregations - Structuring aggregations

优质
小牛编辑
137浏览
2023-12-01

Structuring aggregations

结构化聚合

Aggregations guide 中所述,可以在聚合中定义子聚合。

聚合可能是 Metrics 聚合(一个跟踪和计算指标的聚合)或者 Bucket 聚合 (构建桶聚合)

例如,这里是一个3级聚合组成的聚合:

  • Terms aggregation (bucket)
  • Date Histogram aggregation (bucket)
  • Average aggregation (metric)
  1. SearchResponse sr = node.client().prepareSearch()
  2. .addAggregation(
  3. AggregationBuilders.terms("by_country").field("country")
  4. .subAggregation(AggregationBuilders.dateHistogram("by_year")
  5. .field("dateOfBirth")
  6. .dateHistogramInterval(DateHistogramInterval.YEAR)
  7. .subAggregation(AggregationBuilders.avg("avg_children").field("children"))
  8. )
  9. )
  10. .execute().actionGet();