计算emp每个sex下的数量
将文本field的fielddata属性设置为true
PUT /company/_mapping/emp
{
"properties": {
"sex": {
"type": "text",
"fielddata": true
}
}
}
执行结果
{
"acknowledged": true
}
统计男女人数
一般统计方式
GET /company/emp/_search
{
"aggs": {
"group_by_tags": {
"terms": { "field": "sex" }
}
}
}
添加size=0,优化统计结果
GET /company/emp/_search
{
"size": 0,
"aggs": {
"all_tags": {
"terms": { "field": "sex" }
}
}
}
执行结果
{
"took": 21,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 9,
"max_score": 0,
"hits": []
},
"aggregations": {
"all_tags": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "man",
"doc_count": 5
},
{
"key": "woman",
"doc_count": 4
}
]
}
}
}
对hobby中包含trivel的emp,计算每个sex下的数量
GET /company/emp/_search
{
"size": 0,
"query": {
"match": {
"hobby": "trivel"
}
},
"aggs": {
"all_tags": {
"terms": {
"field": "sex"
}
}
}
}
执行结果
{
"took": 33,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 3,
"max_score": 0,
"hits": []
},
"aggregations": {
"all_tags": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "man",
"doc_count": 2
},
{
"key": "woman",
"doc_count": 1
}
]
}
}
}
先按sex分组,算出每组的数量,再计算每个sex下的商品的平均年龄
GET /company/emp/_search
{
"size": 0,
"aggs" : {
"group_by_tags" : {
"terms" : { "field" : "sex" },
"aggs" : {
"avg_age" : {
"avg" : { "field" : "age" }
}
}
}
}
}
执行结果
{
"took": 13,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 9,
"max_score": 0,
"hits": []
},
"aggregations": {
"group_by_tags": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "man",
"doc_count": 5,
"avg_age": {
"value": 24.2
}
},
{
"key": "woman",
"doc_count": 4,
"avg_age": {
"value": 22
}
}
]
}
}
}
计算每个sex下的平均年龄,并且按照平均年龄降序排序
GET /company/emp/_search
{
"size": 0,
"aggs" : {
"all_tags" : {
"terms" : { "field" : "sex", "order": { "avg_age": "desc" } },
"aggs" : {
"avg_age" : {
"avg" : { "field" : "age" }
}
}
}
}
}
执行结果
{
"took": 7,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 9,
"max_score": 0,
"hits": []
},
"aggregations": {
"all_tags": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "man",
"doc_count": 5,
"avg_age": {
"value": 24.2
}
},
{
"key": "woman",
"doc_count": 4,
"avg_age": {
"value": 22
}
}
]
}
}
}
按照指定的年龄范围区间进行分组,然后在每组内再按照sex进行分组,最后再计算每组的平均年龄
GET /company/emp/_search
{
"size": 0,
"aggs": {
"group_by_age": {
"range": {
"field": "age",
"ranges": [
{
"from": 0,
"to": 10
},
{
"from": 10,
"to": 20
},
{
"from": 20,
"to": 30
},
{
"from": 30,
"to": 100
}
]
},
"aggs": {
"group_by_sex": {
"terms": {
"field": "sex"
},
"aggs": {
"average_age": {
"avg": {
"field": "age"
}
}
}
}
}
}
}
}
执行结果
{
"took": 16,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 9,
"max_score": 0,
"hits": []
},
"aggregations": {
"group_by_age": {
"buckets": [
{
"key": "0.0-10.0",
"from": 0,
"to": 10,
"doc_count": 0,
"group_by_sex": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": []
}
},
{
"key": "10.0-20.0",
"from": 10,
"to": 20,
"doc_count": 2,
"group_by_sex": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "man",
"doc_count": 1,
"average_age": {
"value": 17
}
},
{
"key": "woman",
"doc_count": 1,
"average_age": {
"value": 18
}
}
]
}
},
{
"key": "20.0-30.0",
"from": 20,
"to": 30,
"doc_count": 5,
"group_by_sex": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "woman",
"doc_count": 3,
"average_age": {
"value": 23.333333333333332
}
},
{
"key": "man",
"doc_count": 2,
"average_age": {
"value": 21
}
}
]
}
},
{
"key": "30.0-100.0",
"from": 30,
"to": 100,
"doc_count": 2,
"group_by_sex": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "man",
"doc_count": 2,
"average_age": {
"value": 31
}
}
]
}
}
]
}
}
}