Metrics Aggregations [指标聚合]
avg aggregation
cardinality aggregation
max aggregation
min aggregation
sum aggregation
value count aggregation
extended stats aggregation
Bucket Aggregations [桶聚合]
Missing Aggregation
统计某个不存在字段notExistsField的数量
GET index/type/_search
{
"query": {
"match_all": {}
},
"aggs": {
"missing_example" : {
"missing": {
"field": "notExistsField"
}
}
}
}
Range Aggregation
范围聚合,统计每个范围内的文档数量
GET index/type/_search
{
"query": {
"match_all": {}
},
"aggs": {
"range_example": {
"range": {
"field": "amount",
"keyed": true,
"ranges": [
{
"to": 10
},
{
"from": 11,
"to": 20
},
{
"from": 21,
"to": 30
}
]
}
}
}
}
Terms Aggregation
类似于MySQL的 group by,按sex统计文档数量
GET index/type/_search
{
"query": {
"match_all": {}
},
"aggs": {
"terms_example": {
"terms": {
"field": "sex",
"size": 10
}
}
}
}
Significant Terms Aggregation
与Terms的区别: 返回结果中多了doc_count、bg_count
GET index/type/_search
{
"size": 0,
"query": {
"match_all": {}
},
"aggs": {
"terms_example": {
"significant_terms": {
"field": "state"
}
}
}
}
// 返回结果
{
"took": 14,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 264729,
"max_score": 0,
"hits": []
},
"aggregations": {
"terms_example": {
"doc_count": 264729, //文档数量
"bg_count": 266199, //未知
"buckets": [
{
"key": "1",
"doc_count": 103122, //key为1的文档数量
"score": 0.0021440541483472653,
"bg_count": 103127 //未知
}
]
}
}
}