Elastic实战Rest统计

索引信息

查看索引信息

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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
[root@localhost elasticsearch-6.0.0 ]$ curl -XGET "http://127.0.0.1:9200/db_test?pretty=true"
{
"db_test" : {
"aliases" : { },
"mappings" : {
"tb_opus" : {
"properties" : {
"aliasName" : {
"type" : "text",
"fields" : {
"origin" : {
"type" : "keyword"
},
"pinyin" : {
"type" : "text",
"term_vector" : "with_positions_offsets",
"analyzer" : "ik_pinyin_analyzer"
},
"suggest" : {
"type" : "completion",
"analyzer" : "ik_max_word",
"preserve_separators" : true,
"preserve_position_increments" : true,
"max_input_length" : 50
}
},
"analyzer" : "ik_max_word"
},
"appId" : {
"type" : "long"
},
"author" : {
"type" : "text",
"fields" : {
"origin" : {
"type" : "keyword"
},
"pinyin" : {
"type" : "text",
"term_vector" : "with_positions_offsets",
"analyzer" : "ik_pinyin_analyzer"
},
"suggest" : {
"type" : "completion",
"analyzer" : "ik_max_word",
"preserve_separators" : true,
"preserve_position_increments" : true,
"max_input_length" : 50
}
},
"analyzer" : "ik_smart"
},
"createTime" : {
"type" : "date"
},
"enabled" : {
"type" : "integer"
},
"favoriteTotal" : {
"type" : "long"
},
"fingerprint" : {
"type" : "keyword"
},
"id" : {
"type" : "long"
},
"images" : {
"type" : "keyword"
},
"introduce" : {
"type" : "text",
"fields" : {
"pinyin" : {
"type" : "text",
"boost" : 10.0,
"term_vector" : "with_positions_offsets",
"analyzer" : "ik_pinyin_analyzer"
}
},
"analyzer" : "ik_smart"
},
"issueTime" : {
"type" : "long"
},
"opusName" : {
"type" : "text",
"fields" : {
"origin" : {
"type" : "keyword"
},
"pinyin" : {
"type" : "text",
"term_vector" : "with_positions_offsets",
"analyzer" : "ik_pinyin_analyzer"
},
"suggest" : {
"type" : "completion",
"analyzer" : "ik_max_word",
"preserve_separators" : true,
"preserve_position_increments" : true,
"max_input_length" : 50
}
},
"analyzer" : "ik_max_word"
},
"score" : {
"type" : "double"
},
"sort" : {
"type" : "long"
},
"tags" : {
"type" : "text",
"fields" : {
"origin" : {
"type" : "keyword"
},
"pinyin" : {
"type" : "text",
"term_vector" : "with_positions_offsets",
"analyzer" : "ik_pinyin_analyzer"
},
"suggest" : {
"type" : "completion",
"analyzer" : "ik_max_word",
"preserve_separators" : true,
"preserve_position_increments" : true,
"max_input_length" : 50
}
},
"analyzer" : "ik_max_word"
},
"type" : {
"type" : "integer"
},
"updateTime" : {
"type" : "date"
},
"userId" : {
"type" : "long"
},
"uuid" : {
"type" : "keyword"
},
"viewTotal" : {
"type" : "long"
}
}
}
},
"settings" : {
"index" : {
"number_of_shards" : "1",
"provided_name" : "db_test",
"creation_date" : "1514353694378",
"analysis" : {
"filter" : {
"mix_pinyin" : {
"lowercase" : "true",
"padding_char" : " ",
"first_letter" : "prefix",
"keep_original" : "true",
"remove_duplicated_term" : "true",
"type" : "pinyin",
"keep_full_pinyin" : "true"
}
},
"analyzer" : {
"ik_pinyin_analyzer" : {
"filter" : [
"mix_pinyin",
"word_delimiter"
],
"type" : "custom",
"tokenizer" : "ik_max_word"
},
"default" : {
"tokenizer" : "ik_max_word"
}
}
},
"number_of_replicas" : "1",
"uuid" : "seg7yr55R121l8tRW3r7uA",
"version" : {
"created" : "6000099"
}
}
}
}
}

统计应用

分类统计查询

查询关键词为的文档, 并按照type分组(group by)

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
[root@localhost elasticsearch-6.0.0 ]$  curl -XPOST "http://127.0.0.1:9200/db_test/tb_opus/_search?pretty=true" -H 'Content-Type: application/json' -d'
{
"query": {
"match": {
"introduce": "青春"
}
},
"size": 0,
"aggs": {
"type_count": {
"terms": {
"field": "type"
}
}
}
}'

{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 84,
"max_score" : 0.0,
"hits" : [ ]
},
"aggregations" : {
"type_count" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [
{
"key" : 30,
"doc_count" : 76
},
{
"key" : 10,
"doc_count" : 4
},
{
"key" : 20,
"doc_count" : 4
}
]
}
}
}

