|
@@ -7,28 +7,30 @@ Assuming the data consists of documents representing exams grades (between 0 and
|
|
|
|
|
|
[source,js]
|
|
|
--------------------------------------------------
|
|
|
+POST /exams/_search?size=0
|
|
|
{
|
|
|
"aggs" : {
|
|
|
"avg_grade" : { "avg" : { "field" : "grade" } }
|
|
|
}
|
|
|
}
|
|
|
--------------------------------------------------
|
|
|
+// CONSOLE
|
|
|
+// TEST[setup:exams]
|
|
|
|
|
|
The above aggregation computes the average grade over all documents. The aggregation type is `avg` and the `field` setting defines the numeric field of the documents the average will be computed on. The above will return the following:
|
|
|
|
|
|
-
|
|
|
[source,js]
|
|
|
--------------------------------------------------
|
|
|
{
|
|
|
...
|
|
|
-
|
|
|
"aggregations": {
|
|
|
"avg_grade": {
|
|
|
- "value": 75
|
|
|
+ "value": 75.0
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
--------------------------------------------------
|
|
|
+// TESTRESPONSE[s/\.\.\./"took": $body.took,"timed_out": false,"_shards": $body._shards,"hits": $body.hits,/]
|
|
|
|
|
|
The name of the aggregation (`avg_grade` above) also serves as the key by which the aggregation result can be retrieved from the returned response.
|
|
|
|
|
@@ -38,32 +40,31 @@ Computing the average grade based on a script:
|
|
|
|
|
|
[source,js]
|
|
|
--------------------------------------------------
|
|
|
+POST /exams/_search?size=0
|
|
|
{
|
|
|
- ...,
|
|
|
-
|
|
|
"aggs" : {
|
|
|
- "avg_grade" : {
|
|
|
- "avg" : {
|
|
|
+ "avg_grade" : {
|
|
|
+ "avg" : {
|
|
|
"script" : {
|
|
|
- "inline" : "doc['grade'].value",
|
|
|
- "lang" : "painless"
|
|
|
+ "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" : {
|
|
|
- "avg_grade" : {
|
|
|
- "avg" : {
|
|
|
+ "avg_grade" : {
|
|
|
+ "avg" : {
|
|
|
"script" : {
|
|
|
"file": "my_script",
|
|
|
"params": {
|
|
@@ -75,6 +76,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.
|
|
|
|
|
@@ -84,20 +87,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" : {
|
|
|
- "avg_corrected_grade" : {
|
|
|
- "avg" : {
|
|
|
- "field" : "grade",
|
|
|
- "script" : {
|
|
|
- "lang": "painless",
|
|
|
- "inline": "_value * params.correction",
|
|
|
- "params" : {
|
|
|
- "correction" : 1.2
|
|
|
- }
|
|
|
+ "avg_corrected_grade" : {
|
|
|
+ "avg" : {
|
|
|
+ "field" : "grade",
|
|
|
+ "script" : {
|
|
|
+ "lang": "painless",
|
|
|
+ "inline": "_value * params.correction",
|
|
|
+ "params" : {
|
|
|
+ "correction" : 1.2
|
|
|
}
|
|
|
}
|
|
|
}
|
|
@@ -105,6 +105,8 @@ It turned out that the exam was way above the level of the students and a grade
|
|
|
}
|
|
|
}
|
|
|
--------------------------------------------------
|
|
|
+// CONSOLE
|
|
|
+// TEST[setup:exams]
|
|
|
|
|
|
==== Missing value
|
|
|
|
|
@@ -114,6 +116,7 @@ had a value.
|
|
|
|
|
|
[source,js]
|
|
|
--------------------------------------------------
|
|
|
+POST /exams/_search?size=0
|
|
|
{
|
|
|
"aggs" : {
|
|
|
"grade_avg" : {
|
|
@@ -125,5 +128,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 `10`.
|