2013-08-28 19:24:34 -04:00
|
|
|
[[query-dsl-range-query]]
|
2015-06-03 19:59:22 -04:00
|
|
|
=== Range Query
|
2013-08-28 19:24:34 -04:00
|
|
|
|
|
|
|
Matches 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`:
|
|
|
|
|
|
|
|
[source,js]
|
|
|
|
--------------------------------------------------
|
2016-05-24 05:58:43 -04:00
|
|
|
GET _search
|
2013-08-28 19:24:34 -04:00
|
|
|
{
|
2016-05-24 05:58:43 -04:00
|
|
|
"query": {
|
|
|
|
"range" : {
|
|
|
|
"age" : {
|
|
|
|
"gte" : 10,
|
|
|
|
"lte" : 20,
|
|
|
|
"boost" : 2.0
|
|
|
|
}
|
2013-08-28 19:24:34 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
--------------------------------------------------
|
2016-05-24 05:58:43 -04:00
|
|
|
// CONSOLE
|
2013-08-28 19:24:34 -04:00
|
|
|
|
2013-09-12 09:07:15 -04:00
|
|
|
The `range` query accepts the following parameters:
|
2013-08-28 19:24:34 -04:00
|
|
|
|
2013-09-12 09:07:15 -04:00
|
|
|
[horizontal]
|
|
|
|
`gte`:: Greater-than or equal to
|
|
|
|
`gt`:: Greater-than
|
|
|
|
`lte`:: Less-than or equal to
|
|
|
|
`lt`:: Less-than
|
2014-03-26 09:51:02 -04:00
|
|
|
`boost`:: Sets the boost value of the query, defaults to `1.0`
|
2013-08-28 19:24:34 -04:00
|
|
|
|
2014-09-22 13:40:16 -04:00
|
|
|
|
2015-08-06 11:24:29 -04:00
|
|
|
[[ranges-on-dates]]
|
|
|
|
==== Ranges on date fields
|
|
|
|
|
|
|
|
When running `range` queries on fields of type <<date,`date`>>, ranges can be
|
2016-04-26 14:30:30 -04:00
|
|
|
specified using <<date-math>>:
|
2014-07-31 10:22:03 -04:00
|
|
|
|
|
|
|
[source,js]
|
|
|
|
--------------------------------------------------
|
2016-05-24 05:58:43 -04:00
|
|
|
GET _search
|
2014-07-31 10:22:03 -04:00
|
|
|
{
|
2016-05-24 05:58:43 -04:00
|
|
|
"query": {
|
|
|
|
"range" : {
|
|
|
|
"date" : {
|
|
|
|
"gte" : "now-1d/d",
|
|
|
|
"lt" : "now/d"
|
|
|
|
}
|
2014-07-31 10:22:03 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
--------------------------------------------------
|
2016-05-24 05:58:43 -04:00
|
|
|
// CONSOLE
|
2014-07-31 10:22:03 -04:00
|
|
|
|
2015-08-06 11:24:29 -04:00
|
|
|
===== Date math and rounding
|
|
|
|
|
|
|
|
When using <<date-math,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:
|
|
|
|
|
|
|
|
[horizontal]
|
|
|
|
`gt`::
|
|
|
|
|
|
|
|
Greater than the date rounded up: `2014-11-18||/M` becomes
|
|
|
|
`2014-11-30T23:59:59.999`, ie excluding the entire month.
|
|
|
|
|
|
|
|
`gte`::
|
|
|
|
|
|
|
|
Greater than or equal to the date rounded down: `2014-11-18||/M` becomes
|
|
|
|
`2014-11-01`, ie including the entire month.
|
|
|
|
|
|
|
|
`lt`::
|
|
|
|
|
|
|
|
Less than the date rounded down: `2014-11-18||/M` becomes `2014-11-01`, ie
|
|
|
|
excluding the entire month.
|
|
|
|
|
|
|
|
`lte`::
|
|
|
|
|
|
|
|
Less than or equal to the date rounded up: `2014-11-18||/M` becomes
|
|
|
|
`2014-11-30T23:59:59.999`, ie including the entire month.
|
2014-07-31 10:22:03 -04:00
|
|
|
|
2015-08-06 11:24:29 -04:00
|
|
|
===== Date format in range queries
|
2014-07-31 10:22:03 -04:00
|
|
|
|
2015-08-06 11:24:29 -04:00
|
|
|
Formatted dates will be parsed using the <<mapping-date-format,`format`>>
|
|
|
|
specified on the <<date,`date`>> field by default, but it can be overridden by
|
|
|
|
passing the `format` parameter to the `range` query:
|
2014-09-22 13:40:16 -04:00
|
|
|
|
|
|
|
[source,js]
|
|
|
|
--------------------------------------------------
|
2016-05-24 05:58:43 -04:00
|
|
|
GET _search
|
2014-09-22 13:40:16 -04:00
|
|
|
{
|
2016-05-24 05:58:43 -04:00
|
|
|
"query": {
|
|
|
|
"range" : {
|
|
|
|
"born" : {
|
|
|
|
"gte": "01/01/2012",
|
|
|
|
"lte": "2013",
|
|
|
|
"format": "dd/MM/yyyy||yyyy"
|
|
|
|
}
|
2014-09-22 13:40:16 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
--------------------------------------------------
|
2016-05-24 05:58:43 -04:00
|
|
|
// CONSOLE
|
2015-08-06 11:24:29 -04:00
|
|
|
|
2018-05-24 05:20:00 -04:00
|
|
|
Note that if the date misses some of the year, month and day coordinates, the
|
|
|
|
missing parts are filled with the start of
|
|
|
|
https://en.wikipedia.org/wiki/Unix_time[unix time], which is January 1st, 1970.
|
|
|
|
This means, that when e.g. specifying `dd` as the format, a value like `"gte" : 10`
|
|
|
|
will translate to `1970-01-10T00:00:00.000Z`.
|
|
|
|
|
2015-08-06 11:24:29 -04:00
|
|
|
===== Time zone in range queries
|
|
|
|
|
|
|
|
Dates can be converted from another timezone to UTC either by specifying the
|
|
|
|
time zone in the date value itself (if the <<mapping-date-format, `format`>>
|
|
|
|
accepts it), or it can be specified as the `time_zone` parameter:
|
|
|
|
|
|
|
|
[source,js]
|
|
|
|
--------------------------------------------------
|
2016-05-24 05:58:43 -04:00
|
|
|
GET _search
|
2015-08-06 11:24:29 -04:00
|
|
|
{
|
2016-05-24 05:58:43 -04:00
|
|
|
"query": {
|
|
|
|
"range" : {
|
|
|
|
"timestamp" : {
|
|
|
|
"gte": "2015-01-01 00:00:00", <1>
|
|
|
|
"lte": "now", <2>
|
|
|
|
"time_zone": "+01:00"
|
|
|
|
}
|
2015-08-06 11:24:29 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
--------------------------------------------------
|
2016-05-24 05:58:43 -04:00
|
|
|
// CONSOLE
|
2015-08-06 11:24:29 -04:00
|
|
|
<1> This date will be converted to `2014-12-31T23:00:00 UTC`.
|
2019-03-29 18:38:37 -04:00
|
|
|
<2> `now` is not affected by the `time_zone` parameter, its always the current system time (in UTC).
|
|
|
|
However, when using <<date-math,date math rounding>> (e.g. down to the nearest day using `now/d`),
|
|
|
|
the provided `time_zone` will be considered.
|
|
|
|
|
2018-11-06 10:45:10 -05:00
|
|
|
|
|
|
|
[[querying-range-fields]]
|
|
|
|
==== Querying range fields
|
|
|
|
|
|
|
|
`range` queries can be used on fields of type <<range,`range`>>, allowing to
|
|
|
|
match a range specified in the query with a range field value in the document.
|
|
|
|
The `relation` parameter controls how these two ranges are matched:
|
|
|
|
|
|
|
|
[horizontal]
|
|
|
|
`WITHIN`::
|
|
|
|
|
|
|
|
Matches documents who's range field is entirely within the query's range.
|
|
|
|
|
|
|
|
`CONTAINS`::
|
|
|
|
|
|
|
|
Matches documents who's range field entirely contains the query's range.
|
|
|
|
|
|
|
|
`INTERSECTS`::
|
|
|
|
|
|
|
|
Matches documents who's range field intersects the query's range.
|
|
|
|
This is the default value when querying range fields.
|
|
|
|
|
|
|
|
For examples, see <<range,`range`>> mapping type.
|