2013-11-24 06:13:08 -05:00
[[search-aggregations-metrics-stats-aggregation]]
2014-05-12 19:35:58 -04:00
=== Stats Aggregation
2013-11-24 06:13:08 -05:00
A `multi-value` metrics aggregation that computes stats over numeric values extracted from the aggregated documents. These values can be extracted either from specific numeric fields in the documents, or be generated by a provided script.
The stats that are returned consist of: `min`, `max`, `sum`, `count` and `avg`.
Assuming the data consists of documents representing exams grades (between 0 and 100) of students
[source,js]
--------------------------------------------------
2017-05-01 13:33:24 -04:00
POST /exams/_search?size=0
2013-11-24 06:13:08 -05:00
{
"aggs" : {
"grades_stats" : { "stats" : { "field" : "grade" } }
}
}
--------------------------------------------------
2017-05-01 13:33:24 -04:00
// CONSOLE
// TEST[setup:exams]
2013-11-24 06:13:08 -05:00
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:
[source,js]
--------------------------------------------------
{
...
"aggregations": {
"grades_stats": {
2017-05-01 13:33:24 -04:00
"count": 2,
"min": 50.0,
"max": 100.0,
"avg": 75.0,
"sum": 150.0
2013-11-24 06:13:08 -05:00
}
}
}
--------------------------------------------------
2017-05-01 13:33:24 -04:00
// TESTRESPONSE[s/\.\.\./"took": $body.took,"timed_out": false,"_shards": $body._shards,"hits": $body.hits,/]
2013-11-24 06:13:08 -05:00
2014-01-17 11:20:05 -05:00
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.
2013-11-24 06:13:08 -05:00
==== Script
Computing the grades stats based on a script:
[source,js]
--------------------------------------------------
2017-05-01 13:33:24 -04:00
POST /exams/_search?size=0
2013-11-24 06:13:08 -05:00
{
"aggs" : {
2017-05-01 13:33:24 -04:00
"grades_stats" : {
2016-06-27 09:55:16 -04:00
"stats" : {
2017-05-01 13:33:24 -04:00
"script" : {
2016-06-27 09:55:16 -04:00
"lang": "painless",
2017-06-09 11:29:25 -04:00
"source": "doc['grade'].value"
2016-06-27 09:55:16 -04:00
}
2017-05-01 13:33:24 -04:00
}
2016-06-27 09:55:16 -04:00
}
2013-11-24 06:13:08 -05:00
}
}
--------------------------------------------------
2017-05-01 13:33:24 -04:00
// CONSOLE
// TEST[setup:exams]
2013-11-24 06:13:08 -05:00
2017-05-17 17:42:25 -04:00
This will interpret the `script` parameter as an `inline` script with the `painless` script language and no script parameters. To use a stored script use the following syntax:
2015-05-12 05:37:22 -04:00
[source,js]
--------------------------------------------------
2017-05-01 13:33:24 -04:00
POST /exams/_search?size=0
2015-05-12 05:37:22 -04:00
{
"aggs" : {
"grades_stats" : {
"stats" : {
"script" : {
2017-06-09 11:29:25 -04:00
"id": "my_script",
2015-05-12 05:37:22 -04:00
"params" : {
"field" : "grade"
}
}
}
}
}
}
--------------------------------------------------
2017-05-01 13:33:24 -04:00
// CONSOLE
2017-05-17 17:42:25 -04:00
// TEST[setup:exams,stored_example_script]
2015-04-26 11:30:38 -04:00
2013-11-24 06:13:08 -05:00
===== Value Script
2014-01-17 11:20:05 -05:00
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 a value script to get the new stats:
2013-11-24 06:13:08 -05:00
[source,js]
--------------------------------------------------
2017-05-01 13:33:24 -04:00
POST /exams/_search?size=0
2013-11-24 06:13:08 -05:00
{
"aggs" : {
2017-05-01 13:33:24 -04:00
"grades_stats" : {
"stats" : {
"field" : "grade",
"script" : {
"lang": "painless",
2017-06-09 11:29:25 -04:00
"source": "_value * params.correction",
2017-05-01 13:33:24 -04:00
"params" : {
"correction" : 1.2
2013-11-24 06:13:08 -05:00
}
}
}
}
}
}
2015-05-07 10:46:40 -04:00
--------------------------------------------------
2017-05-01 13:33:24 -04:00
// CONSOLE
// TEST[setup:exams]
2015-05-07 10:46:40 -04:00
==== Missing value
The `missing` parameter defines how documents that are missing a value should be treated.
By default they will be ignored but it is also possible to treat them as if they
had a value.
[source,js]
--------------------------------------------------
2017-05-01 13:33:24 -04:00
POST /exams/_search?size=0
2015-05-07 10:46:40 -04:00
{
"aggs" : {
"grades_stats" : {
"stats" : {
"field" : "grade",
"missing": 0 <1>
}
}
}
}
--------------------------------------------------
2017-05-01 13:33:24 -04:00
// CONSOLE
// TEST[setup:exams]
2015-05-07 10:46:40 -04:00
<1> Documents without a value in the `grade` field will fall into the same bucket as documents that have the value `0`.