Elasticsearch进阶二
聚合函数 1.搜索address中包含mill的所有人的年龄分布及平均年龄和平均薪资
GET /bank/_search
{
"query": {
"match": {
"address": "mill"
}
},
"aggs": {
"ageDistributed": { //取名字
"terms": {
"field": "age",
"size": 10
}
},
"aggAvg":{ //取名字
"avg": {
"field": "age"
}
},
"balanceAvg":{ //取名字
"avg": {
"field": "balance"
}
}
},
"size": 0 //不看查出来的数据,只看聚合数据
}
2.按年龄聚合,并且查出这些年龄段的平均薪资
GET /bank/_search
{
"query": {
"match_all": {}
},
"aggs": {
"aggAgeFenBu": {
"terms": {
"field": "age",
"size": 100
},
"aggs": { //子聚合
"balanceAvg": {
"avg": {
"field": "balance"
}
}
}
}
}
}
3.查出总体年龄分布,并且这些年龄段中男生的平均薪资和女生的平均薪资。以及这个年龄段的总体平均薪资
GET /bank/_search
{
"aggs": {
"aggAgeFenBu": {
"terms": {
"field": "age",
"size": 10
},"aggs": {
"genderAgg": {
"terms": {
"field": "gender.keyword",
"size": 100
},"aggs": {
"balanceAvg": {
"avg": {
"field": "balance"
}
}
}
},
"balanceAvg":{
"avg": {
"field": "balance"
}
}
}
}
}
}