说明
聚合允许使用者对 es 文档进行统计分析,类似与关系型数据库中的count()、max()、group by
官方文档:
https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations.html
常见聚合
- max
- min
- avg
- sum
- terms
统计 最大点赞数量
查询最大点赞数量
相当于 SQL:select max(like) from toutiao_v1
GET /toutiao_v1/_search
{
"aggs": {
"max_like": { //本次聚合的名称,会出现在结果中,统计多个时有用
"max": { //相当于mysql的 max()
"field": "like" //对 like 字段求最大值
}
}
},
"size": 0 //其他记录不查询
}
注意: 执行查询时,要去掉注释
执行结果如下:
GET /toutiao_v1/_search
{
"took":1,
"timed_out":false,
"_shards":{
"total":1,
"successful":1,
"skipped":0,
"failed":0
},
"hits":{
"total":{
"value":4,
"relation":"eq"
},
"max_score":null,
"hits":[
]
},
"aggregations":{
"max_like":{
"value":983
}
}
}
统计多个
相当于 SQL:select max(like),min(like) from toutiao_v1
GET /toutiao_v1/_search
{
"aggs": {
"max_like": {
"max": {
"field": "like"
}
},
"min_like": {
"min": {
"field": "like"
}
}
},
"size": 0
}
查询结果如下:
{
"took":0,
"timed_out":false,
"_shards":{
"total":1,
"successful":1,
"skipped":0,
"failed":0
},
"hits":{
"total":{
"value":4,
"relation":"eq"
},
"max_score":null,
"hits":[
]
},
"aggregations":{
"min_like":{
"value":229
},
"max_like":{
"value":983
}
}
}
terms
相当于 mysql 中的 select count() from 表 group by ... having ...
简单统计
统计各个分类的文档数量
相当于 SQL:select type as key, count(1) as doc_count from toutiao_v1 group by type
GET /toutiao_v1/_search
{
"aggs": {
"terms_type": { //本次聚合的名称,会出现在结果中
"terms": {
"field": "type" //根据 type 字段分组统计
}
}
},
"size": 0
}
加上 having
相当于 SQL:select type as key, count(1) as doc_count from toutiao_v1 group by type having count(1) >=2
GET /toutiao_v1/_search
{
"aggs": {
"terms_type": { //本次聚合的名称,会出现在结果中
"terms": {
"field": "type", //根据 type 字段分组统计
"min_doc_count": 2 //控制最小计数,统计数量大于等于3才显示
}
}
},
"size": 0
}
限制结果数量、指定排序
相当于SQL:
select type as key, count(1) as doc_count from toutiao_v1 group by type having count(1) >=3 order by count(1) desc limit 1
GET /toutiao_v1/_search
{
"aggs": {
"terms_type": { //本次聚合的名称,会出现在结果中
"terms": {
"field": "type", //根据 type 字段分组统计
"min_doc_count": 3, //控制最小计数,统计数量大于等于3才显示
"size": 3, //返回结果的数量,最多返回3条记录
"order": {"_count": "desc" } //排序方式,根据统计的数量 正序、倒序排序
}
}
},
"size": 0
}
执行结果:
{
"took":560,
"timed_out":false,
"_shards":{
"total":1,
"successful":1,
"skipped":0,
"failed":0
},
"hits":{
"total":{
"value":4,
"relation":"eq"
},
"max_score":null,
"hits":[
]
},
"aggregations":{
"terms_type":{
"doc_count_error_upper_bound":0,
"sum_other_doc_count":0,
"buckets":[
{
"key":"java",
"doc_count":3
},
{
"key":"mysql",
"doc_count":1
}
]
}
}
}
不能统计 text 类型字段
会报错
GET /toutiao_v1/_search
{
"aggs": {
"terms_content": {
"terms": {
"field": "content", //这里针对text类型的content进行统计
"size": 3
}
}
},
"size": 0
}
报错如下:
Text fields are not optimised for operations that require per-document field data like aggregations and sorting
再次聚合
统计各个分类的文档数量,并且这些分类中,最大的点赞数量
相当于 SQL:select count(1),max(like) from toutiao_v1 group by type
GET /toutiao_v1/_search
{
"aggs": {
"terms_type": { //本次聚合的名称,会出现在结果中
"terms": {
"field": "type",
"size": 3
},
"aggs": {
"max_like": { //本次聚合的名称,会出现在结果中
"max": {
"field": "like"
}
}
}
}
},
"size": 0
}
执行结果如下:
{
"took":15,
"timed_out":false,
"_shards":{
"total":1,
"successful":1,
"skipped":0,
"failed":0
},
"hits":{
"total":{
"value":4,
"relation":"eq"
},
"max_score":null,
"hits":[
]
},
"aggregations":{
"terms_type":{
"doc_count_error_upper_bound":0,
"sum_other_doc_count":0,
"buckets":[
{
"key":"java",
"doc_count":3,
"max_like":{
"value":983
}
},
{
"key":"mysql",
"doc_count":1,
"max_like":{
"value":397
}
}
]
}
}
}