基础函数统计查询

查询评分最高评分和最低评分的文档(max|min|avg|sum)

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
[root@localhost elasticsearch-6.0.0 ]$ curl -XPOST "http://127.0.0.1:9200/db_test/tb_opus/_search?pretty=true" -H 'Content-Type: application/json' -d'
{
"size": 0,
"aggs": {
"max_score": {
"max": {
"field": "score"
}
},
"min_score": {
"min": {
"field": "score"
}
}
}
}'

{
"took" : 3,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 650,
"max_score" : 0.0,
"hits" : [ ]
},
"aggregations" : {
"max_score" : {
"value" : 99.0
},
"min_score" : {
"value" : 0.0
}
}
}

统计信息查询

统计评分Top5的文档数

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
55
56
57
58
59
60
61
[root@localhost elasticsearch-6.0.0 ]$  curl -XPOST "http://127.0.0.1:9200/db_test/tb_opus/_search?pretty=true" -H 'Content-Type: application/json' -d'
{
"query": {
"match_all": {

}
},
"size": 0,
"aggs": {
"score": {
"terms": {
"field": "score",
"size": 5
}
}
}
}'

{
"took" : 3,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 651,
"max_score" : 0.0,
"hits" : [ ]
},
"aggregations" : {
"score" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 590,
"buckets" : [
{
"key" : 38.0,
"doc_count" : 13
},
{
"key" : 85.0,
"doc_count" : 13
},
{
"key" : 15.0,
"doc_count" : 12
},
{
"key" : 30.0,
"doc_count" : 11
},
{
"key" : 92.0,
"doc_count" : 11
}
]
}
}
}

按照给定维度数据进行统计

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
[root@localhost elasticsearch-6.0.0 ]$ curl -XPOST "http://127.0.0.1:9200/db_test/tb_opus/_search?pretty=true" -H 'Content-Type: application/json' -d'
{
"query": {
"terms": {
"type": ["10", "20"]
}
},
"size": 0,
"aggs": {
"significant_type": {
"significant_terms": {
"field": "type"
}
}
}
}'

{
"took" : 13,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 141,
"max_score" : 0.0,
"hits" : [ ]
},
"aggregations" : {
"significant_type" : {
"doc_count" : 141,
"bg_count" : 675,
"buckets" : [
{
"key" : 20,
"doc_count" : 91,
"score" : 2.4442432473215634,
"bg_count" : 91
},
{
"key" : 10,
"doc_count" : 50,
"score" : 1.3429907952316285,
"bg_count" : 50
}
]
}
}
}

查询评分的基本统计信息(count|min|max|avg|sum)

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
[root@localhost elasticsearch-6.0.0 ]$ curl -XPOST "http://127.0.0.1:9200/db_test/tb_opus/_search?pretty=true" -H 'Content-Type: application/json' -d'
{
"size": 0,
"aggs": {
"score_stats": {
"stats": {
"field": "score"
}
}
}
}'

{
"took" : 3,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 650,
"max_score" : 0.0,
"hits" : [ ]
},
"aggregations" : {
"score_stats" : {
"count" : 650,
"min" : 0.0,
"max" : 99.0,
"avg" : 48.643076923076926,
"sum" : 31618.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
[root@localhost elasticsearch-6.0.0 ]$ curl -XPOST "http://127.0.0.1:9200/db_test/tb_opus/_search?pretty=true" -H 'Content-Type: application/json' -d'
{
"size": 0,
"aggs": {
"score_stats": {
"extended_stats": {
"field": "score"
}
}
}
}'

{
"took" : 2,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 650,
"max_score" : 0.0,
"hits" : [ ]
},
"aggregations" : {
"score_stats" : {
"count" : 650,
"min" : 0.0,
"max" : 99.0,
"avg" : 48.643076923076926,
"sum" : 31618.0,
"sum_of_squares" : 2069492.0,
"variance" : 817.6849136094673,
"std_deviation" : 28.59519039295712,
"std_deviation_bounds" : {
"upper" : 105.83345770899118,
"lower" : -8.547303862837317
}
}
}
}

分段区间统计查询

分段统计指定区间内的评分和类型

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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
[root@localhost elasticsearch-6.0.0 ]$ curl -XPOST "http://127.0.0.1:9200/db_test/tb_opus/_search?pretty=true" -H 'Content-Type: application/json' -d'
{
"size": 0,
"aggs": {
"score_range": {
"range": {
"field": "score",
"ranges": [
{
"to": 50
},
{
"from": 50,
"to": 80
},
{
"from": 80
}
]
}
},
"type_range": {
"range": {
"field": "type",
"ranges": [
{
"to": 30
},
{
"from": 30,
"to": 50
},
{
"from": 50,
"to": 80
},
{
"from": 80
}
]
}
}
}
}'
{
"took" : 2,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 651,
"max_score" : 0.0,
"hits" : [ ]
},
"aggregations" : {
"type_range" : {
"buckets" : [
{
"key" : "*-30.0",
"to" : 30.0,
"doc_count" : 141
},
{
"key" : "30.0-50.0",
"from" : 30.0,
"to" : 50.0,
"doc_count" : 509
},
{
"key" : "50.0-80.0",
"from" : 50.0,
"to" : 80.0,
"doc_count" : 0
},
{
"key" : "80.0-*",
"from" : 80.0,
"doc_count" : 0
}
]
},
"score_range" : {
"buckets" : [
{
"key" : "*-50.0",
"to" : 50.0,
"doc_count" : 338
},
{
"key" : "50.0-80.0",
"from" : 50.0,
"to" : 80.0,
"doc_count" : 192
},
{
"key" : "80.0-*",
"from" : 80.0,
"doc_count" : 120
}
]
}
}
}

