2013-08-28 19:24:34 -04:00
|
|
|
[[query-dsl-range-query]]
|
2019-07-18 10:18:11 -04:00
|
|
|
=== Range query
|
|
|
|
++++
|
|
|
|
<titleabbrev>Range</titleabbrev>
|
|
|
|
++++
|
2013-08-28 19:24:34 -04:00
|
|
|
|
2019-06-25 15:24:44 -04:00
|
|
|
Returns documents that contain terms within a provided range.
|
|
|
|
|
|
|
|
[[range-query-ex-request]]
|
|
|
|
==== Example request
|
|
|
|
|
|
|
|
The following search returns documents where the `age` field contains a term
|
|
|
|
between `10` and `20`.
|
2013-08-28 19:24:34 -04:00
|
|
|
|
2019-09-09 12:35:50 -04:00
|
|
|
[source,console]
|
2019-06-25 15:24:44 -04:00
|
|
|
----
|
2020-01-22 03:38:52 -05: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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-06-25 15:24:44 -04:00
|
|
|
----
|
2013-08-28 19:24:34 -04:00
|
|
|
|
2019-06-25 15:24:44 -04:00
|
|
|
[[range-query-top-level-params]]
|
|
|
|
==== Top-level parameters for `range`
|
|
|
|
|
|
|
|
`<field>`::
|
|
|
|
+
|
|
|
|
--
|
2019-07-31 14:18:22 -04:00
|
|
|
(Required, object) Field you wish to search.
|
2019-06-25 15:24:44 -04:00
|
|
|
--
|
|
|
|
|
|
|
|
[[range-query-field-params]]
|
|
|
|
==== Parameters for `<field>`
|
|
|
|
|
|
|
|
`gt`::
|
2019-07-31 14:18:22 -04:00
|
|
|
(Optional) Greater than.
|
2019-06-25 15:24:44 -04:00
|
|
|
|
|
|
|
`gte`::
|
2019-07-31 14:18:22 -04:00
|
|
|
(Optional) Greater than or equal to.
|
2019-06-25 15:24:44 -04:00
|
|
|
|
|
|
|
`lt`::
|
2019-07-31 14:18:22 -04:00
|
|
|
(Optional) Less than.
|
2019-06-25 15:24:44 -04:00
|
|
|
|
|
|
|
`lte`::
|
2019-07-31 14:18:22 -04:00
|
|
|
(Optional) Less than or equal to.
|
2019-06-25 15:24:44 -04:00
|
|
|
|
|
|
|
`format`::
|
|
|
|
+
|
|
|
|
--
|
2019-07-31 14:18:22 -04:00
|
|
|
(Optional, string) Date format used to convert `date` values in the query.
|
2019-06-25 15:24:44 -04:00
|
|
|
|
|
|
|
By default, {es} uses the <<mapping-date-format,date `format`>> provided in the
|
|
|
|
`<field>`'s mapping. This value overrides that mapping format.
|
2013-08-28 19:24:34 -04:00
|
|
|
|
2019-07-31 14:18:22 -04:00
|
|
|
For valid syntax, see <<mapping-date-format,`format`>>.
|
2013-08-28 19:24:34 -04:00
|
|
|
|
2019-06-25 15:24:44 -04:00
|
|
|
[WARNING]
|
|
|
|
====
|
|
|
|
If a `format` and `date` value are incomplete, {es} replaces any missing year,
|
|
|
|
month, or date component with the start of
|
|
|
|
https://en.wikipedia.org/wiki/Unix_time[Unix time], which is January 1st, 1970.
|
|
|
|
|
|
|
|
For example, if the `format` value is `dd`, {es} converts a `gte` value of `10`
|
|
|
|
to `1970-01-10T00:00:00.000Z`.
|
|
|
|
====
|
|
|
|
|
|
|
|
--
|
|
|
|
|
|
|
|
[[querying-range-fields]]
|
|
|
|
`relation`::
|
|
|
|
+
|
|
|
|
--
|
2019-07-31 14:18:22 -04:00
|
|
|
(Optional, string) Indicates how the range query matches values for `range`
|
|
|
|
fields. Valid values are:
|
2019-06-25 15:24:44 -04:00
|
|
|
|
|
|
|
`INTERSECTS` (Default)::
|
|
|
|
Matches documents with a range field value that intersects the query's range.
|
|
|
|
|
|
|
|
`CONTAINS`::
|
|
|
|
Matches documents with a range field value that entirely contains the query's range.
|
|
|
|
|
|
|
|
`WITHIN`::
|
|
|
|
Matches documents with a range field value entirely within the query's range.
|
|
|
|
--
|
|
|
|
|
|
|
|
`time_zone`::
|
|
|
|
+
|
|
|
|
--
|
2019-07-31 14:18:22 -04:00
|
|
|
(Optional, string)
|
2019-06-25 15:24:44 -04:00
|
|
|
https://en.wikipedia.org/wiki/List_of_UTC_time_offsets[Coordinated Universal
|
|
|
|
Time (UTC) offset] or
|
|
|
|
https://en.wikipedia.org/wiki/List_of_tz_database_time_zones[IANA time zone]
|
2019-07-31 14:18:22 -04:00
|
|
|
used to convert `date` values in the query to UTC.
|
2019-06-25 15:24:44 -04:00
|
|
|
|
|
|
|
Valid values are ISO 8601 UTC offsets, such as `+01:00` or -`08:00`, and IANA
|
|
|
|
time zone IDs, such as `America/Los_Angeles`.
|
|
|
|
|
|
|
|
For an example query using the `time_zone` parameter, see
|
|
|
|
<<range-query-time-zone,Time zone in `range` queries>>.
|
|
|
|
|
|
|
|
[NOTE]
|
|
|
|
====
|
|
|
|
The `time_zone` parameter does **not** affect the <<date-math,date math>> value
|
|
|
|
of `now`. `now` is always the current system time in UTC.
|
|
|
|
|
|
|
|
However, the `time_zone` parameter does convert dates calculated using `now` and
|
|
|
|
<<date-math,date math rounding>>. For example, the `time_zone` parameter will
|
|
|
|
convert a value of `now/d`.
|
|
|
|
====
|
|
|
|
--
|
|
|
|
|
|
|
|
`boost`::
|
|
|
|
+
|
|
|
|
--
|
2019-07-31 14:18:22 -04:00
|
|
|
(Optional, float) Floating point number used to decrease or increase the
|
2019-08-02 14:15:12 -04:00
|
|
|
<<relevance-scores,relevance scores>> of a query. Defaults to `1.0`.
|
2019-06-25 15:24:44 -04:00
|
|
|
|
|
|
|
You can use the `boost` parameter to adjust relevance scores for searches
|
|
|
|
containing two or more queries.
|
|
|
|
|
|
|
|
Boost values are relative to the default value of `1.0`. A boost value between
|
|
|
|
`0` and `1.0` decreases the relevance score. A value greater than `1.0`
|
|
|
|
increases the relevance score.
|
|
|
|
--
|
|
|
|
|
|
|
|
[[range-query-notes]]
|
|
|
|
==== Notes
|
2014-09-22 13:40:16 -04:00
|
|
|
|
2020-02-12 16:56:14 -05:00
|
|
|
[[ranges-on-text-and-keyword]]
|
|
|
|
===== Using the `range` query with `text` and `keyword` fields
|
|
|
|
Range queries on <<text, `text`>> or <<keyword, `keyword`>> files will not be executed if
|
|
|
|
<<query-dsl-allow-expensive-queries, `search.allow_expensive_queries`>> is set to false.
|
|
|
|
|
2015-08-06 11:24:29 -04:00
|
|
|
[[ranges-on-dates]]
|
2019-06-25 15:24:44 -04:00
|
|
|
===== Using the `range` query with `date` fields
|
|
|
|
|
|
|
|
When the `<field>` parameter is a <<date,`date`>> field datatype, you can use
|
|
|
|
<<date-math,date math>> with the following parameters:
|
2015-08-06 11:24:29 -04:00
|
|
|
|
2019-06-25 15:24:44 -04:00
|
|
|
* `gt`
|
|
|
|
* `gte`
|
|
|
|
* `lt`
|
|
|
|
* `lte`
|
|
|
|
|
|
|
|
For example, the following search returns documents where the `timestamp` field
|
|
|
|
contains a date between today and yesterday.
|
2014-07-31 10:22:03 -04:00
|
|
|
|
2019-09-09 12:35:50 -04:00
|
|
|
[source,console]
|
2019-06-25 15:24:44 -04:00
|
|
|
----
|
2020-01-22 03:38:52 -05:00
|
|
|
GET /_search
|
2014-07-31 10:22:03 -04:00
|
|
|
{
|
2016-05-24 05:58:43 -04:00
|
|
|
"query": {
|
|
|
|
"range" : {
|
2019-06-25 15:24:44 -04:00
|
|
|
"timestamp" : {
|
2016-05-24 05:58:43 -04:00
|
|
|
"gte" : "now-1d/d",
|
|
|
|
"lt" : "now/d"
|
|
|
|
}
|
2014-07-31 10:22:03 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-06-25 15:24:44 -04:00
|
|
|
----
|
2014-07-31 10:22:03 -04:00
|
|
|
|
2015-08-06 11:24:29 -04:00
|
|
|
|
2019-06-25 15:24:44 -04:00
|
|
|
[[range-query-date-math-rounding]]
|
|
|
|
====== Date math and rounding
|
|
|
|
{es} rounds <<date-math,date math>> values in parameters as follows:
|
2015-08-06 11:24:29 -04:00
|
|
|
|
|
|
|
`gt`::
|
2019-06-25 15:24:44 -04:00
|
|
|
+
|
|
|
|
--
|
|
|
|
Rounds up to the lastest millisecond.
|
2015-08-06 11:24:29 -04:00
|
|
|
|
2019-09-19 16:38:07 -04:00
|
|
|
For example, `2014-11-18||/M` rounds up to `2014-11-30T23:59:59.999`, excluding
|
2019-06-25 15:24:44 -04:00
|
|
|
the entire month.
|
|
|
|
--
|
2015-08-06 11:24:29 -04:00
|
|
|
|
|
|
|
`gte`::
|
2019-06-25 15:24:44 -04:00
|
|
|
+
|
|
|
|
--
|
|
|
|
Rounds down to the first millisecond.
|
2015-08-06 11:24:29 -04:00
|
|
|
|
2019-09-19 16:38:07 -04:00
|
|
|
For example, `2014-11-18||/M` rounds down to `2014-11-01`, including
|
2019-06-25 15:24:44 -04:00
|
|
|
the entire month.
|
|
|
|
--
|
2015-08-06 11:24:29 -04:00
|
|
|
|
|
|
|
`lt`::
|
2019-06-25 15:24:44 -04:00
|
|
|
+
|
|
|
|
--
|
|
|
|
Rounds down to the first millisecond.
|
2015-08-06 11:24:29 -04:00
|
|
|
|
2019-06-25 15:24:44 -04:00
|
|
|
For example, `2014-11-18||/M` rounds down to `2014-11-01`, excluding
|
|
|
|
the entire month.
|
|
|
|
--
|
2015-08-06 11:24:29 -04:00
|
|
|
|
|
|
|
`lte`::
|
2019-06-25 15:24:44 -04:00
|
|
|
+
|
|
|
|
--
|
|
|
|
Rounds up to the lastest millisecond.
|
2015-08-06 11:24:29 -04:00
|
|
|
|
2019-06-25 15:24:44 -04:00
|
|
|
For example, `2014-11-18||/M` rounds up to `2014-11-30T23:59:59.999`, including
|
|
|
|
the entire month.
|
|
|
|
--
|
2014-07-31 10:22:03 -04:00
|
|
|
|
2019-06-25 15:24:44 -04:00
|
|
|
[[range-query-time-zone]]
|
|
|
|
===== Example query using `time_zone` parameter
|
2014-07-31 10:22:03 -04:00
|
|
|
|
2019-06-25 15:24:44 -04:00
|
|
|
You can use the `time_zone` parameter to convert `date` values to UTC using a
|
|
|
|
UTC offset. For example:
|
2014-09-22 13:40:16 -04:00
|
|
|
|
2019-09-09 12:35:50 -04:00
|
|
|
[source,console]
|
2019-06-25 15:24:44 -04:00
|
|
|
----
|
2020-01-22 03:38:52 -05:00
|
|
|
GET /_search
|
2015-08-06 11:24:29 -04:00
|
|
|
{
|
2020-01-10 08:23:25 -05:00
|
|
|
"query": {
|
|
|
|
"range": {
|
|
|
|
"timestamp": {
|
|
|
|
"time_zone": "+01:00", <1>
|
|
|
|
"gte": "2020-01-01T00:00:00", <2>
|
|
|
|
"lte": "now" <3>
|
|
|
|
}
|
2015-08-06 11:24:29 -04:00
|
|
|
}
|
2020-01-10 08:23:25 -05:00
|
|
|
}
|
2015-08-06 11:24:29 -04:00
|
|
|
}
|
2019-06-25 15:24:44 -04:00
|
|
|
----
|
2020-01-10 08:23:25 -05:00
|
|
|
// TEST[continued]
|
2019-09-09 12:35:50 -04:00
|
|
|
|
2019-06-25 15:24:44 -04:00
|
|
|
<1> Indicates that `date` values use a UTC offset of `+01:00`.
|
|
|
|
<2> With a UTC offset of `+01:00`, {es} converts this date to
|
2020-01-10 08:23:25 -05:00
|
|
|
`2019-12-31T23:00:00 UTC`.
|
2020-01-22 03:38:52 -05:00
|
|
|
<3> The `time_zone` parameter does not affect the `now` value.
|