基本概念
GROUP BY
但是功能更强大。
官方java-api原文
地址:https://www.elastic.co/guide/en/elasticsearch/client/java-api/1.7/java-aggs.html
There are many different types of aggregations, each with its own purpose and output. To better understand these types, it is often easier to break them into two main families:
-
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.
The interesting part comes next. Since each bucket effectively defines a document set (all documents belonging to the bucket), one can potentially associate aggregations on the bucket level, and those will execute within the context of that bucket. This is where the real power of aggregations kicks in:
对应译文
Metric Aggregation(聚合操作)
Percentile Aggregation
Here is an example on how to create the aggregation request:
MetricsAggregationBuilder aggregation =
AggregationBuilders
.percentiles("agg")
.field("height");
You can provide your own percentiles instead of using defaults:
MetricsAggregationBuilder aggregation =
AggregationBuilders
.percentiles("agg")
.field("height")
.percentiles(1.0, 5.0, 10.0, 20.0, 30.0, 75.0, 95.0, 99.0);
Use aggregation responseedit
Import Aggregation definition classes:
import org.elasticsearch.search.aggregations.metrics.percentiles.Percentile;
import org.elasticsearch.search.aggregations.metrics.percentiles.Percentiles;
// sr is here your SearchResponse object
Percentiles agg = sr.getAggregations().get("agg");
// For each entry
for (Percentile entry : agg) {
double percent = entry.getPercent(); // Percent
double value = entry.getValue(); // Value
logger.info("percent [{}], value [{}]", percent, value);
}
This will basically produce for the first example:
percent [1.0], value [0.814338896154595] percent [5.0], value [0.8761912455821302] percent [25.0], value [1.173346540141847] percent [50.0], value [1.5432023318692198] percent [75.0], value [1.923915462033674] percent [95.0], value [2.2273644908535335] percent [99.0], value [2.284989339108279]
digest具体可参考文章:https://github.com/tdunning/t-digest/blob/master/docs/t-digest-paper/histo.pdf
"values" : {
"1.0": 15,
"5.0": 20,
"25.0": 23,
"50.0": 25,
"75.0": 29,
"95.0": 60,
"99.0": 150
}
本文深入探讨了Elasticsearch的聚合功能,特别是Metric Aggregation中的Percentiles Aggregation。通过官方Java API原文和译文解释了如何创建聚合请求,并提供了自定义百分位数的方法。此外,还提到了参考资料,帮助理解digest概念。
3558

被折叠的 条评论
为什么被折叠?



