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]
|
|
|
|
--------------------------------------------------
|
|
|
|
{
|
|
|
|
"range" : {
|
2013-09-12 09:07:15 -04:00
|
|
|
"age" : {
|
|
|
|
"gte" : 10,
|
|
|
|
"lte" : 20,
|
2013-08-28 19:24:34 -04:00
|
|
|
"boost" : 2.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
--------------------------------------------------
|
|
|
|
|
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
|
|
|
[float]
|
2015-06-03 19:59:22 -04:00
|
|
|
==== Date options
|
2014-09-22 13:40:16 -04:00
|
|
|
|
2014-07-31 10:22:03 -04:00
|
|
|
When applied on `date` fields the `range` filter accepts also a `time_zone` parameter.
|
|
|
|
The `time_zone` parameter will be applied to your input lower and upper bounds and will
|
|
|
|
move them to UTC time based date:
|
|
|
|
|
|
|
|
[source,js]
|
|
|
|
--------------------------------------------------
|
|
|
|
{
|
|
|
|
"range" : {
|
|
|
|
"born" : {
|
|
|
|
"gte": "2012-01-01",
|
|
|
|
"lte": "now",
|
|
|
|
"time_zone": "+1:00"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
--------------------------------------------------
|
|
|
|
|
|
|
|
In the above example, `gte` will be actually moved to `2011-12-31T23:00:00` UTC date.
|
|
|
|
|
|
|
|
NOTE: if you give a date with a timezone explicitly defined and use the `time_zone` parameter, `time_zone` will be
|
2014-12-02 06:15:47 -05:00
|
|
|
ignored. For example, setting `gte` to `2012-01-01T00:00:00+01:00` with `"time_zone":"+10:00"` will still use `+01:00` time zone.
|
2014-07-31 10:22:03 -04:00
|
|
|
|
2014-09-22 13:40:16 -04:00
|
|
|
When applied on `date` fields the `range` query accepts also a `format` parameter.
|
|
|
|
The `format` parameter will help support another date format than the one defined in mapping:
|
|
|
|
|
|
|
|
[source,js]
|
|
|
|
--------------------------------------------------
|
|
|
|
{
|
|
|
|
"range" : {
|
|
|
|
"born" : {
|
|
|
|
"gte": "01/01/2012",
|
|
|
|
"lte": "2013",
|
|
|
|
"format": "dd/MM/yyyy||yyyy"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
--------------------------------------------------
|