关系型数据库中除了有聚合函数外,还可以对查询出的数据进⾏分组group by,再在组上 进⾏指标聚合。在ES中称为桶聚合。
一:Terms Aggregation 根据字段项分组聚合
1.按照年龄进行分组
下面的size是10,如果分组很多的时候,这里可以进行调整,写100也是不会存在问题的
POST /nba/_search { "query": { "term": { "teamNameEn": { "value": "Rockets" } } }, "aggs": { "aggsAge": { "terms": { "field": "age", "size": 10 } } }, "size": 1 }
效果:
{ "took" : 3, "timed_out" : false, "_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 21, "relation" : "eq" }, "max_score" : 3.2723064, "hits" : [ { "_index" : "nba", "_type" : "_doc", "_id" : "86", "_score" : 3.2723064, "_source" : { "countryEn" : "Switzerland", "teamName" : "火箭", "birthDay" : 769233600000, "country" : "瑞士", "teamCityEn" : "Houston", "code" : "clint_capela", "displayAffiliation" : "Switzerland/Switzerland", "displayName" : "克林特 卡佩拉", "schoolType" : "", "teamConference" : "西部", "teamConferenceEn" : "Western", "weight" : "108.9 公斤", "teamCity" : "休斯顿", "playYear" : 5, "jerseyNo" : "15", "teamNameEn" : "Rockets", "draft" : 2014, "displayNameEn" : "Clint Capela", "heightValue" : 2.08, "birthDayStr" : "1994-05-18", "position" : "中锋", "age" : 25, "playerId" : "203991" } } ] }, "aggregations" : { "aggsAge" : { "doc_count_error_upper_bound" : 0, "sum_other_doc_count" : 3, "buckets" : [ { "key" : 21, "doc_count" : 4 }, { "key" : 25, "doc_count" : 3 }, { "key" : 23, "doc_count" : 2 }, { "key" : 30, "doc_count" : 2 }, { "key" : 34, "doc_count" : 2 }, { "key" : 22, "doc_count" : 1 }, { "key" : 24, "doc_count" : 1 }, { "key" : 26, "doc_count" : 1 }, { "key" : 27, "doc_count" : 1 }, { "key" : 29, "doc_count" : 1 } ] } } }
二:order 分组聚合排序
2.分组聚合排序
先进行分组,然后使用order进行排序
POST /nba/_search { "query": { "term": { "teamNameEn": { "value": "Rockets" } } }, "aggs": { "aggsAge": { "terms": { "field": "age", "size": 100, "order": { "_key": "asc" } } } }, "size": 1 }
效果:
{ "took" : 6, "timed_out" : false, "_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 21, "relation" : "eq" }, "max_score" : 3.2723064, "hits" : [ { "_index" : "nba", "_type" : "_doc", "_id" : "86", "_score" : 3.2723064, "_source" : { "countryEn" : "Switzerland", "teamName" : "火箭", "birthDay" : 769233600000, "country" : "瑞士", "teamCityEn" : "Houston", "code" : "clint_capela", "displayAffiliation" : "Switzerland/Switzerland", "displayName" : "克林特 卡佩拉", "schoolType" : "", "teamConference" : "西部", "teamConferenceEn" : "Western", "weight" : "108.9 公斤", "teamCity" : "休斯顿", "playYear" : 5, "jerseyNo" : "15", "teamNameEn" : "Rockets", "draft" : 2014, "displayNameEn" : "Clint Capela", "heightValue" : 2.08, "birthDayStr" : "1994-05-18", "position" : "中锋", "age" : 25, "playerId" : "203991" } } ] }, "aggregations" : { "aggsAge" : { "doc_count_error_upper_bound" : 0, "sum_other_doc_count" : 0, "buckets" : [ { "key" : 21, "doc_count" : 4 }, { "key" : 22, "doc_count" : 1 }, { "key" : 23, "doc_count" : 2 }, { "key" : 24, "doc_count" : 1 }, { "key" : 25, "doc_count" : 3 }, { "key" : 26, "doc_count" : 1 }, { "key" : 27, "doc_count" : 1 }, { "key" : 29, "doc_count" : 1 }, { "key" : 30, "doc_count" : 2 }, { "key" : 31, "doc_count" : 1 }, { "key" : 33, "doc_count" : 1 }, { "key" : 34, "doc_count" : 2 }, { "key" : 37, "doc_count" : 1 } ] } } }
3.⽕箭队根据年龄进⾏分组,分组信息通过⽂档数从⼤到⼩排序 (通过⽂档数)
POST /nba/_search { "query": { "term": { "teamNameEn": { "value": "Rockets" } } }, "aggs": { "aggsAge": { "terms": { "field": "age", "size": 100, "order": { "_count": "desc" } } } }, "size": 1 }
效果:
{ "took" : 2, "timed_out" : false, "_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 21, "relation" : "eq" }, "max_score" : 3.2723064, "hits" : [ { "_index" : "nba", "_type" : "_doc", "_id" : "86", "_score" : 3.2723064, "_source" : { "countryEn" : "Switzerland", "teamName" : "火箭", "birthDay" : 769233600000, "country" : "瑞士", "teamCityEn" : "Houston", "code" : "clint_capela", "displayAffiliation" : "Switzerland/Switzerland", "displayName" : "克林特 卡佩拉", "schoolType" : "", "teamConference" : "西部", "teamConferenceEn" : "Western", "weight" : "108.9 公斤", "teamCity" : "休斯顿", "playYear" : 5, "jerseyNo" : "15", "teamNameEn" : "Rockets", "draft" : 2014, "displayNameEn" : "Clint Capela", "heightValue" : 2.08, "birthDayStr" : "1994-05-18", "position" : "中锋", "age" : 25, "playerId" : "203991" } } ] }, "aggregations" : { "aggsAge" : { "doc_count_error_upper_bound" : 0, "sum_other_doc_count" : 0, "buckets" : [ { "key" : 21, "doc_count" : 4 }, { "key" : 25, "doc_count" : 3 }, { "key" : 23, "doc_count" : 2 }, { "key" : 30, "doc_count" : 2 }, { "key" : 34, "doc_count" : 2 }, { "key" : 22, "doc_count" : 1 }, { "key" : 24, "doc_count" : 1 }, { "key" : 26, "doc_count" : 1 }, { "key" : 27, "doc_count" : 1 }, { "key" : 29, "doc_count" : 1 }, { "key" : 31, "doc_count" : 1 }, { "key" : 33, "doc_count" : 1 }, { "key" : 37, "doc_count" : 1 } ] } } }
4.每⽀球队按该队所有球员的平均年龄进⾏分组排序 (通过分组指标值)
POST /nba/_search { "aggs": { "aggsTeamNameEn": { "terms": { "field": "teamNameEn", "size": 100, "order": { "avgAge": "desc" } }, "aggs":{ "avgAge":{ "avg": { "field": "age" } } } } }, "size": 1 }
效果:
{ "took" : 44, "timed_out" : false, "_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 566, "relation" : "eq" }, "max_score" : 1.0, "hits" : [ { "_index" : "nba", "_type" : "_doc", "_id" : "1", "_score" : 1.0, "_source" : { "countryEn" : "United States", "teamName" : "老鹰", "birthDay" : 831182400000, "country" : "美国", "teamCityEn" : "Atlanta", "code" : "jaylen_adams", "displayAffiliation" : "United States", "displayName" : "杰伦 亚当斯", "schoolType" : "College", "teamConference" : "东部", "teamConferenceEn" : "Eastern", "weight" : "86.2 公斤", "teamCity" : "亚特兰大", "playYear" : 1, "jerseyNo" : "10", "teamNameEn" : "Hawks", "draft" : 2018, "displayNameEn" : "Jaylen Adams", "heightValue" : 1.88, "birthDayStr" : "1996-05-04", "position" : "后卫", "age" : 23, "playerId" : "1629121" } } ] }, "aggregations" : { "aggsTeamNameEn" : { "doc_count_error_upper_bound" : 0, "sum_other_doc_count" : 0, "buckets" : [ { "key" : "Bucks", "doc_count" : 14, "avgAge" : { "value" : 28.142857142857142 } }, { "key" : "Mavericks", "doc_count" : 20, "avgAge" : { "value" : 27.85 } }, { "key" : "Lakers", "doc_count" : 21, "avgAge" : { "value" : 27.714285714285715 } }, { "key" : "Raptors", "doc_count" : 17, "avgAge" : { "value" : 26.823529411764707 } }, { "key" : "Wizards", "doc_count" : 17, "avgAge" : { "value" : 26.823529411764707 } }, { "key" : "Heat", "doc_count" : 17, "avgAge" : { "value" : 26.764705882352942 } }, { "key" : "Rockets", "doc_count" : 21, "avgAge" : { "value" : 26.761904761904763 } }, { "key" : "Spurs", "doc_count" : 20, "avgAge" : { "value" : 26.75 } }, { "key" : "Jazz", "doc_count" : 17, "avgAge" : { "value" : 26.647058823529413 } }, { "key" : "Pistons", "doc_count" : 21, "avgAge" : { "value" : 26.476190476190474 } }, { "key" : "76ers", "doc_count" : 22, "avgAge" : { "value" : 26.363636363636363 } }, { "key" : "Warriors", "doc_count" : 20, "avgAge" : { "value" : 26.25 } }, { "key" : "Timberwolves", "doc_count" : 15, "avgAge" : { "value" : 26.0 } }, { "key" : "Grizzlies", "doc_count" : 23, "avgAge" : { "value" : 25.91304347826087 } }, { "key" : "Magic", "doc_count" : 19, "avgAge" : { "value" : 25.894736842105264 } }, { "key" : "Kings", "doc_count" : 22, "avgAge" : { "value" : 25.863636363636363 } }, { "key" : "Suns", "doc_count" : 20, "avgAge" : { "value" : 25.85 } }, { "key" : "Nets", "doc_count" : 18, "avgAge" : { "value" : 25.77777777777778 } }, { "key" : "Cavaliers", "doc_count" : 21, "avgAge" : { "value" : 25.714285714285715 } }, { "key" : "Thunder", "doc_count" : 18, "avgAge" : { "value" : 25.555555555555557 } }, { "key" : "Clippers", "doc_count" : 19, "avgAge" : { "value" : 25.526315789473685 } }, { "key" : "Trail Blazers", "doc_count" : 16, "avgAge" : { "value" : 25.4375 } }, { "key" : "Celtics", "doc_count" : 17, "avgAge" : { "value" : 25.176470588235293 } }, { "key" : "Hawks", "doc_count" : 18, "avgAge" : { "value" : 25.166666666666668 } }, { "key" : "Hornets", "doc_count" : 19, "avgAge" : { "value" : 25.05263157894737 } }, { "key" : "Pacers", "doc_count" : 14, "avgAge" : { "value" : 24.928571428571427 } }, { "key" : "Nuggets", "doc_count" : 18, "avgAge" : { "value" : 24.555555555555557 } }, { "key" : "Knicks", "doc_count" : 21, "avgAge" : { "value" : 24.523809523809526 } }, { "key" : "Bulls", "doc_count" : 22, "avgAge" : { "value" : 24.454545454545453 } }, { "key" : "Pelicans", "doc_count" : 19, "avgAge" : { "value" : 24.36842105263158 } } ] } } }
三:筛选分组聚合
1.湖⼈和⽕箭队按球队平均年龄进⾏分组排序 (指定值列表)
POST /nba/_search { "aggs": { "aggsTeamNameEn": { "terms": { "field": "teamNameEn", "include": ["Lakers","Rockets","Warriors"], "exclude": ["Warriors"], "size": 100, "order": { "avgAge": "desc" } }, "aggs":{ "avgAge":{ "avg": { "field": "age" } } } } }, "size": 1 }
效果:
{ "took" : 7, "timed_out" : false, "_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 566, "relation" : "eq" }, "max_score" : 1.0, "hits" : [ { "_index" : "nba", "_type" : "_doc", "_id" : "1", "_score" : 1.0, "_source" : { "countryEn" : "United States", "teamName" : "老鹰", "birthDay" : 831182400000, "country" : "美国", "teamCityEn" : "Atlanta", "code" : "jaylen_adams", "displayAffiliation" : "United States", "displayName" : "杰伦 亚当斯", "schoolType" : "College", "teamConference" : "东部", "teamConferenceEn" : "Eastern", "weight" : "86.2 公斤", "teamCity" : "亚特兰大", "playYear" : 1, "jerseyNo" : "10", "teamNameEn" : "Hawks", "draft" : 2018, "displayNameEn" : "Jaylen Adams", "heightValue" : 1.88, "birthDayStr" : "1996-05-04", "position" : "后卫", "age" : 23, "playerId" : "1629121" } } ] }, "aggregations" : { "aggsTeamNameEn" : { "doc_count_error_upper_bound" : 0, "sum_other_doc_count" : 0, "buckets" : [ { "key" : "Lakers", "doc_count" : 21, "avgAge" : { "value" : 27.714285714285715 } }, { "key" : "Rockets", "doc_count" : 21, "avgAge" : { "value" : 26.761904761904763 } } ] } } }
2.湖⼈和⽕箭队按球队平均年龄进⾏分组排序 (正则表达式匹配值)
POST /nba/_search
{
"aggs": {
"aggsTeamNameEn": {
"terms": {
"field": "teamNameEn",
"include": "Lakers|Ro.*|Warriors.*",
"exclude": "Warriors",
"size": 100,
"order": {
"avgAge": "desc"
}
},
"aggs":{
"avgAge":{
"avg": {
"field": "age"
}
}
}
}
},
"size": 1
}
四:Range Aggregation 范围分组聚合
1.NBA球员年龄按20,20-35,35这样分组
POST /nba/_search { "aggs": { "ageRange": { "range": { "field": "age", "ranges": [ { "to": 20 },{ "from": 20, "to":35 },{ "from":35 } ] } } }, "size": 1 }
效果:
{ "took" : 2, "timed_out" : false, "_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 566, "relation" : "eq" }, "max_score" : 1.0, "hits" : [ { "_index" : "nba", "_type" : "_doc", "_id" : "1", "_score" : 1.0, "_source" : { "countryEn" : "United States", "teamName" : "老鹰", "birthDay" : 831182400000, "country" : "美国", "teamCityEn" : "Atlanta", "code" : "jaylen_adams", "displayAffiliation" : "United States", "displayName" : "杰伦 亚当斯", "schoolType" : "College", "teamConference" : "东部", "teamConferenceEn" : "Eastern", "weight" : "86.2 公斤", "teamCity" : "亚特兰大", "playYear" : 1, "jerseyNo" : "10", "teamNameEn" : "Hawks", "draft" : 2018, "displayNameEn" : "Jaylen Adams", "heightValue" : 1.88, "birthDayStr" : "1996-05-04", "position" : "后卫", "age" : 23, "playerId" : "1629121" } } ] }, "aggregations" : { "ageRange" : { "buckets" : [ { "key" : "*-20.0", "to" : 20.0, "doc_count" : 15 }, { "key" : "20.0-35.0", "from" : 20.0, "to" : 35.0, "doc_count" : 531 }, { "key" : "35.0-*", "from" : 35.0, "doc_count" : 20 } ] } } }
2.NBA球员年龄按20,20-35,35这样分组 (起别名)
POST /nba/_search { "aggs": { "ageRange": { "range": { "field": "age", "ranges": [ { "to": 20, "key":"A" },{ "from": 20, "to":35, "key":"B" },{ "from":35, "key":"C" } ] } } }, "size": 1 }
3.Date Range Aggregation 时间范围分组聚合
POST /nba/_search { "aggs": { "aggsBirthday": { "range": { "field": "birthDay", "format":"yyyy-MM", "ranges": [ { "to": "1989-01" },{ "from": "1989-01", "to":"1999-01" },{ "from": "1999-01" } ] } } }, "size": 1 }
五:Date Histogram Aggregation 时间柱状图聚合
1.按天、⽉、年等进⾏聚合统计。
可按 year (1y), quarter (1q), month (1M), week (1w), day (1d), hour (1h), minute (1m), second (1s) 间隔聚合
NBA球员按出⽣年分组
POST /nba/_search { "aggs": { "aggsBirthday": { "date_histogram": { "field": "birthDay", "format": "yyyy-MM", "interval": "year" } } }, "size": 1 }
效果:
#! Deprecation: [interval] on [date_histogram] is deprecated, use [fixed_interval] or [calendar_interval] in the future. { "took" : 2, "timed_out" : false, "_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 566, "relation" : "eq" }, "max_score" : 1.0, "hits" : [ { "_index" : "nba", "_type" : "_doc", "_id" : "1", "_score" : 1.0, "_source" : { "countryEn" : "United States", "teamName" : "老鹰", "birthDay" : 831182400000, "country" : "美国", "teamCityEn" : "Atlanta", "code" : "jaylen_adams", "displayAffiliation" : "United States", "displayName" : "杰伦 亚当斯", "schoolType" : "College", "teamConference" : "东部", "teamConferenceEn" : "Eastern", "weight" : "86.2 公斤", "teamCity" : "亚特兰大", "playYear" : 1, "jerseyNo" : "10", "teamNameEn" : "Hawks", "draft" : 2018, "displayNameEn" : "Jaylen Adams", "heightValue" : 1.88, "birthDayStr" : "1996-05-04", "position" : "后卫", "age" : 23, "playerId" : "1629121" } } ] }, "aggregations" : { "aggsBirthday" : { "buckets" : [ { "key_as_string" : "1977-01", "key" : 220924800000, "doc_count" : 1 }, { "key_as_string" : "1978-01", "key" : 252460800000, "doc_count" : 1 }, { "key_as_string" : "1979-01", "key" : 283996800000, "doc_count" : 0 }, { "key_as_string" : "1980-01", "key" : 315532800000, "doc_count" : 3 }, { "key_as_string" : "1981-01", "key" : 347155200000, "doc_count" : 2 }, { "key_as_string" : "1982-01", "key" : 378691200000, "doc_count" : 3 }, { "key_as_string" : "1983-01", "key" : 410227200000, "doc_count" : 2 }, { "key_as_string" : "1984-01", "key" : 441763200000, "doc_count" : 8 }, { "key_as_string" : "1985-01", "key" : 473385600000, "doc_count" : 15 }, { "key_as_string" : "1986-01", "key" : 504921600000, "doc_count" : 19 }, { "key_as_string" : "1987-01", "key" : 536457600000, "doc_count" : 16 }, { "key_as_string" : "1988-01", "key" : 567993600000, "doc_count" : 27 }, { "key_as_string" : "1989-01", "key" : 599616000000, "doc_count" : 24 }, { "key_as_string" : "1990-01", "key" : 631152000000, "doc_count" : 35 }, { "key_as_string" : "1991-01", "key" : 662688000000, "doc_count" : 31 }, { "key_as_string" : "1992-01", "key" : 694224000000, "doc_count" : 36 }, { "key_as_string" : "1993-01", "key" : 725846400000, "doc_count" : 46 }, { "key_as_string" : "1994-01", "key" : 757382400000, "doc_count" : 45 }, { "key_as_string" : "1995-01", "key" : 788918400000, "doc_count" : 57 }, { "key_as_string" : "1996-01", "key" : 820454400000, "doc_count" : 56 }, { "key_as_string" : "1997-01", "key" : 852076800000, "doc_count" : 57 }, { "key_as_string" : "1998-01", "key" : 883612800000, "doc_count" : 39 }, { "key_as_string" : "1999-01", "key" : 915148800000, "doc_count" : 28 }, { "key_as_string" : "2000-01", "key" : 946684800000, "doc_count" : 15 } ] } } }