在聚合查询之前,需要设置文本field的fielddata为true
PUT /person/_mapping
{
"properties": {
"tags": {
"type": "text",
"fielddata": true
}
}
}
- 需求:
查询每个tag下的人数
GET /person/_search
{
"aggs": {
"group_by_tags": {
"terms": {
"field": "tags"
}
}
}
}
group_by_tags
是自定义的分组名称。
搜索结果: ```json { //省略了查询结果 “aggregations” : { “group_by_tags” : {
} } }"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [
{
"key" : "cat",
"doc_count" : 2
},
{
"key" : "cute",
"doc_count" : 2
},
{
"key" : "beatiful",
"doc_count" : 1
},
{
"key" : "boy",
"doc_count" : 1
}
]
- **需求**:查询名称中包含“man”的用户,然后根据tag分组
```json
GET /person/_search
{
"query": {
"match": {
"name": "man"
}
},
"aggs": {
"group_by_name": {
"terms": {
"field": "tags",
"size": 10
}
}
}
}
搜索结果
{
//省略了查询结果
"aggregations" : {
"group_by_name" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [
{
"key" : "beatiful",
"doc_count" : 1
},
{
"key" : "boy",
"doc_count" : 1
},
{
"key" : "cat",
"doc_count" : 1
},
{
"key" : "cute",
"doc_count" : 1
}
]
}
}
}
需求:先分组,然后再计算平均年龄
GET /person/_search
{
"aggs": {
"group_by_tags": {
"terms": {
"field": "tags",
"size": 10
},
"aggs": {
"avg_age": {
"avg": {
"field": "age"
}
}
}
}
}
}
{
//省略了查询结果
"aggregations" : {
"group_by_tags" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [
{
"key" : "cat",
"doc_count" : 2,
"avg_age" : {
"value" : 15.0
}
},
{
"key" : "cute",
"doc_count" : 2,
"avg_age" : {
"value" : 22.5
}
},
{
"key" : "beatiful",
"doc_count" : 1,
"avg_age" : {
"value" : 25.0
}
},
{
"key" : "boy",
"doc_count" : 1,
"avg_age" : {
"value" : 10.0
}
}
]
}
}
}
需求:计算每个person的平均年龄,然后按照平均年龄降序排序
GET /person/_search
{
"aggs": {
"group_by_tags": {
"terms": {
"field": "tags",
"order": {
"avg_key": "desc"
}
},
"aggs": {
"avg_key": {
"avg": {
"field": "age"
}
}
}
}
}
}
avg_key
是自定义的一个值,需要跟聚合的值保持一致。{
//省略了查询结果
"aggregations" : {
"group_by_tags" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [
{
"key" : "beatiful",
"doc_count" : 1,
"avg_key" : {
"value" : 25.0
}
},
{
"key" : "cute",
"doc_count" : 2,
"avg_key" : {
"value" : 22.5
}
},
{
"key" : "cat",
"doc_count" : 2,
"avg_key" : {
"value" : 15.0
}
},
{
"key" : "boy",
"doc_count" : 1,
"avg_key" : {
"value" : 10.0
}
}
]
}
}
}
需求:根据指定年龄范围分组,然后每组内再根据tag进行分组,最后计算没组的平均年龄
GET /person/_search
{
"aggs": {
"range_age": {
"range": {
"field": "age",
"ranges": [
{
"from": 10,
"to": 20
},
{
"from": 20,
"to": 30
},
{
"from": 30,
"to": 40
}
]
},
"aggs": {
"group_by_tags": {
"terms": {
"field": "tags",
"size": 10
},
"aggs": {
"avg_age": {
"avg": {
"field": "age"
}
}
}
}
}
}
}
}
{
//省略了查询结果
"aggregations" : {
"range_age" : {
"buckets" : [
{
"key" : "10.0-20.0",
"from" : 10.0,
"to" : 20.0,
"doc_count" : 1,
"group_by_tags" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [
{
"key" : "boy",
"doc_count" : 1,
"avg_age" : {
"value" : 10.0
}
},
{
"key" : "cat",
"doc_count" : 1,
"avg_age" : {
"value" : 10.0
}
}
]
}
},
{
"key" : "20.0-30.0",
"from" : 20.0,
"to" : 30.0,
"doc_count" : 2,
"group_by_tags" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [
{
"key" : "cute",
"doc_count" : 2,
"avg_age" : {
"value" : 22.5
}
},
{
"key" : "beatiful",
"doc_count" : 1,
"avg_age" : {
"value" : 25.0
}
},
{
"key" : "cat",
"doc_count" : 1,
"avg_age" : {
"value" : 20.0
}
}
]
}
},
{
"key" : "30.0-40.0",
"from" : 30.0,
"to" : 40.0,
"doc_count" : 0,
"group_by_tags" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [ ]
}
}
]
}
}
}