@@ -9,12 +9,16 @@ Assuming the data consists of documents representing exams grades (between 0 and
9
9
10
10
[source,js]
11
11
--------------------------------------------------
12
+ GET /exams/_search
12
13
{
14
+ "size": 0,
13
15
"aggs" : {
14
16
"grades_stats" : { "extended_stats" : { "field" : "grade" } }
15
17
}
16
18
}
17
19
--------------------------------------------------
20
+ // CONSOLE
21
+ // TEST[setup:exams]
18
22
19
23
The above aggregation computes the grades statistics over all documents. The aggregation type is `extended_stats` and the `field` setting defines the numeric field of the documents the stats will be computed on. The above will return the following:
20
24
@@ -25,23 +29,24 @@ The above aggregation computes the grades statistics over all documents. The agg
25
29
...
26
30
27
31
"aggregations": {
28
- "grade_stats ": {
29
- "count": 9 ,
30
- "min": 72 ,
31
- "max": 99 ,
32
- "avg": 86 ,
33
- "sum": 774 ,
34
- "sum_of_squares": 67028 ,
35
- "variance": 51.55555555555556 ,
36
- "std_deviation": 7.180219742846005 ,
32
+ "grades_stats ": {
33
+ "count": 2 ,
34
+ "min": 50.0 ,
35
+ "max": 100.0 ,
36
+ "avg": 75.0 ,
37
+ "sum": 150.0 ,
38
+ "sum_of_squares": 12500.0 ,
39
+ "variance": 625.0 ,
40
+ "std_deviation": 25.0 ,
37
41
"std_deviation_bounds": {
38
- "upper": 100.36043948569201 ,
39
- "lower": 71.63956051430799
42
+ "upper": 125.0 ,
43
+ "lower": 25.0
40
44
}
41
45
}
42
46
}
43
47
}
44
48
--------------------------------------------------
49
+ // TESTRESPONSE[s/\.\.\./"took": $body.took,"timed_out": false,"_shards": $body._shards,"hits": $body.hits,/]
45
50
46
51
The name of the aggregation (`grades_stats` above) also serves as the key by which the aggregation result can be retrieved from the returned response.
47
52
@@ -52,7 +57,9 @@ three standard deviations, you can set `sigma` in the request:
52
57
53
58
[source,js]
54
59
--------------------------------------------------
60
+ GET /exams/_search
55
61
{
62
+ "size": 0,
56
63
"aggs" : {
57
64
"grades_stats" : {
58
65
"extended_stats" : {
@@ -63,6 +70,8 @@ three standard deviations, you can set `sigma` in the request:
63
70
}
64
71
}
65
72
--------------------------------------------------
73
+ // CONSOLE
74
+ // TEST[setup:exams]
66
75
<1> `sigma` controls how many standard deviations +/- from the mean should be displayed
67
76
68
77
`sigma` can be any non-negative double, meaning you can request non-integer values such as `1.5`. A value of `0` is valid, but will simply
@@ -82,9 +91,9 @@ Computing the grades stats based on a script:
82
91
83
92
[source,js]
84
93
--------------------------------------------------
94
+ GET /exams/_search
85
95
{
86
- ...,
87
-
96
+ "size": 0,
88
97
"aggs" : {
89
98
"grades_stats" : {
90
99
"extended_stats" : {
@@ -97,14 +106,16 @@ Computing the grades stats based on a script:
97
106
}
98
107
}
99
108
--------------------------------------------------
109
+ // CONSOLE
110
+ // TEST[setup:exams]
100
111
101
112
This will interpret the `script` parameter as an `inline` script with the `painless` script language and no script parameters. To use a file script use the following syntax:
102
113
103
114
[source,js]
104
115
--------------------------------------------------
116
+ GET /exams/_search
105
117
{
106
- ...,
107
-
118
+ "size": 0,
108
119
"aggs" : {
109
120
"grades_stats" : {
110
121
"extended_stats" : {
@@ -119,34 +130,36 @@ This will interpret the `script` parameter as an `inline` script with the `painl
119
130
}
120
131
}
121
132
--------------------------------------------------
133
+ // CONSOLE
134
+ // TEST[setup:exams,stored_example_script]
122
135
123
136
===== Value Script
124
137
125
138
It turned out that the exam was way above the level of the students and a grade correction needs to be applied. We can use value script to get the new stats:
126
139
127
140
[source,js]
128
141
--------------------------------------------------
142
+ GET /exams/_search
129
143
{
144
+ "size": 0,
130
145
"aggs" : {
131
- ...
132
-
133
- "aggs" : {
134
- "grades_stats" : {
135
- "extended_stats" : {
136
- "field" : "grade",
137
- "script" : {
138
- "lang" : "painless",
139
- "source": "_value * params.correction",
140
- "params" : {
141
- "correction" : 1.2
142
- }
146
+ "grades_stats" : {
147
+ "extended_stats" : {
148
+ "field" : "grade",
149
+ "script" : {
150
+ "lang" : "painless",
151
+ "source": "_value * params.correction",
152
+ "params" : {
153
+ "correction" : 1.2
143
154
}
144
155
}
145
156
}
146
157
}
147
158
}
148
159
}
149
160
--------------------------------------------------
161
+ // CONSOLE
162
+ // TEST[setup:exams]
150
163
151
164
==== Missing value
152
165
@@ -156,7 +169,9 @@ had a value.
156
169
157
170
[source,js]
158
171
--------------------------------------------------
172
+ GET /exams/_search
159
173
{
174
+ "size": 0,
160
175
"aggs" : {
161
176
"grades_stats" : {
162
177
"extended_stats" : {
@@ -167,5 +182,7 @@ had a value.
167
182
}
168
183
}
169
184
--------------------------------------------------
185
+ // CONSOLE
186
+ // TEST[setup:exams]
170
187
171
188
<1> Documents without a value in the `grade` field will fall into the same bucket as documents that have the value `0`.
0 commit comments