WARNING: Version 5.2 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 Query
editRange Query
editMatches documents with fields that have terms within a certain range.
The type of the Lucene query depends on the field type, for string
fields, the TermRangeQuery
, while for number/date fields, the query is
a NumericRangeQuery
. The following example returns all documents where
age
is between 10
and 20
:
GET _search { "query": { "range" : { "age" : { "gte" : 10, "lte" : 20, "boost" : 2.0 } } } }
The range
query accepts the following parameters:
|
Greater-than or equal to |
|
Greater-than |
|
Less-than or equal to |
|
Less-than |
|
Sets the boost value of the query, defaults to |
Ranges on date fields
editWhen running range
queries on fields of type date
, ranges can be
specified using Date Math:
GET _search { "query": { "range" : { "date" : { "gte" : "now-1d/d", "lt" : "now/d" } } } }
Date math and rounding
editWhen using date math to round dates to the nearest day, month, hour, etc, the rounded dates depend on whether the ends of the ranges are inclusive or exclusive.
Rounding up moves to the last millisecond of the rounding scope, and rounding down to the first millisecond of the rounding scope. For example:
|
Greater than the date rounded up: |
|
Greater than or equal to the date rounded down: |
|
Less than the date rounded down: |
|
Less than or equal to the date rounded up: |
Date format in range queries
editFormatted dates will be parsed using the format
specified on the date
field by default, but it can be overridden by
passing the format
parameter to the range
query:
GET _search { "query": { "range" : { "born" : { "gte": "01/01/2012", "lte": "2013", "format": "dd/MM/yyyy||yyyy" } } } }
Time zone in range queries
editDates can be converted from another timezone to UTC either by specifying the
time zone in the date value itself (if the format
accepts it), or it can be specified as the time_zone
parameter: