天天看点

Elasticsearch 桶统计数据聚合

桶总数聚合(stats bucket aggregation)

桶总数聚合用于计算兄弟聚合中所有桶指定度量值的各类统计数据。被指定的度量必须是数字并且兄弟聚合必须为多桶聚合。

语法

{
	"stats_bucket": {
		"buckets_path": "the_sum"
	}
}
           
参数名称 描述 必需 默认值
buckets_path 通向期望计算统计数据的桶的路径 必需
gap_policy 当数据出现间隙时采用的策略 可选 skip
format 应用于聚合输出值的格式 可选 null
POST /bank/_search
{
  "size": 0,
  "aggs": {
    "per_state":{
      "terms": { "field": "state.keyword"}
    },
    "per_state_stats": {
      "stats_bucket": {
        "buckets_path": "per_state>_count"
      }
    }
  }
}
           

note:buckets_path用于指示桶统计数据聚合计算per_state聚合中terms聚合中的doc_count的统计数据。

{
  "took" : 2,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 1000,
    "max_score" : 0.0,
    "hits" : [ ]
  },
  "aggregations" : {
    "per_state" : {
      "doc_count_error_upper_bound" : 20,
      "sum_other_doc_count" : 770,
      "buckets" : [
        {
          "key" : "ID",
          "doc_count" : 27
        },
        {
          "key" : "TX",
          "doc_count" : 27
        },
        {
          "key" : "AL",
          "doc_count" : 25
        },
        {
          "key" : "MD",
          "doc_count" : 25
        },
        {
          "key" : "TN",
          "doc_count" : 23
        },
        {
          "key" : "MA",
          "doc_count" : 21
        },
        {
          "key" : "NC",
          "doc_count" : 21
        },
        {
          "key" : "ND",
          "doc_count" : 21
        },
        {
          "key" : "ME",
          "doc_count" : 20
        },
        {
          "key" : "MO",
          "doc_count" : 20
        }
      ]
    },
    "per_state_stats" : {
      "count" : 10,
      "min" : 20.0,
      "max" : 27.0,
      "avg" : 23.0,
      "sum" : 230.0
    }
  }
}
           

翻译源:Elasticsearch 6.5 文档

继续阅读