1. Terms Aggregation 根据字段值项分组聚合
GET /megacorp/employee/_search?size=0 HTTP/1.1
Host: 192.168.31.233:9200
Content-Type: application/json
{
"aggs": {
"age_terms": {
"terms": {
"field": "age"
}
}
}
}
Results:
{
"took": 42,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 7,
"max_score": 0.0,
"hits": []
},
"aggregations": {
"age_terms": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": 35,
"doc_count": 3
},
{
"key": 25,
"doc_count": 2
},
{
"key": 32,
"doc_count": 2
}
]
}
}
}
结果说明:
“doc_count_error_upper_bound”: 0:文档计数的最大偏差值
“sum_other_doc_count”: 463:未返回的其他项的文档数
默认情况下返回按文档计数从高到低的前10个分组:
2. filter Aggregation 对满足过滤查询的文档进行聚合计算
POST /megacorp/employee/_search?size=0
{
"aggs": {
"age_terms": {
"filter": {"match":{"gender":"F"}},
"aggs": {
"avg_age": {
"avg": {
"field": "age"
}
}
}
}
}
}
3. Range Aggregation 范围分组聚合
GET /megacorp/employee/_search?size=0 HTTP/1.1
Host: 192.168.31.233:9200
Content-Type: application/json
{
"aggs": {
"age_range": {
"range": {
"field": "age",
"ranges": [
{
"to": 25
},
{
"from": 25,
"to": 35
},
{
"from": 35
}
]
}
}
}
}
Results:
{
"took": 7,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 7,
"max_score": 0.0,
"hits": []
},
"aggregations": {
"age_range": {
"buckets": [
{
"key": "*-25.0",
"to": 25.0,
"doc_count": 0
},
{
"key": "25.0-35.0",
"from": 25.0,
"to": 35.0,
"doc_count": 4
},
{
"key": "35.0-*",
"from": 35.0,
"doc_count": 3
}
]
}
}
}
5. Date Range Aggregation 时间范围分组聚合
6. Date Histogram Aggregation 时间直方图(柱状)聚合
7. Missing Aggregation 缺失值的桶聚合
8. Geo Distance Aggregation 地理距离分区聚合
参考官网链接:
https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-geodistance-aggregation.html