1. The previous blog about ES aggregation
Elasticsearch tutorial (3) ES aggregate query DSL
Elasticsearch tutorial (4) High Level REST Client API query aggregation grouping
Elasticsearch Tutorial (5) Index Aggregation SQL DSL JavaAPI
Elasticsearch Tutorial (6) Bucket Aggregation Query DSL-Terms Aggregation
Elasticsearch tutorial (10) ES term terms prefix search aggregation query detailed summary
Elasticsearch tutorial (11) elasticsearch bucket aggregation query DSL
2. Test data
PUT /pigg/_doc/1
{
"name": "老亚瑟",
"age": 30,
"sex": "男",
"group": "日落圣殿",
"tag":["战士", "坦克"],
"date": "2019-12-26",
"friend": "安琪拉"
}
PUT /pigg/_doc/2
{
"name": "安琪拉",
"age": 16,
"sex": "女",
"group": "日落圣殿",
"tag":["法师"],
"date": "2019-01-01",
"friend": ""
}
PUT /pigg/_doc/3
{
"name": "凯",
"age": 28,
"sex": "男",
"group": "长城守卫军",
"tag":["战士"],
"date": "2020-01-01"
}
PUT /pigg/_doc/4
{
"name": "盾山",
"age": 38,
"sex": "男",
"group": "长城守卫军",
"tag":["辅助", "坦克"],
"date": "2020-02-02"
}
PUT /pigg/_doc/5
{
"name": "百里守约",
"age": 18,
"sex": "男",
"group": "长城守卫军",
"tag":["射手"],
"date": "2020-03-03"
}
PUT /pigg/_doc/6
{
"name": "李元芳",
"age": 15,
"sex": "男",
"group": "长安",
"tag":["刺客"],
"date": "2020-03-23"
}
PUT /pigg/_doc/7
{
"name": "陈咬金",
"age": 40,
"sex": "男",
"group": "长安",
"tag":["战士", "坦克"]
}
Three, group aggregation
- select count(1) from table group by…
GET /pigg/_search
{
"size": 0,
"aggs": {
"count_of_group": {
"terms": {
"field": "group.keyword"
}
}
}
}
The return is as follows:
"aggregations" : {
"count_of_group" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [
{
"key" : "长城守卫军",
"doc_count" : 3
},
{
"key" : "日落圣殿",
"doc_count" : 2
},
{
"key" : "长安",
"doc_count" : 2
}
]
}
}
Fourth, filter first, then polymerize
- select count(1) from table where … group by…
First filter the scope of the query data, and then aggregate, hits are the filtered data
GET /pigg/_search
{
"size": 10,
"query": {
"bool": {
"filter": [
{
"term": {
"sex.keyword": "男"
}
}
]
}
},
"aggs": {
"count_of_group": {
"terms": {
"field": "group.keyword"
}
}
}
}
Five, filter aggregation first, then inline terms aggregation
The aggregation result is the same as the above, the difference is that this hits contains all the data before the filter
GET /pigg/_search
{
"size": 10,
"aggs": {
"group_of_man": {
"filter": {
"term": {
"sex.keyword": "男"
}
},
"aggs": {
"count_of_group": {
"terms": {
"field": "group.keyword",
"size": 10
}
}
}
}
}
}
6. Filter aggregation first, then embedded terms aggregation, and then embedded avg aggregation
select count(1) avg(age) from table where … group by…
GET /pigg/_search
{
"size": 10,
"aggs": {
"group_of_man": {
"filter": {
"term": {
"sex.keyword": "男"
}
},
"aggs": {
"count_of_group": {
"terms": {
"field": "group.keyword",
"size": 10
},
"aggs": {
"avg_age": {
"avg": {
"field": "age"
}
}
}
}
}
}
}
}
"buckets" : [
{
"key" : "长城守卫军",
"doc_count" : 3,
"avg_age" : {
"value" : 28.0
}
},
{
"key" : "长安",
"doc_count" : 2,
"avg_age" : {
"value" : 27.5
}
},
{
"key" : "日落圣殿",
"doc_count" : 1,
"avg_age" : {
"value" : 30.0
}
}
]
Seven, filter aggregation first, then embedded terms aggregation, then embedded avg aggregation, and then sort by average age
select count(1) avg(age) from table where … group by… order by avg_age asc
GET /pigg/_search
{
"size": 10,
"aggs": {
"group_of_man": {
"filter": {
"term": {
"sex.keyword": "男"
}
},
"aggs": {
"count_of_group": {
"terms": {
"field": "group.keyword",
"size": 10,
"order": {
"avg_age": "asc"
}
},
"aggs": {
"avg_age": {
"avg": {
"field": "age"
}
}
}
}
}
}
}
}
Eight, realize the function of having having count
select count(1) avg(age) from table where … group by… having count > 1
GET /pigg/_search
{
"size": 10,
"aggs": {
"group_of_man": {
"filter": {
"term": {
"sex.keyword": "男"
}
},
"aggs": {
"count_of_group": {
"terms": {
"field": "group.keyword",
"size": 10
},
"aggs": {
"having": {
"bucket_selector": {
"buckets_path": {
"count_of_group": "_count"
},
"script": {
"source": "params.count_of_group > 1"
}
}
}
}
}
}
}
}
}
Nine, realize the function of having avg
select count(1) avg(age) from table where … group by… having avg_age > 28
GET /pigg/_search
{
"size":10,
"aggs":{
"group_of_man":{
"filter":{
"term":{
"sex.keyword":"男"
}
},
"aggs":{
"count_of_group":{
"terms":{
"field":"group.keyword",
"size":10
},
"aggs":{
"avg_age":{
"avg":{
"field":"age"
}
},
"avg_age_filter":{
"bucket_selector":{
"buckets_path":{
"avg_age":"avg_age"
},
"script":{
"source":"params.avg_age > 28"
}
}
}
}
}
}
}
}
}
Ten, to achieve distinct de-duplication
select count(distinct(group)) as count_of_distinct_group from pigg group by sex
GET /pigg/_search
{
"size":10,
"aggs":{
"count_of_group":{
"terms":{
"field":"sex.keyword"
},
"aggs":{
"count_of_distinct_group":{
"cardinality":{
"field":"group.keyword"
}
}
}
}
}
}