WARNING: Version 1.4 of Elasticsearch has passed its EOL date.
This documentation is no longer being maintained and may be removed. If you are running this version, we strongly advise you to upgrade. For the latest information, see the current release documentation.
Range Facets
editRange Facets
editFacets are deprecated and will be removed in a future release. You are encouraged to migrate to aggregations instead.
The equivalent aggregation would be the
range
aggregation.
range
facet allows to specify a set of ranges and get both the number
of docs (count) that fall within each range, and aggregated data either
based on the field, or using another field. Here is a simple example:
{ "query" : { "match_all" : {} }, "facets" : { "range1" : { "range" : { "field" : "field_name", "ranges" : [ { "to" : 50 }, { "from" : 20, "to" : 70 }, { "from" : 70, "to" : 120 }, { "from" : 150 } ] } } } }
Another option which is a bit more DSL enabled is to provide the ranges on the actual field name, for example:
{ "query" : { "match_all" : {} }, "facets" : { "range1" : { "range" : { "my_field" : [ { "to" : 50 }, { "from" : 20, "to" : 70 }, { "from" : 70, "to" : 120 }, { "from" : 150 } ] } } } }
The range
facet always includes the from
parameter and excludes the
to
parameter for each range.
Key and Value
editThe range
facet allows to use a different field to check if its value
falls within a range, and another field to compute aggregated data per
range (like total). For example:
{ "query" : { "match_all" : {} }, "facets" : { "range1" : { "range" : { "key_field" : "field_name", "value_field" : "another_field_name", "ranges" : [ { "to" : 50 }, { "from" : 20, "to" : 70 }, { "from" : 70, "to" : 120 }, { "from" : 150 } ] } } } }
Script Key and Value
editSometimes, some munging of both the key and the value are needed. In the key case, before it is checked if it falls within a range, and for the value, when the statistical data is computed per range scripts can be used. Here is an example:
{ "query" : { "match_all" : {} }, "facets" : { "range1" : { "range" : { "key_script" : "doc['date'].date.minuteOfHour", "value_script" : "doc['num1'].value", "ranges" : [ { "to" : 50 }, { "from" : 20, "to" : 70 }, { "from" : 70, "to" : 120 }, { "from" : 150 } ] } } } }
Date Ranges
editThe range facet support also providing the range as string formatted dates.