直方图统计, 按照指定维度和步长值统计

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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
[root@localhost elasticsearch-6.0.0 ]$ curl -XPOST "http://127.0.0.1:9200/db_test/tb_opus/_search?pretty=true" -H 'Content-Type: application/json' -d'
{
"query": {
"match_all": {

}
},
"size": 0,
"aggs": {
"score": {
"histogram": {
"field": "score",
"interval": 10
}
},
"type": {
"histogram": {
"field": "type",
"interval": 5
}
}
}
}'
{
"took" : 5,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 651,
"max_score" : 0.0,
"hits" : [ ]
},
"aggregations" : {
"score" : {
"buckets" : [
{
"key" : 0.0,
"doc_count" : 70
},
{
"key" : 10.0,
"doc_count" : 59
},
{
"key" : 20.0,
"doc_count" : 64
},
{
"key" : 30.0,
"doc_count" : 81
},
{
"key" : 40.0,
"doc_count" : 64
},
{
"key" : 50.0,
"doc_count" : 64
},
{
"key" : 60.0,
"doc_count" : 60
},
{
"key" : 70.0,
"doc_count" : 68
},
{
"key" : 80.0,
"doc_count" : 55
},
{
"key" : 90.0,
"doc_count" : 65
}
]
},
"type" : {
"buckets" : [
{
"key" : 10.0,
"doc_count" : 50
},
{
"key" : 15.0,
"doc_count" : 0
},
{
"key" : 20.0,
"doc_count" : 91
},
{
"key" : 25.0,
"doc_count" : 0
},
{
"key" : 30.0,
"doc_count" : 509
}
]
}
}
}

过滤统计score=90,type=20的文档

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
[root@localhost elasticsearch-6.0.0 ]$ curl -XPOST "http://127.0.0.1:9200/db_test/tb_opus/_search?pretty=true" -H 'Content-Type: application/json' -d'
{
"query": {
"match_all": {

}
},
"size": 0,
"aggs": {
"score80": {
"filter": {
"term": {"score": 80}
}
},
"type20": {
"filter": {
"term": {"type": 20}
}
}
}
}'

{
"took" : 5,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 651,
"max_score" : 0.0,
"hits" : [ ]
},
"aggregations" : {
"type20" : {
"doc_count" : 91
},
"score80" : {
"doc_count" : 3
}
}
}

矩阵统计

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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
[root@localhost elasticsearch-6.0.0 ]$ curl -XPOST "http://127.0.0.1:9200/db_test/tb_opus/_search?pretty=true" -H 'Content-Type: application/json' -d'
{
"query": {
"match_all": {

}
},
"size": 0,
"aggs": {
"matrixstats": {
"matrix_stats": {
"fields": ["score", "type"]
}
}
}
}'

{
"took" : 2,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 651,
"max_score" : 0.0,
"hits" : [ ]
},
"aggregations" : {
"matrixstats" : {
"doc_count" : 650,
"fields" : [
{
"name" : "score",
"count" : 650,
"mean" : 48.6430769230769,
"variance" : 818.9448287305912,
"skewness" : 0.06430023639098066,
"kurtosis" : 1.827011562407035,
"covariance" : {
"score" : 818.9448287305912,
"type" : -6.150574848879931
},
"correlation" : {
"score" : 1.0,
"type" : -0.03572662420282527
}
},
{
"name" : "type",
"count" : 650,
"mean" : 27.061538461538458,
"variance" : 36.19035202086057,
"skewness" : -1.894319693928367,
"kurtosis" : 5.30339872762004,
"covariance" : {
"score" : -6.150574848879931,
"type" : 36.19035202086057
},
"correlation" : {
"score" : -0.03572662420282527,
"type" : 1.0
}
}
]
}
}
}

参考文档