度量聚合:求字段的平均值,最小值,最大值,总和等
桶聚合:将文档分成不同的桶,桶的划分可以根据字段的值,范围,日期间隔
管道聚合:在桶聚合的结果上执行进一步计算
进行聚合的语法如下
{
"aggs": {
"" : {
"" : {
"field": ""
}
}
}
}
聚合也可以进行嵌套
{
"aggs": {
"" : {
"" : {
"field": ""
},
"aggs": {
"" : {
"" : {
"field": ""
}
}
}
}
}
}
聚集被命名为 avg_age
POST user/_search
{
"query": {
"match_all": {}
},
"aggs": {"avg_age": {"avg": {"field": "age"}}}
}
POST user/_search
{
"query": {
"match_all": {}
},
"aggs": {"max_age": {"max": {"field": "age"}}}
}
POST user/_search
{
"query": {
"match_all": {}
},
"aggs": {"min_age": {"min": {"field": "age"}}}
}
POST user/_search
{
"query": {
"match_all": {}
},
"aggs": {"sum_age": {"sum": {"field": "age"}}}
}
按照某个字段的值进行聚合
POST user/_search
{
"query": {
"match_all": {}
},
"aggs": {"group_by_age": {"terms": {"field": "age"}}}
}
按照某个字段的范围进行聚合,from提供区间下界(包括),to提供区间上界(不包括)
POST user/_search
{
"query": {
"match_all": {}
},
"aggs": {
"age_ranges":{
"range": {
"field": "age",
"ranges": [
{ "to": 10 },
{ "from": 10, "to": 20 },
{ "from": 20 }
]
}
}
}
}
POST user/_search
{
"query": {
"match_all": {}
},
"aggs": {
"age_ranges": {
"range": {
"field": "age",
"ranges": [
{ "to": 10 }, { "from": 10, "to": 20 }, { "from": 20 }
]
},
"aggs": {
"age_avg": {"avg": {"field": "age"}}
}
},
"range_avg": {
"avg_bucket": {"buckets_path": "age_ranges>age_avg"}
}
}
}
对年龄分组,并求分组后的平均值,然后对分组的平均值再求平均值
POST user/_search
{
"query": {
"match_all": {}
},
"aggs": {
"age_ranges": {
"range": {
"field": "age",
"ranges": [
{ "to": 10 }, { "from": 10, "to": 20 }, { "from": 20 }
]
},
"aggs": {
"age_sum": { "sum": {"field": "age"} }
}
},
"range_sum": {
"sum_bucket": { "buckets_path": "age_ranges>age_sum" }
}
}
}
对年龄分组,并求分组后的和,然后对分组的和再求和
[1]https://www.elastic.co/guide/en/elasticsearch/reference/8.11/search-aggregations.html
[2]https://juejin.cn/post/7103514121642983455