ES 2.4 java API aggregation API

  • Post author:
  • Post category:java


链接地址:

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的介绍。