桶最大值聚合(max bucket aggregation)
桶最大值聚合用于标识出兄弟聚合中指定度量值最大的桶,然后将此桶的值与键输出。被指定的度量必须是数字并且兄弟聚合必须为多桶聚合。
语法
{
"max_bucket": {
"buckets_path": "the_sum"
}
}
参数名称 | 描述 | 必需 | 默认值 |
---|---|---|---|
buckets_path | 通向期望获取最大值的桶的路径 | 必需 | |
gap_policy | 当数据出现间隙时采用的策略 | 可选 | skip |
format | 应用于聚合输出值的格式 | 可选 | null |
POST /bank/_search
{
"size":0,
"aggs" : {
"per_state": {
"terms": {
"field": "state.keyword"
}
},
"max_state_count": {
"max_bucket": {
"buckets_path": "per_state>_count"
}
}
}
}
note:buckets_path用于指示桶最大值聚合获取per_state聚合中terms聚合中的doc_count的最大值。
{
"took" : 5,
"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
}
]
},
"max_state_count" : {
"value" : 27.0,
"keys" : [
"ID",
"TX"
]
}
}
}
note:当最大值存在于多个桶中时,keys为一个字符串数组。