|
@@ -9,12 +9,15 @@ Assuming the data consists of documents representing exams grades (between 0 and
|
|
|
|
|
|
[source,js]
|
|
|
--------------------------------------------------
|
|
|
+POST /exams/_search?size=0
|
|
|
{
|
|
|
"aggs" : {
|
|
|
"grades_stats" : { "stats" : { "field" : "grade" } }
|
|
|
}
|
|
|
}
|
|
|
--------------------------------------------------
|
|
|
+// CONSOLE
|
|
|
+// TEST[setup:exams]
|
|
|
|
|
|
The above aggregation computes the grades statistics over all documents. The aggregation type is `stats` and the `field` setting defines the numeric field of the documents the stats will be computed on. The above will return the following:
|
|
|
|
|
@@ -26,15 +29,16 @@ The above aggregation computes the grades statistics over all documents. The agg
|
|
|
|
|
|
"aggregations": {
|
|
|
"grades_stats": {
|
|
|
- "count": 6,
|
|
|
- "min": 60,
|
|
|
- "max": 98,
|
|
|
- "avg": 78.5,
|
|
|
- "sum": 471
|
|
|
+ "count": 2,
|
|
|
+ "min": 50.0,
|
|
|
+ "max": 100.0,
|
|
|
+ "avg": 75.0,
|
|
|
+ "sum": 150.0
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
--------------------------------------------------
|
|
|
+// TESTRESPONSE[s/\.\.\./"took": $body.took,"timed_out": false,"_shards": $body._shards,"hits": $body.hits,/]
|
|
|
|
|
|
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.
|
|
|
|
|
@@ -44,29 +48,29 @@ Computing the grades stats based on a script:
|
|
|
|
|
|
[source,js]
|
|
|
--------------------------------------------------
|
|
|
+POST /exams/_search?size=0
|
|
|
{
|
|
|
- ...,
|
|
|
-
|
|
|
"aggs" : {
|
|
|
- "grades_stats" : {
|
|
|
+ "grades_stats" : {
|
|
|
"stats" : {
|
|
|
- "script" : {
|
|
|
+ "script" : {
|
|
|
"lang": "painless",
|
|
|
- "inline": "doc['grade'].value"
|
|
|
+ "inline": "doc['grade'].value"
|
|
|
}
|
|
|
- }
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
--------------------------------------------------
|
|
|
+// CONSOLE
|
|
|
+// TEST[setup:exams]
|
|
|
|
|
|
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:
|
|
|
|
|
|
[source,js]
|
|
|
--------------------------------------------------
|
|
|
+POST /exams/_search?size=0
|
|
|
{
|
|
|
- ...,
|
|
|
-
|
|
|
"aggs" : {
|
|
|
"grades_stats" : {
|
|
|
"stats" : {
|
|
@@ -81,6 +85,8 @@ This will interpret the `script` parameter as an `inline` script with the `painl
|
|
|
}
|
|
|
}
|
|
|
--------------------------------------------------
|
|
|
+// CONSOLE
|
|
|
+// TEST[setup:exams]
|
|
|
|
|
|
TIP: for indexed scripts replace the `file` parameter with an `id` parameter.
|
|
|
|
|
@@ -90,20 +96,17 @@ It turned out that the exam was way above the level of the students and a grade
|
|
|
|
|
|
[source,js]
|
|
|
--------------------------------------------------
|
|
|
+POST /exams/_search?size=0
|
|
|
{
|
|
|
"aggs" : {
|
|
|
- ...
|
|
|
-
|
|
|
- "aggs" : {
|
|
|
- "grades_stats" : {
|
|
|
- "stats" : {
|
|
|
- "field" : "grade",
|
|
|
- "script" :
|
|
|
- "lang": "painless",
|
|
|
- "inline": "_value * params.correction",
|
|
|
- "params" : {
|
|
|
- "correction" : 1.2
|
|
|
- }
|
|
|
+ "grades_stats" : {
|
|
|
+ "stats" : {
|
|
|
+ "field" : "grade",
|
|
|
+ "script" : {
|
|
|
+ "lang": "painless",
|
|
|
+ "inline": "_value * params.correction",
|
|
|
+ "params" : {
|
|
|
+ "correction" : 1.2
|
|
|
}
|
|
|
}
|
|
|
}
|
|
@@ -111,6 +114,8 @@ It turned out that the exam was way above the level of the students and a grade
|
|
|
}
|
|
|
}
|
|
|
--------------------------------------------------
|
|
|
+// CONSOLE
|
|
|
+// TEST[setup:exams]
|
|
|
|
|
|
==== Missing value
|
|
|
|
|
@@ -120,6 +125,7 @@ had a value.
|
|
|
|
|
|
[source,js]
|
|
|
--------------------------------------------------
|
|
|
+POST /exams/_search?size=0
|
|
|
{
|
|
|
"aggs" : {
|
|
|
"grades_stats" : {
|
|
@@ -131,5 +137,7 @@ had a value.
|
|
|
}
|
|
|
}
|
|
|
--------------------------------------------------
|
|
|
+// CONSOLE
|
|
|
+// TEST[setup:exams]
|
|
|
|
|
|
<1> Documents without a value in the `grade` field will fall into the same bucket as documents that have the value `0`.
|