聚合
功能
- 聚合提供了对数据进行组合和提取统计结果的能力。
- 同一个返回中可以分别返回hits结果和聚合结果
例子
-
GET /bank/_search
{
"size": 0,
"aggs": { //聚合
"group_by_state": { //聚合名字
"terms": {
"field": "state.keyword", //按照state.keyword字段聚合
"order": { //排序
"average_balance": "desc" //按照average_balance倒序排序
}
},
"aggs": { //聚合
"average_balance": { //聚合名字
"avg": { //平均值
"field": "balance" //计算balance字段的平均值
}
}
}
}
}
}
-
GET /bank/_search
{
"size": 0,
"aggs": {
"group_by_age": { //通过age分组
"range": {
"field": "age", //按照age字段
"ranges": [ //年龄分段范围
{
"from": 20,
"to": 30
},
{
"from": 30,
"to": 40
},
{
"from": 40,
"to": 50
}
]
},
"aggs": {
"group_by_gender": { //按照gender聚合
"terms": {
"field": "gender.keyword"
},
"aggs": {
"average_balance": { //按照balance聚合
"avg": {
"field": "balance"
}
}
}
}
}
}
}
}
网友评论