Docs: CONSOLEify the avg aggregation docs
This creates the `COPY AS CURL` and `VIEW IN CONSOLE` buttons and makes the build test the examples. Relates to #18160
This commit is contained in:
parent
ee84ce09d7
commit
274ee30d34
|
@ -35,7 +35,6 @@ buildRestTests.expectedUnconvertedCandidates = [
|
|||
'reference/aggregations/bucket/significantterms-aggregation.asciidoc',
|
||||
'reference/aggregations/bucket/terms-aggregation.asciidoc',
|
||||
'reference/aggregations/matrix/stats-aggregation.asciidoc',
|
||||
'reference/aggregations/metrics/avg-aggregation.asciidoc',
|
||||
'reference/aggregations/metrics/cardinality-aggregation.asciidoc',
|
||||
'reference/aggregations/metrics/extendedstats-aggregation.asciidoc',
|
||||
'reference/aggregations/metrics/geobounds-aggregation.asciidoc',
|
||||
|
@ -405,7 +404,7 @@ buildRestTests.setups['stackoverflow'] = '''
|
|||
type: question
|
||||
refresh: true
|
||||
body: |'''
|
||||
|
||||
|
||||
// Make Kibana strongly connected to elasticsearch and logstash
|
||||
// Make Kibana rarer (and therefore higher-ranking) than Javascript
|
||||
// Make Javascript strongly connected to jquery and angular
|
||||
|
@ -438,4 +437,27 @@ for (int i = 0; i < 50; i++) {
|
|||
}
|
||||
buildRestTests.setups['stackoverflow'] += """
|
||||
"""
|
||||
|
||||
// Used by some aggregations
|
||||
buildRestTests.setups['exams'] = '''
|
||||
- do:
|
||||
indices.create:
|
||||
index: exams
|
||||
body:
|
||||
settings:
|
||||
number_of_shards: 1
|
||||
number_of_replicas: 1
|
||||
mappings:
|
||||
exam:
|
||||
properties:
|
||||
grade:
|
||||
type: byte
|
||||
- do:
|
||||
bulk:
|
||||
index: exams
|
||||
type: exam
|
||||
refresh: true
|
||||
body: |
|
||||
{"index":{}}
|
||||
{"grade": 100}
|
||||
{"index":{}}
|
||||
{"grade": 50}'''
|
||||
|
|
|
@ -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`.
|
||||
|
|
Loading…
Reference in New Issue