2013-11-24 06:13:08 -05:00
[[search-aggregations-bucket-range-aggregation]]
2014-05-12 19:35:58 -04:00
=== Range Aggregation
2013-11-24 06:13:08 -05:00
A multi-bucket value source based aggregation that enables the user to define a set of ranges - each representing a bucket. During the aggregation process, the values extracted from each document will be checked against each bucket range and "bucket" the relevant/matching document.
2016-02-09 05:07:32 -05:00
Note that this aggregation includes the `from` value and excludes the `to` value for each range.
2013-11-24 06:13:08 -05:00
Example:
[source,js]
--------------------------------------------------
2017-08-30 06:11:10 -04:00
GET /_search
2013-11-24 06:13:08 -05:00
{
"aggs" : {
"price_ranges" : {
2014-05-12 19:35:58 -04:00
"range" : {
2013-11-24 06:13:08 -05:00
"field" : "price",
"ranges" : [
2017-08-30 06:11:10 -04:00
{ "to" : 100.0 },
{ "from" : 100.0, "to" : 200.0 },
{ "from" : 200.0 }
2013-11-24 06:13:08 -05:00
]
}
}
}
}
--------------------------------------------------
2017-08-30 06:11:10 -04:00
// CONSOLE
// TEST[setup:sales]
// TEST[s/GET \/_search/GET \/_search\?filter_path=aggregations/]
2013-11-24 06:13:08 -05:00
Response:
[source,js]
--------------------------------------------------
{
...
"aggregations": {
2014-01-28 11:46:26 -05:00
"price_ranges" : {
"buckets": [
{
2017-08-30 06:11:10 -04:00
"key": "*-100.0",
"to": 100.0,
2014-01-28 11:46:26 -05:00
"doc_count": 2
},
{
2017-08-30 06:11:10 -04:00
"key": "100.0-200.0",
"from": 100.0,
"to": 200.0,
"doc_count": 2
2014-01-28 11:46:26 -05:00
},
{
2017-08-30 06:11:10 -04:00
"key": "200.0-*",
"from": 200.0,
"doc_count": 3
2014-01-28 11:46:26 -05:00
}
]
}
2013-11-24 06:13:08 -05:00
}
}
--------------------------------------------------
2017-08-30 06:11:10 -04:00
// TESTRESPONSE[s/\.\.\.//]
2013-11-24 06:13:08 -05:00
==== Keyed Response
2014-05-12 16:15:07 -04:00
Setting the `keyed` flag to `true` will associate a unique string key with each bucket and return the ranges as a hash rather than an array:
2013-11-24 06:13:08 -05:00
[source,js]
--------------------------------------------------
2017-08-30 06:11:10 -04:00
GET /_search
2013-11-24 06:13:08 -05:00
{
"aggs" : {
"price_ranges" : {
2014-05-12 19:35:58 -04:00
"range" : {
2013-11-24 06:13:08 -05:00
"field" : "price",
"keyed" : true,
"ranges" : [
2017-08-30 06:11:10 -04:00
{ "to" : 100 },
{ "from" : 100, "to" : 200 },
{ "from" : 200 }
2013-11-24 06:13:08 -05:00
]
}
}
}
}
--------------------------------------------------
2017-08-30 06:11:10 -04:00
// CONSOLE
// TEST[setup:sales]
// TEST[s/GET \/_search/GET \/_search\?filter_path=aggregations/]
2013-11-24 06:13:08 -05:00
Response:
[source,js]
--------------------------------------------------
{
...
"aggregations": {
2014-01-28 11:46:26 -05:00
"price_ranges" : {
"buckets": {
2017-08-30 06:11:10 -04:00
"*-100.0": {
"to": 100.0,
2014-01-28 11:46:26 -05:00
"doc_count": 2
},
2017-08-30 06:11:10 -04:00
"100.0-200.0": {
"from": 100.0,
"to": 200.0,
"doc_count": 2
2014-01-28 11:46:26 -05:00
},
2017-08-30 06:11:10 -04:00
"200.0-*": {
"from": 200.0,
"doc_count": 3
2014-01-28 11:46:26 -05:00
}
2013-11-24 06:13:08 -05:00
}
}
}
}
--------------------------------------------------
2017-08-30 06:11:10 -04:00
// TESTRESPONSE[s/\.\.\.//]
2013-11-24 06:13:08 -05:00
It is also possible to customize the key for each range:
[source,js]
--------------------------------------------------
2017-08-30 06:11:10 -04:00
GET /_search
2013-11-24 06:13:08 -05:00
{
"aggs" : {
"price_ranges" : {
2014-05-12 19:35:58 -04:00
"range" : {
2013-11-24 06:13:08 -05:00
"field" : "price",
"keyed" : true,
"ranges" : [
2017-08-30 06:11:10 -04:00
{ "key" : "cheap", "to" : 100 },
{ "key" : "average", "from" : 100, "to" : 200 },
{ "key" : "expensive", "from" : 200 }
2013-11-24 06:13:08 -05:00
]
}
}
}
}
--------------------------------------------------
2017-08-30 06:11:10 -04:00
// CONSOLE
// TEST[setup:sales]
// TEST[s/GET \/_search/GET \/_search\?filter_path=aggregations/]
Response:
[source,js]
--------------------------------------------------
{
...
"aggregations": {
"price_ranges" : {
"buckets": {
"cheap": {
"to": 100.0,
"doc_count": 2
},
"average": {
"from": 100.0,
"to": 200.0,
"doc_count": 2
},
"expensive": {
"from": 200.0,
"doc_count": 3
}
}
}
}
}
--------------------------------------------------
// TESTRESPONSE[s/\.\.\.//]
2013-11-24 06:13:08 -05:00
==== Script
2017-08-30 06:11:10 -04:00
Range aggregation accepts a `script` parameter. This parameter allows to defined an inline `script` that
will be executed during aggregation execution.
The following example shows how to use an `inline` script with the `painless` script language and no script parameters:
2013-11-24 06:13:08 -05:00
[source,js]
--------------------------------------------------
2017-08-30 06:11:10 -04:00
GET /_search
2013-11-24 06:13:08 -05:00
{
"aggs" : {
"price_ranges" : {
2014-01-28 11:46:26 -05:00
"range" : {
2016-06-27 09:55:16 -04:00
"script" : {
"lang": "painless",
2017-06-09 11:29:25 -04:00
"source": "doc['price'].value"
2016-06-27 09:55:16 -04:00
},
2013-11-24 06:13:08 -05:00
"ranges" : [
2017-08-30 06:11:10 -04:00
{ "to" : 100 },
{ "from" : 100, "to" : 200 },
{ "from" : 200 }
2013-11-24 06:13:08 -05:00
]
}
}
}
}
--------------------------------------------------
2017-08-30 06:11:10 -04:00
// CONSOLE
It is also possible to use stored scripts. Here is a simple stored script:
[source,js]
--------------------------------------------------
POST /_scripts/convert_currency
{
"script": {
"lang": "painless",
"source": "doc[params.field].value * params.conversion_rate"
}
}
--------------------------------------------------
// CONSOLE
// TEST[setup:sales]
2013-11-24 06:13:08 -05:00
2017-08-30 06:11:10 -04:00
And this new stored script can be used in the range aggregation like this:
2015-05-12 05:37:22 -04:00
[source,js]
--------------------------------------------------
2017-08-30 06:11:10 -04:00
GET /_search
2015-05-12 05:37:22 -04:00
{
"aggs" : {
"price_ranges" : {
"range" : {
"script" : {
2017-08-30 06:11:10 -04:00
"id": "convert_currency", <1>
"params": { <2>
"field": "price",
"conversion_rate": 0.835526591
2015-05-12 05:37:22 -04:00
}
},
"ranges" : [
2017-08-30 06:11:10 -04:00
{ "from" : 0, "to" : 100 },
2015-05-12 05:37:22 -04:00
{ "from" : 100 }
]
}
}
}
}
--------------------------------------------------
2017-08-30 06:11:10 -04:00
// CONSOLE
// TEST[s/GET \/_search/GET \/_search\?filter_path=aggregations/]
// TEST[continued]
<1> Id of the stored script
<2> Parameters to use when executing the stored script
//////////////////////////
[source,js]
--------------------------------------------------
{
"aggregations": {
"price_ranges" : {
"buckets": [
{
"key" : "0.0-100.0",
"from" : 0.0,
"to" : 100.0,
"doc_count" : 2
},
{
"key" : "100.0-*",
"from" : 100.0,
"doc_count" : 5
}
]
}
}
}
--------------------------------------------------
// TESTRESPONSE
//////////////////////////
2015-05-12 05:37:22 -04:00
2013-11-24 06:13:08 -05:00
==== Value Script
Lets say the product prices are in USD but we would like to get the price ranges in EURO. We can use value script to convert the prices prior the aggregation (assuming conversion rate of 0.8)
[source,js]
--------------------------------------------------
2017-08-30 06:11:10 -04:00
GET /sales/_search
2013-11-24 06:13:08 -05:00
{
"aggs" : {
"price_ranges" : {
2014-05-12 19:35:58 -04:00
"range" : {
2013-11-24 06:13:08 -05:00
"field" : "price",
2016-06-27 09:55:16 -04:00
"script" : {
2017-06-09 11:29:25 -04:00
"source": "_value * params.conversion_rate",
2016-06-27 09:55:16 -04:00
"params" : {
"conversion_rate" : 0.8
}
2013-11-24 06:13:08 -05:00
},
"ranges" : [
{ "to" : 35 },
{ "from" : 35, "to" : 70 },
{ "from" : 70 }
]
}
}
}
}
--------------------------------------------------
2017-08-30 06:11:10 -04:00
// CONSOLE
// TEST[setup:sales]
2013-11-24 06:13:08 -05:00
==== Sub Aggregations
The following example, not only "bucket" the documents to the different buckets but also computes statistics over the prices in each price range
[source,js]
--------------------------------------------------
2017-08-30 06:11:10 -04:00
GET /_search
2013-11-24 06:13:08 -05:00
{
"aggs" : {
"price_ranges" : {
2014-05-12 19:35:58 -04:00
"range" : {
2013-11-24 06:13:08 -05:00
"field" : "price",
"ranges" : [
2017-08-30 06:11:10 -04:00
{ "to" : 100 },
{ "from" : 100, "to" : 200 },
{ "from" : 200 }
2013-11-24 06:13:08 -05:00
]
},
"aggs" : {
2014-05-12 19:35:58 -04:00
"price_stats" : {
2013-11-24 06:13:08 -05:00
"stats" : { "field" : "price" }
}
}
}
}
}
--------------------------------------------------
2017-08-30 06:11:10 -04:00
// CONSOLE
// TEST[setup:sales]
// TEST[s/GET \/_search/GET \/_search\?filter_path=aggregations/]
2013-11-24 06:13:08 -05:00
Response:
[source,js]
--------------------------------------------------
{
2017-08-30 06:11:10 -04:00
...
"aggregations": {
"price_ranges": {
"buckets": [
{
"key": "*-100.0",
"to": 100.0,
"doc_count": 2,
"price_stats": {
"count": 2,
"min": 10.0,
"max": 50.0,
"avg": 30.0,
"sum": 60.0
}
},
{
"key": "100.0-200.0",
"from": 100.0,
"to": 200.0,
"doc_count": 2,
"price_stats": {
"count": 2,
"min": 150.0,
"max": 175.0,
"avg": 162.5,
"sum": 325.0
}
},
{
"key": "200.0-*",
"from": 200.0,
"doc_count": 3,
"price_stats": {
"count": 3,
"min": 200.0,
"max": 200.0,
"avg": 200.0,
"sum": 600.0
}
2014-01-28 11:46:26 -05:00
}
2017-08-30 06:11:10 -04:00
]
2013-11-24 06:13:08 -05:00
}
2017-08-30 06:11:10 -04:00
}
2013-11-24 06:13:08 -05:00
}
--------------------------------------------------
2017-08-30 06:11:10 -04:00
// TESTRESPONSE[s/\.\.\.//]
2013-11-24 06:13:08 -05:00
If a sub aggregation is also based on the same value source as the range aggregation (like the `stats` aggregation in the example above) it is possible to leave out the value source definition for it. The following will return the same response as above:
[source,js]
--------------------------------------------------
2017-08-30 06:11:10 -04:00
GET /_search
2013-11-24 06:13:08 -05:00
{
"aggs" : {
"price_ranges" : {
2014-05-12 19:35:58 -04:00
"range" : {
2013-11-24 06:13:08 -05:00
"field" : "price",
"ranges" : [
2017-08-30 06:11:10 -04:00
{ "to" : 100 },
{ "from" : 100, "to" : 200 },
{ "from" : 200 }
2013-11-24 06:13:08 -05:00
]
},
"aggs" : {
2014-05-12 19:35:58 -04:00
"price_stats" : {
2013-11-24 06:13:08 -05:00
"stats" : {} <1>
}
}
}
}
}
2014-05-12 19:35:58 -04:00
--------------------------------------------------
2017-08-30 06:11:10 -04:00
// CONSOLE
2014-05-12 16:15:07 -04:00
<1> We don't need to specify the `price` as we "inherit" it by default from the parent `range` aggregation