ES22-JAVA API 指標聚合

1.最大值

/**
	 * 統計最大值
	 */
	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());

	}

2.最小值

/**
	 * 統計最小值
	 */
	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());

	}

3.平均值

/**
	 * 統計平均值
	 */
	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());

	}

4.求和統計

/**
	 * 統計就和
	 */
	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());

	}

5.基本統計

/**
	 * 基本統計
	 */
	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}}

6.高級統計

/**
	 * 高級統計
	 */
	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}}}

7.基數統計

/**
	 * 基數統計
	 */
	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());

	}

8.文檔數量統計

/**
	 * 文檔數量統計
	 */
	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());

	}

9.百分位統計

/**
	 * 百分位統計
	 */
	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
相關文章
相關標籤/搜索