range-query.asciidoc 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. [[query-dsl-range-query]]
  2. === Range query
  3. ++++
  4. <titleabbrev>Range</titleabbrev>
  5. ++++
  6. Returns documents that contain terms within a provided range.
  7. [[range-query-ex-request]]
  8. ==== Example request
  9. The following search returns documents where the `age` field contains a term
  10. between `10` and `20`.
  11. [source,console]
  12. ----
  13. GET _search
  14. {
  15. "query": {
  16. "range" : {
  17. "age" : {
  18. "gte" : 10,
  19. "lte" : 20,
  20. "boost" : 2.0
  21. }
  22. }
  23. }
  24. }
  25. ----
  26. [[range-query-top-level-params]]
  27. ==== Top-level parameters for `range`
  28. `<field>`::
  29. +
  30. --
  31. (Required, object) Field you wish to search.
  32. --
  33. [[range-query-field-params]]
  34. ==== Parameters for `<field>`
  35. `gt`::
  36. (Optional) Greater than.
  37. `gte`::
  38. (Optional) Greater than or equal to.
  39. `lt`::
  40. (Optional) Less than.
  41. `lte`::
  42. (Optional) Less than or equal to.
  43. `format`::
  44. +
  45. --
  46. (Optional, string) Date format used to convert `date` values in the query.
  47. By default, {es} uses the <<mapping-date-format,date `format`>> provided in the
  48. `<field>`'s mapping. This value overrides that mapping format.
  49. For valid syntax, see <<mapping-date-format,`format`>>.
  50. [WARNING]
  51. ====
  52. If a `format` and `date` value are incomplete, {es} replaces any missing year,
  53. month, or date component with the start of
  54. https://en.wikipedia.org/wiki/Unix_time[Unix time], which is January 1st, 1970.
  55. For example, if the `format` value is `dd`, {es} converts a `gte` value of `10`
  56. to `1970-01-10T00:00:00.000Z`.
  57. ====
  58. --
  59. [[querying-range-fields]]
  60. `relation`::
  61. +
  62. --
  63. (Optional, string) Indicates how the range query matches values for `range`
  64. fields. Valid values are:
  65. `INTERSECTS` (Default)::
  66. Matches documents with a range field value that intersects the query's range.
  67. `CONTAINS`::
  68. Matches documents with a range field value that entirely contains the query's range.
  69. `WITHIN`::
  70. Matches documents with a range field value entirely within the query's range.
  71. --
  72. `time_zone`::
  73. +
  74. --
  75. (Optional, string)
  76. https://en.wikipedia.org/wiki/List_of_UTC_time_offsets[Coordinated Universal
  77. Time (UTC) offset] or
  78. https://en.wikipedia.org/wiki/List_of_tz_database_time_zones[IANA time zone]
  79. used to convert `date` values in the query to UTC.
  80. Valid values are ISO 8601 UTC offsets, such as `+01:00` or -`08:00`, and IANA
  81. time zone IDs, such as `America/Los_Angeles`.
  82. For an example query using the `time_zone` parameter, see
  83. <<range-query-time-zone,Time zone in `range` queries>>.
  84. [NOTE]
  85. ====
  86. The `time_zone` parameter does **not** affect the <<date-math,date math>> value
  87. of `now`. `now` is always the current system time in UTC.
  88. However, the `time_zone` parameter does convert dates calculated using `now` and
  89. <<date-math,date math rounding>>. For example, the `time_zone` parameter will
  90. convert a value of `now/d`.
  91. ====
  92. --
  93. `boost`::
  94. +
  95. --
  96. (Optional, float) Floating point number used to decrease or increase the
  97. <<relevance-scores,relevance scores>> of a query. Defaults to `1.0`.
  98. You can use the `boost` parameter to adjust relevance scores for searches
  99. containing two or more queries.
  100. Boost values are relative to the default value of `1.0`. A boost value between
  101. `0` and `1.0` decreases the relevance score. A value greater than `1.0`
  102. increases the relevance score.
  103. --
  104. [[range-query-notes]]
  105. ==== Notes
  106. [[ranges-on-dates]]
  107. ===== Using the `range` query with `date` fields
  108. When the `<field>` parameter is a <<date,`date`>> field datatype, you can use
  109. <<date-math,date math>> with the following parameters:
  110. * `gt`
  111. * `gte`
  112. * `lt`
  113. * `lte`
  114. For example, the following search returns documents where the `timestamp` field
  115. contains a date between today and yesterday.
  116. [source,console]
  117. ----
  118. GET _search
  119. {
  120. "query": {
  121. "range" : {
  122. "timestamp" : {
  123. "gte" : "now-1d/d",
  124. "lt" : "now/d"
  125. }
  126. }
  127. }
  128. }
  129. ----
  130. [[range-query-date-math-rounding]]
  131. ====== Date math and rounding
  132. {es} rounds <<date-math,date math>> values in parameters as follows:
  133. `gt`::
  134. +
  135. --
  136. Rounds up to the lastest millisecond.
  137. For example, `2014-11-18||/M` rounds up to `2014-11-30T23:59:59.999`, including
  138. the entire month.
  139. --
  140. `gte`::
  141. +
  142. --
  143. Rounds down to the first millisecond.
  144. For example, `2014-11-18||/M` rounds down to `2014-11-01`, excluding
  145. the entire month.
  146. --
  147. `lt`::
  148. +
  149. --
  150. Rounds down to the first millisecond.
  151. For example, `2014-11-18||/M` rounds down to `2014-11-01`, excluding
  152. the entire month.
  153. --
  154. `lte`::
  155. +
  156. --
  157. Rounds up to the lastest millisecond.
  158. For example, `2014-11-18||/M` rounds up to `2014-11-30T23:59:59.999`, including
  159. the entire month.
  160. --
  161. [[range-query-time-zone]]
  162. ===== Example query using `time_zone` parameter
  163. You can use the `time_zone` parameter to convert `date` values to UTC using a
  164. UTC offset. For example:
  165. [source,console]
  166. ----
  167. GET _search
  168. {
  169. "query": {
  170. "range" : {
  171. "timestamp" : {
  172. "time_zone": "+01:00", <1>
  173. "gte": "2015-01-01 00:00:00", <2>
  174. "lte": "now" <3>
  175. }
  176. }
  177. }
  178. }
  179. ----
  180. <1> Indicates that `date` values use a UTC offset of `+01:00`.
  181. <2> With a UTC offset of `+01:00`, {es} converts this date to
  182. `2014-12-31T23:00:00 UTC`.
  183. <3> The `time_zone` parameter does not affect the `now` value.