mirror of
https://github.com/honeymoose/OpenSearch.git
synced 2025-02-22 21:05:23 +00:00
CONSOLE-ify min and max aggregation docs
Adds the `VIEW IN CONSOLE` and `COPY AS CURL` links to the docs and makes the build automatically test them. Relates to #18160
This commit is contained in:
parent
8c856eaa9f
commit
c2a580304b
@ -45,8 +45,6 @@ buildRestTests.expectedUnconvertedCandidates = [
|
||||
'reference/aggregations/metrics/extendedstats-aggregation.asciidoc',
|
||||
'reference/aggregations/metrics/geobounds-aggregation.asciidoc',
|
||||
'reference/aggregations/metrics/geocentroid-aggregation.asciidoc',
|
||||
'reference/aggregations/metrics/max-aggregation.asciidoc',
|
||||
'reference/aggregations/metrics/min-aggregation.asciidoc',
|
||||
'reference/aggregations/metrics/percentile-aggregation.asciidoc',
|
||||
'reference/aggregations/metrics/percentile-rank-aggregation.asciidoc',
|
||||
'reference/aggregations/metrics/scripted-metric-aggregation.asciidoc',
|
||||
@ -169,6 +167,7 @@ integTest {
|
||||
Closure configFile = {
|
||||
extraConfigFile it, "src/test/cluster/config/$it"
|
||||
}
|
||||
configFile 'scripts/my_script.painless'
|
||||
configFile 'scripts/my_init_script.painless'
|
||||
configFile 'scripts/my_map_script.painless'
|
||||
configFile 'scripts/my_combine_script.painless'
|
||||
|
@ -1,7 +1,10 @@
|
||||
[[search-aggregations-metrics-max-aggregation]]
|
||||
=== Max Aggregation
|
||||
|
||||
A `single-value` metrics aggregation that keeps track and returns the maximum value among the 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.
|
||||
A `single-value` metrics aggregation that keeps track and returns the maximum
|
||||
value among the 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.
|
||||
|
||||
NOTE: The `min` and `max` aggregation operate on the `double` representation of
|
||||
the data. As a consequence, the result may be approximate when running on longs
|
||||
@ -11,12 +14,15 @@ Computing the max price value across all documents
|
||||
|
||||
[source,js]
|
||||
--------------------------------------------------
|
||||
POST /sales/_search?size=0
|
||||
{
|
||||
"aggs" : {
|
||||
"max_price" : { "max" : { "field" : "price" } }
|
||||
}
|
||||
}
|
||||
--------------------------------------------------
|
||||
// CONSOLE
|
||||
// TEST[setup:sales]
|
||||
|
||||
Response:
|
||||
|
||||
@ -24,45 +30,52 @@ Response:
|
||||
--------------------------------------------------
|
||||
{
|
||||
...
|
||||
|
||||
"aggregations": {
|
||||
"max_price": {
|
||||
"value": 35
|
||||
"value": 200.0
|
||||
}
|
||||
}
|
||||
}
|
||||
--------------------------------------------------
|
||||
// TESTRESPONSE[s/\.\.\./"took": $body.took,"timed_out": false,"_shards": $body._shards,"hits": $body.hits,/]
|
||||
|
||||
As can be seen, the name of the aggregation (`max_price` above) also serves as the key by which the aggregation result can be retrieved from the returned response.
|
||||
As can be seen, the name of the aggregation (`max_price` above) also serves as
|
||||
the key by which the aggregation result can be retrieved from the returned
|
||||
response.
|
||||
|
||||
==== Script
|
||||
|
||||
Computing the max price value across all document, this time using a script:
|
||||
The `max` aggregation can also calculate the maximum of a script. The example
|
||||
below computes the maximum price:
|
||||
|
||||
[source,js]
|
||||
--------------------------------------------------
|
||||
POST /sales/_search
|
||||
{
|
||||
"aggs" : {
|
||||
"max_price" : {
|
||||
"max" : {
|
||||
"max_price" : {
|
||||
"max" : {
|
||||
"script" : {
|
||||
"inline" : "doc['price'].value",
|
||||
"lang" : "painless"
|
||||
"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:
|
||||
This will use the <<modules-scripting-painless, Painless>> scripting language
|
||||
and no script parameters. To use a file script use the following syntax:
|
||||
|
||||
[source,js]
|
||||
--------------------------------------------------
|
||||
POST /sales/_search
|
||||
{
|
||||
"aggs" : {
|
||||
"max_price" : {
|
||||
"max" : {
|
||||
"max_price" : {
|
||||
"max" : {
|
||||
"script" : {
|
||||
"file": "my_script",
|
||||
"params": {
|
||||
@ -74,23 +87,28 @@ 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.
|
||||
TIP: For indexed scripts replace the `file` parameter with an `id` parameter.
|
||||
|
||||
==== Value Script
|
||||
|
||||
Let's say that the prices of the documents in our index are in USD, but we would like to compute the max in EURO (and for the sake of this example, lets say the conversion rate is 1.2). We can use a value script to apply the conversion rate to every value before it is aggregated:
|
||||
Let's say that the prices of the documents in our index are in USD, but we
|
||||
would like to compute the max in EURO (and for the sake of this example, let's
|
||||
say the conversion rate is 1.2). We can use a value script to apply the
|
||||
conversion rate to every value before it is aggregated:
|
||||
|
||||
[source,js]
|
||||
--------------------------------------------------
|
||||
POST /sales/_search
|
||||
{
|
||||
"aggs" : {
|
||||
"max_price_in_euros" : {
|
||||
"max" : {
|
||||
"field" : "price",
|
||||
"script" : {
|
||||
"lang": "painless",
|
||||
"inline": "_value * params.conversion_rate",
|
||||
"inline" : "_value * params.conversion_rate",
|
||||
"params" : {
|
||||
"conversion_rate" : 1.2
|
||||
}
|
||||
@ -100,15 +118,18 @@ Let's say that the prices of the documents in our index are in USD, but we would
|
||||
}
|
||||
}
|
||||
--------------------------------------------------
|
||||
// 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 they will be ignored but it is also possible to treat
|
||||
them as if they had a value.
|
||||
|
||||
[source,js]
|
||||
--------------------------------------------------
|
||||
POST /sales/_search
|
||||
{
|
||||
"aggs" : {
|
||||
"grade_max" : {
|
||||
@ -120,5 +141,8 @@ had a value.
|
||||
}
|
||||
}
|
||||
--------------------------------------------------
|
||||
// CONSOLE
|
||||
// TEST[setup:sales]
|
||||
|
||||
<1> Documents without a value in the `grade` field will fall into the same bucket as documents that have the value `10`.
|
||||
<1> Documents without a value in the `grade` field will fall into the same
|
||||
bucket as documents that have the value `10`.
|
||||
|
@ -1,7 +1,10 @@
|
||||
[[search-aggregations-metrics-min-aggregation]]
|
||||
=== Min Aggregation
|
||||
|
||||
A `single-value` metrics aggregation that keeps track and returns the minimum value among 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.
|
||||
A `single-value` metrics aggregation that keeps track and returns the minimum
|
||||
value among 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.
|
||||
|
||||
NOTE: The `min` and `max` aggregation operate on the `double` representation of
|
||||
the data. As a consequence, the result may be approximate when running on longs
|
||||
@ -11,12 +14,15 @@ Computing the min price value across all documents:
|
||||
|
||||
[source,js]
|
||||
--------------------------------------------------
|
||||
POST /sales/_search?size=0
|
||||
{
|
||||
"aggs" : {
|
||||
"min_price" : { "min" : { "field" : "price" } }
|
||||
}
|
||||
}
|
||||
--------------------------------------------------
|
||||
// CONSOLE
|
||||
// TEST[setup:sales]
|
||||
|
||||
Response:
|
||||
|
||||
@ -27,42 +33,50 @@ Response:
|
||||
|
||||
"aggregations": {
|
||||
"min_price": {
|
||||
"value": 10
|
||||
"value": 10.0
|
||||
}
|
||||
}
|
||||
}
|
||||
--------------------------------------------------
|
||||
// TESTRESPONSE[s/\.\.\./"took": $body.took,"timed_out": false,"_shards": $body._shards,"hits": $body.hits,/]
|
||||
|
||||
As can be seen, the name of the aggregation (`min_price` above) also serves as the key by which the aggregation result can be retrieved from the returned response.
|
||||
As can be seen, the name of the aggregation (`min_price` above) also serves as
|
||||
the key by which the aggregation result can be retrieved from the returned
|
||||
response.
|
||||
|
||||
==== Script
|
||||
|
||||
Computing the min price value across all document, this time using a script:
|
||||
The `min` aggregation can also calculate the maximum of a script. The example
|
||||
below computes the minimum price:
|
||||
|
||||
[source,js]
|
||||
--------------------------------------------------
|
||||
POST /sales/_search
|
||||
{
|
||||
"aggs" : {
|
||||
"min_price" : {
|
||||
"min" : {
|
||||
"min_price" : {
|
||||
"min" : {
|
||||
"script" : {
|
||||
"inline" : "doc['price'].value",
|
||||
"lang" : "painless"
|
||||
"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:
|
||||
This will use the <<modules-scripting-painless, Painless>> scripting language
|
||||
and no script parameters. To use a file script use the following syntax:
|
||||
|
||||
[source,js]
|
||||
--------------------------------------------------
|
||||
POST /sales/_search
|
||||
{
|
||||
"aggs" : {
|
||||
"min_price" : {
|
||||
"min" : {
|
||||
"min_price" : {
|
||||
"min" : {
|
||||
"script" : {
|
||||
"file": "my_script",
|
||||
"params": {
|
||||
@ -74,23 +88,28 @@ 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.
|
||||
TIP: For indexed scripts replace the `file` parameter with an `id` parameter.
|
||||
|
||||
==== Value Script
|
||||
|
||||
Let's say that the prices of the documents in our index are in USD, but we would like to compute the min in EURO (and for the sake of this example, lets say the conversion rate is 1.2). We can use a value script to apply the conversion rate to every value before it is aggregated:
|
||||
Let's say that the prices of the documents in our index are in USD, but we
|
||||
would like to compute the min in EURO (and for the sake of this example, let's
|
||||
say the conversion rate is 1.2). We can use a value script to apply the
|
||||
conversion rate to every value before it is aggregated:
|
||||
|
||||
[source,js]
|
||||
--------------------------------------------------
|
||||
POST /sales/_search
|
||||
{
|
||||
"aggs" : {
|
||||
"min_price_in_euros" : {
|
||||
"min" : {
|
||||
"field" : "price",
|
||||
"script" :
|
||||
"lang" : "painless",
|
||||
"inline": "_value * params.conversion_rate",
|
||||
"script" : {
|
||||
"inline" : "_value * params.conversion_rate",
|
||||
"params" : {
|
||||
"conversion_rate" : 1.2
|
||||
}
|
||||
@ -100,15 +119,18 @@ Let's say that the prices of the documents in our index are in USD, but we would
|
||||
}
|
||||
}
|
||||
--------------------------------------------------
|
||||
// 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 they will be ignored but it is also possible to treat
|
||||
them as if they had a value.
|
||||
|
||||
[source,js]
|
||||
--------------------------------------------------
|
||||
POST /sales/_search
|
||||
{
|
||||
"aggs" : {
|
||||
"grade_min" : {
|
||||
@ -120,5 +142,8 @@ had a value.
|
||||
}
|
||||
}
|
||||
--------------------------------------------------
|
||||
// CONSOLE
|
||||
// TEST[setup:sales]
|
||||
|
||||
<1> Documents without a value in the `grade` field will fall into the same bucket as documents that have the value `10`.
|
||||
<1> Documents without a value in the `grade` field will fall into the same
|
||||
bucket as documents that have the value `10`.
|
||||
|
2
docs/src/test/cluster/config/scripts/my_script.painless
Normal file
2
docs/src/test/cluster/config/scripts/my_script.painless
Normal file
@ -0,0 +1,2 @@
|
||||
// Simple script to load a field. Not really a good example, but a simple one.
|
||||
doc[params.field].value
|
Loading…
x
Reference in New Issue
Block a user