一、概述
度量类型聚合主要针对的number类型的数据,需要ES做比较多的计算工作
参考向导:地址
import org.elasticsearch.search.aggregations.AggregationBuilders;
SearchResponse sr = node.client().prepareSearch() .setQuery( /* your query */ ) .addAggregation( /* add an aggregation */ ) .execute().actionGet();
1.1、构建聚合
可以在聚合内定义子聚合。 聚合可以是度量聚合或桶聚合。
例如,这是一个由以下各项组成的3级聚合: term聚合(桶) 日期直方图聚合(桶) 平均聚合(度量)
SearchResponse sr = node.client().prepareSearch() .addAggregation( AggregationBuilders.terms("by_country").field("country") .subAggregation(AggregationBuilders.dateHistogram("by_year") .field("dateOfBirth") .dateHistogramInterval(DateHistogramInterval.YEAR) .subAggregation(AggregationBuilders.avg("avg_children").field("children")) ) ) .execute().actionGet();
1.2、度量聚合【metrics】
更多API
1)基本聚合:min max sum avg操作,一般作用于number类型字段上
// 这里可以修改成 min max sum avg MinAggregationBuilder aggregation = AggregationBuilders .min("号码") .field("num"); SearchResponse sr = client.prepareSearch() .addAggregation(aggregation ) .execute().actionGet(); Min aaa = sr.getAggregations().get("号码"); double value = aaa.getValue(); System.out.println("号码:"+value);
2)综合聚合
StatsAggregationBuilder aggregation = AggregationBuilders .stats("agg") .field("height");
使用
// sr is here your SearchResponse object Stats agg = sr.getAggregations().get("agg"); double min = agg.getMin(); double max = agg.getMax(); double avg = agg.getAvg(); double sum = agg.getSum(); long count = agg.getCount();
3)扩展综合聚合
ExtendedStatsAggregationBuilder aggregation = AggregationBuilders .extendedStats("agg") .field("height");
使用
// sr is here your SearchResponse object ExtendedStats agg = sr.getAggregations().get("agg"); double min = agg.getMin(); double max = agg.getMax(); double avg = agg.getAvg(); double sum = agg.getSum(); long count = agg.getCount(); double stdDeviation = agg.getStdDeviation(); double sumOfSquares = agg.getSumOfSquares(); double variance = agg.getVariance();
4)count聚合
ValueCountAggregationBuilder aggregation = AggregationBuilders .count("agg") .field("height");
使用
// sr is here your SearchResponse object ValueCount agg = sr.getAggregations().get("agg"); long value = agg.getValue();
5)百分比聚合
PercentilesAggregationBuilder aggregation = AggregationBuilders .percentiles("agg") .field("height");
默认会提供百分比区间。也可以自定义
PercentilesAggregationBuilder aggregation = AggregationBuilders .percentiles("agg") .field("height") .percentiles(1.0, 5.0, 10.0, 20.0, 30.0, 75.0, 95.0, 99.0);
使用
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); }
结果展示
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]
6)百分比行列
示例
PercentileRanksAggregationBuilder aggregation = AggregationBuilders .percentileRanks("agg") .field("height") .values(1.24, 1.91, 2.22);
使用
import org.elasticsearch.search.aggregations.metrics.percentiles.Percentile; import org.elasticsearch.search.aggregations.metrics.percentiles.PercentileRanks; // sr is here your SearchResponse object PercentileRanks 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); }
结果
percent [29.664353095090945], value [1.24] percent [73.9335313461868], value [1.91] percent [94.40095147327283], value [2.22]
7)基数聚合
CardinalityAggregationBuilder aggregation = AggregationBuilders .cardinality("agg") .field("tags");
使用
import org.elasticsearch.search.aggregations.metrics.cardinality.Cardinality; // sr is here your SearchResponse object Cardinality agg = sr.getAggregations().get("agg"); long value = agg.getValue();
更多操作:地址