Docs: CONSOLEify sum aggregation docs

This adds the `COPY AS CURL` and `VIEW IN CONSOLE` buttons to the
docs and makes the build execute the snippets as part of `docs:check`.

Relates to #18160
This commit is contained in:
Nik Everett 2017-02-07 14:17:54 -05:00
parent bbf62e3472
commit 245aa0404a
3 changed files with 75 additions and 42 deletions

View File

@ -43,7 +43,6 @@ buildRestTests.expectedUnconvertedCandidates = [
'reference/aggregations/metrics/percentile-rank-aggregation.asciidoc',
'reference/aggregations/metrics/scripted-metric-aggregation.asciidoc',
'reference/aggregations/metrics/stats-aggregation.asciidoc',
'reference/aggregations/metrics/sum-aggregation.asciidoc',
'reference/aggregations/metrics/tophits-aggregation.asciidoc',
'reference/aggregations/pipeline.asciidoc',
'reference/aggregations/pipeline/avg-bucket-aggregation.asciidoc',
@ -284,7 +283,7 @@ buildRestTests.setups['ledger'] = '''
{"index":{}}
{"date": "2015/01/01 00:00:00", "amount": 50, "type": "expense", "description": "advertisement"}'''
// Used by pipeline aggregation docs
// Used by aggregation docs
buildRestTests.setups['sales'] = '''
- do:
indices.create:

View File

@ -3,7 +3,8 @@
A `single-value` metrics aggregation that computes the average of numeric values that are 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.
Assuming the data consists of documents representing exams grades (between 0 and 100) of students
Assuming the data consists of documents representing exams grades (between 0
and 100) of students we can average their scores with:
[source,js]
--------------------------------------------------

View File

@ -3,78 +3,94 @@
A `single-value` metrics aggregation that sums up numeric values that are 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.
Assuming the data consists of documents representing stock ticks, where each tick holds the change in the stock price from the previous tick.
Assuming the data consists of documents representing sales records we can sum
the sale price of all hats with:
[source,js]
--------------------------------------------------
POST /sales/_search?size=0
{
"query" : {
"constant_score" : {
"filter" : {
"range" : { "timestamp" : { "from" : "now/1d+9.5h", "to" : "now/1d+16h" }}
"match" : { "type" : "hat" }
}
}
},
"aggs" : {
"intraday_return" : { "sum" : { "field" : "change" } }
"hat_prices" : { "sum" : { "field" : "price" } }
}
}
--------------------------------------------------
// CONSOLE
// TEST[setup:sales]
The above aggregation sums up all changes in the today's trading stock ticks which accounts for the intraday return. The aggregation type is `sum` and the `field` setting defines the numeric field of the documents of which values will be summed up. The above will return the following:
Resulting in:
[source,js]
--------------------------------------------------
{
...
"aggregations": {
"intraday_return": {
"value": 2.18
"hat_prices": {
"value": 450.0
}
}
}
--------------------------------------------------
// TESTRESPONSE[s/\.\.\./"took": $body.took,"timed_out": false,"_shards": $body._shards,"hits": $body.hits,/]
The name of the aggregation (`intraday_return` above) also serves as the key by which the aggregation result can be retrieved from the returned response.
==== Script
Computing the intraday return based on a script:
We could also use a script to fetch the sales price:
[source,js]
--------------------------------------------------
POST /sales/_search?size=0
{
...,
"query" : {
"constant_score" : {
"filter" : {
"match" : { "type" : "hat" }
}
}
},
"aggs" : {
"intraday_return" : {
"sum" : {
"hat_prices" : {
"sum" : {
"script" : {
"lang": "painless",
"inline": "doc['change'].value"
}
"inline": "doc.price.value"
}
}
}
}
}
--------------------------------------------------
// CONSOLE
// TEST[setup:sales]
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 /sales/_search?size=0
{
...,
"query" : {
"constant_score" : {
"filter" : {
"match" : { "type" : "hat" }
}
}
},
"aggs" : {
"intraday_return" : {
"sum" : {
"hat_prices" : {
"sum" : {
"script" : {
"file": "my_script",
"params" : {
"field" : "change"
"field" : "price"
}
}
}
@ -82,52 +98,69 @@ This will interpret the `script` parameter as an `inline` script with the `painl
}
}
--------------------------------------------------
// CONSOLE
// TEST[setup:sales]
TIP: for indexed scripts replace the `file` parameter with an `id` parameter.
===== Value Script
Computing the sum of squares over all stock tick changes:
It is also possible to access the field value from the script using `_value`.
For example, this will sum the square of the prices for all hats:
[source,js]
--------------------------------------------------
POST /sales/_search?size=0
{
"query" : {
"constant_score" : {
"filter" : {
"match" : { "type" : "hat" }
}
}
},
"aggs" : {
...
"aggs" : {
"daytime_return" : {
"sum" : {
"field" : "change",
"script" : {
"lang": "painless",
"inline": "_value * _value"
}
"square_hats" : {
"sum" : {
"field" : "price",
"script" : {
"inline": "_value * _value"
}
}
}
}
}
--------------------------------------------------
// CONSOLE
// TEST[setup:sales]
==== 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.
The `missing` parameter defines how documents that are missing a value should
be treated. By default documents missing the value will be ignored but it is
also possible to treat them as if they had a value. For example, this treats
all hat sales without a price as being `100`.
[source,js]
--------------------------------------------------
POST /sales/_search?size=0
{
"query" : {
"constant_score" : {
"filter" : {
"match" : { "type" : "hat" }
}
}
},
"aggs" : {
"total_time" : {
"hat_prices" : {
"sum" : {
"field" : "took",
"field" : "price",
"missing": 100 <1>
}
}
}
}
--------------------------------------------------
<1> Documents without a value in the `took` field will fall into the same bucket as documents that have the value `100`.
// CONSOLE
// TEST[setup:sales]