DSL-聚合查询
聚合允许使用者对 es 文档进行统计分析,类似与关系型数据库中的 group by,当然还有很多其他的聚合,如取最大值、平均值等
使用aggs关键属性进行嵌套
# max
对某个字段取最大值 max
在 Postman 中,向 ES 服务器发 GET 请求
GET http://localhost:9200/student/_search
1
body
- 注意设置size为0,否则结果会将hit打印出来
{
"aggs" : {
"max_age": {
"max": {
"field":"age"
}
}
},
"size":0
}
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
response
{
"took": 15,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 5,
"relation": "eq"
},
"max_score": null,
"hits": []
},
"aggregations": {
"max_age": {
"value": 50.0
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# min
对某个字段取最小值 min
在 Postman 中,向 ES 服务器发 GET 请求
GET http://localhost:9200/student/_search
1
body
{
"aggs" : {
"min_age": {
"min": {
"field":"age"
}
}
},
"size":0
}
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
response
{
"took": 4,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 5,
"relation": "eq"
},
"max_score": null,
"hits": []
},
"aggregations": {
"min_age": {
"value": 20.0
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# sum
对某个字段求和 sum
在 Postman 中,向 ES 服务器发 GET 请求
GET http://localhost:9200/student/_search
1
body
{
"aggs" : {
"sum_age": {
"sum": {
"field":"age"
}
}
},
"size":0
}
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
response
{
"took": 11,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 5,
"relation": "eq"
},
"max_score": null,
"hits": []
},
"aggregations": {
"sum_age": {
"value": 170.0
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# avg
对某个字段取平均值 avg
在 Postman 中,向 ES 服务器发 GET 请求
GET http://localhost:9200/student/_search
1
body
{
"aggs" : {
"avg_age": {
"avg": {
"field":"age"
}
}
},
"size":0
}
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
response
{
"took": 5,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 5,
"relation": "eq"
},
"max_score": null,
"hits": []
},
"aggregations": {
"avg_age": {
"value": 34.0
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# cardinality 去重后求总个数
对某个字段的值进行去重之后再取总数,类似于mysql中distinct_age后求count
在 Postman 中,向 ES 服务器发 GET 请求
GET http://localhost:9200/student/_search
1
body
{
"aggs" : {
"distinct_age": {
"cardinality": {
"field":"age"
}
}
},
"size":0
}
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
response
{
"took": 5,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 5,
"relation": "eq"
},
"max_score": null,
"hits": []
},
"aggregations": {
"distinct_age": {
"value": 4
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# stats 聚合
stats 聚合,对某个字段一次性返回 count,max,min,avg 和 sum 五个指标
在 Postman 中,向 ES 服务器发 GET 请求
GET http://localhost:9200/student/_search
1
body
{
"aggs" : {
"stats_age": {
"stats": {
"field":"age"
}
}
},
"size":0
}
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
response
{
"took": 8,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 5,
"relation": "eq"
},
"max_score": null,
"hits": []
},
"aggregations": {
"stats_age": {
"count": 5,
"min": 20.0,
"max": 50.0,
"avg": 34.0,
"sum": 170.0
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# 桶聚合
桶聚和相当于 sql 中的 group by 语句
# terms分组统计
在 Postman 中,向 ES 服务器发 GET 请求
GET http://localhost:9200/student/_search
1
body
- 类似于select age, count(1) from index group by age
{
"aggs" : {
"age_groupby": {
"terms": {
"field":"age"
}
}
},
"size":0
}
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
response
{
"took": 25,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 5,
"relation": "eq"
},
"max_score": null,
"hits": []
},
"aggregations": {
"age_groupby": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": 30,
"doc_count": 2
},
{
"key": 20,
"doc_count": 1
},
{
"key": 40,
"doc_count": 1
},
{
"key": 50,
"doc_count": 1
}
]
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# terms分组后再聚合
在 Postman 中,向 ES 服务器发 GET 请求
GET http://localhost:9200/student/_search
1
body
- 类似于sql中:select age, sum(age), count(1) from index group by age
{
"aggs" : {
"age_groupby": {
"terms": {"field":"age"},
"aggs": {
"sum_age": {
"sum":{"field":"age"}
}
}
}
},
"size":0
}
1
2
3
4
5
6
7
8
9
10
11
12
13
2
3
4
5
6
7
8
9
10
11
12
13
response
{
"took": 21,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 5,
"relation": "eq"
},
"max_score": null,
"hits": []
},
"aggregations": {
"age_groupby": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": 30,
"doc_count": 2,
"sum_age": {
"value": 60.0
}
},
{
"key": 20,
"doc_count": 1,
"sum_age": {
"value": 20.0
}
},
{
"key": 40,
"doc_count": 1,
"sum_age": {
"value": 40.0
}
},
{
"key": 50,
"doc_count": 1,
"sum_age": {
"value": 50.0
}
}
]
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
Last Updated: 2022/02/05, 15:58:51