/** * 统计最大值 */ public static void max() { // 获取age字段的最大值 AggregationBuilder aggregation = AggregationBuilders.max("maxAge").field("age"); SearchResponse response = getClient().prepareSearch("my_person").addAggregation(aggregation).get(); Max max = response.getAggregations().get("maxAge"); System.out.println(max.getValue()); }
/** * 统计最小值 */ public static void min() { // 获取age字段的最小值 AggregationBuilder aggregation = AggregationBuilders.min("minAge").field("age"); SearchResponse response = getClient().prepareSearch("my_person").addAggregation(aggregation).get(); Min min = response.getAggregations().get("minAge"); System.out.println(min.getValue()); }
/** * 统计平均值 */ public static void avg() { // 获取age字段的平均值 AggregationBuilder aggregation = AggregationBuilders.avg("avgAge").field("age"); SearchResponse response = getClient().prepareSearch("my_person").addAggregation(aggregation).get(); Avg avg = response.getAggregations().get("avgAge"); System.out.println(avg.getValue()); }
/** * 统计就和 */ public static void sum() { // 获取age字段的求和 AggregationBuilder aggregation = AggregationBuilders.sum("sumAge").field("age"); SearchResponse response = getClient().prepareSearch("my_person").addAggregation(aggregation).get(); Sum sum = response.getAggregations().get("sumAge"); System.out.println(sum.getValue()); }
/** * 基本统计 */ public static void stats() { // 基本统计 AggregationBuilder aggregation = AggregationBuilders.stats("aggStats").field("age"); SearchResponse response = getClient().prepareSearch("my_person").addAggregation(aggregation).get(); Stats stats = response.getAggregations().get("aggStats"); System.out.println(stats.toString()); }
返回结果ui
{"aggStats":{"count":6,"min":20.0,"max":28.0,"avg":22.166666666666668,"sum":133.0}}
/** * 高级统计 */ public static void extendedStats() { // 高级统计 AggregationBuilder aggregation = AggregationBuilders.extendedStats("exStats").field("age"); SearchResponse response = getClient().prepareSearch("my_person").addAggregation(aggregation).execute().actionGet(); ExtendedStats exStats = response.getAggregations().get("exStats"); System.out.println(exStats.toString()); }
执行结果spa
{"exStats":{"count":6,"min":20.0,"max":28.0,"avg":22.166666666666668,"sum":133.0,"sum_of_squares":2997.0,"variance":8.138888888888914,"std_deviation":2.8528737947706193,"std_deviation_bounds":{"upper":27.872414256207907,"lower":16.46091907712543}}}
/** * 基数统计 */ public static void cardinality() { // 基数统计 AggregationBuilder aggregation = AggregationBuilders.cardinality("cardinality").field("age"); SearchResponse response = getClient().prepareSearch("my_person").addAggregation(aggregation).get(); Cardinality cardinality = response.getAggregations().get("cardinality"); System.out.println(cardinality.getValue()); }
/** * 文档数量统计 */ public static void valueCount() { // 文档数量统计 AggregationBuilder aggregation = AggregationBuilders.count("valueCount").field("age"); SearchResponse response = getClient().prepareSearch("my_person").addAggregation(aggregation).execute().actionGet(); ValueCount valueCount = response.getAggregations().get("valueCount"); System.out.println(valueCount.getValue()); }
/** * 百分位统计 */ public static void percentiles() { // 百分位统计 AggregationBuilder aggregation = AggregationBuilders.percentiles("percentiles").field("age"); SearchResponse response = getClient().prepareSearch("my_person").addAggregation(aggregation).execute() .actionGet(); Percentiles percentiles = response.getAggregations().get("percentiles"); for (Percentile percentile : percentiles) { System.out.println("percent="+percentile.getPercent() + ",value=" + percentile.getValue()); } }
执行结果code
percent=1.0,value=20.0 percent=5.0,value=20.0 percent=25.0,value=20.0 percent=50.0,value=21.0 percent=75.0,value=22.75 percent=95.0,value=26.75 percent=99.0,value=27.75