Skip to content

Commit

Permalink
CONSOLEify extended_stats docs
Browse files Browse the repository at this point in the history
Related #18160
  • Loading branch information
polyfractal committed Aug 2, 2017
1 parent aafd7f9 commit 268923e
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 28 deletions.
1 change: 0 additions & 1 deletion docs/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ buildRestTests.expectedUnconvertedCandidates = [
'reference/aggregations/bucket/significantterms-aggregation.asciidoc',
'reference/aggregations/bucket/terms-aggregation.asciidoc',
'reference/aggregations/matrix/stats-aggregation.asciidoc',
'reference/aggregations/metrics/extendedstats-aggregation.asciidoc',
'reference/aggregations/metrics/percentile-aggregation.asciidoc',
'reference/aggregations/metrics/percentile-rank-aggregation.asciidoc',
'reference/aggregations/metrics/scripted-metric-aggregation.asciidoc',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,16 @@ Assuming the data consists of documents representing exams grades (between 0 and

[source,js]
--------------------------------------------------
GET /exams/_search
{
"size": 0,
"aggs" : {
"grades_stats" : { "extended_stats" : { "field" : "grade" } }
}
}
--------------------------------------------------
// CONSOLE
// TEST[setup:exams]

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:

Expand All @@ -25,23 +29,24 @@ The above aggregation computes the grades statistics over all documents. The agg
...
"aggregations": {
"grade_stats": {
"count": 9,
"min": 72,
"max": 99,
"avg": 86,
"sum": 774,
"sum_of_squares": 67028,
"variance": 51.55555555555556,
"std_deviation": 7.180219742846005,
"grades_stats": {
"count": 2,
"min": 50.0,
"max": 100.0,
"avg": 75.0,
"sum": 150.0,
"sum_of_squares": 12500.0,
"variance": 625.0,
"std_deviation": 25.0,
"std_deviation_bounds": {
"upper": 100.36043948569201,
"lower": 71.63956051430799
"upper": 125.0,
"lower": 25.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.

Expand All @@ -52,7 +57,9 @@ three standard deviations, you can set `sigma` in the request:

[source,js]
--------------------------------------------------
GET /exams/_search
{
"size": 0,
"aggs" : {
"grades_stats" : {
"extended_stats" : {
Expand All @@ -63,6 +70,8 @@ three standard deviations, you can set `sigma` in the request:
}
}
--------------------------------------------------
// CONSOLE
// TEST[setup:exams]
<1> `sigma` controls how many standard deviations +/- from the mean should be displayed

`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
Expand All @@ -82,9 +91,9 @@ Computing the grades stats based on a script:

[source,js]
--------------------------------------------------
GET /exams/_search
{
...,
"size": 0,
"aggs" : {
"grades_stats" : {
"extended_stats" : {
Expand All @@ -97,14 +106,16 @@ Computing the grades stats based on a script:
}
}
--------------------------------------------------
// 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]
--------------------------------------------------
GET /exams/_search
{
...,
"size": 0,
"aggs" : {
"grades_stats" : {
"extended_stats" : {
Expand All @@ -119,34 +130,36 @@ This will interpret the `script` parameter as an `inline` script with the `painl
}
}
--------------------------------------------------
// CONSOLE
// TEST[setup:exams,stored_example_script]

===== Value Script

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:

[source,js]
--------------------------------------------------
GET /exams/_search
{
"size": 0,
"aggs" : {
...
"aggs" : {
"grades_stats" : {
"extended_stats" : {
"field" : "grade",
"script" : {
"lang" : "painless",
"source": "_value * params.correction",
"params" : {
"correction" : 1.2
}
"grades_stats" : {
"extended_stats" : {
"field" : "grade",
"script" : {
"lang" : "painless",
"source": "_value * params.correction",
"params" : {
"correction" : 1.2
}
}
}
}
}
}
--------------------------------------------------
// CONSOLE
// TEST[setup:exams]

==== Missing value

Expand All @@ -156,7 +169,9 @@ had a value.

[source,js]
--------------------------------------------------
GET /exams/_search
{
"size": 0,
"aggs" : {
"grades_stats" : {
"extended_stats" : {
Expand All @@ -167,5 +182,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`.

0 comments on commit 268923e

Please sign in to comment.