ES 2.4 java API aggregation API

本文介绍了Elasticsearch中三种主要的聚合类型:Bucketing(桶)、Metric(度量)及Pipeline(管道)。Bucketing用于创建桶,每个桶关联一个键和文档标准;Metric则针对一组文档进行度量值的跟踪和计算;Pipeline则是对其他聚合及其相关度量值进行聚合。此外还提供了具体示例,如terms、date_histogram和avg聚合。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

链接地址:https://www.elastic.co/guide/en/elasticsearch/client/java-api/2.4/_bucket_aggregations.html

聚合主要有三大类: 

Bucketing
A family of aggregations that build buckets, where each bucket is associated with a  key and a document criterion. When the aggregation is executed, all the buckets criteria are evaluated on every document in the context and when a criterion matches, the document is considered to "fall in" the relevant bucket. By the end of the aggregation process, we’ll end up with a list of buckets - each one with a set of documents that "belong" to it.
比如:属性的值得范围,分类之类的
Metric
Aggregations that keep track and compute metrics over a set of documents.
数学运算相关,比如:求取平均值
Pipeline
Aggregations that aggregate the output of other aggregations and their associated metrics
关联其他聚合
SearchResponse sr = node.client().prepareSearch()
    .addAggregation(
        AggregationBuilders.terms("by_country").field("country")
        .subAggregation(AggregationBuilders.dateHistogram("by_year")
            .field("dateOfBirth")
            .interval((DateHistogramInterval.YEAR)
            .subAggregation(AggregationBuilders.avg("avg_children").field("children"))
        )
    )
    .execute().actionGet();
  • Terms aggregation (bucket)
    Date Histogram aggregation (bucket)
    Average aggregation (metric)
     


 metric Aggregation几具体操作:

max、min、sun、avg

MetricsAggregationBuilder aggregation =
        AggregationBuilders
                .avg("agg")
                .field("height");

取结果

Avg agg = sr.getAggregations().get("agg");
double value = agg.getValue();

stats包括上述的外加一个count

而extende stats 不仅包括stats还包括平法差一类的,更全面

还有百分之几,范围什么的,可以根据自己的需求再看看这一方面的介绍,metric Aggregation

Bucket aggregations

global 全局的,filter 、filters、missing 没有值或者是null的统计、terms 分类、range、date range、histogram 直方图、date histogram 等等,具体的运用看Bucket aggregation的介绍。

     

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值