range-query.asciidoc 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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-text-and-keyword]]
  107. ===== Using the `range` query with `text` and `keyword` fields
  108. Range queries on <<text, `text`>> or <<keyword, `keyword`>> files will not be executed if
  109. <<query-dsl-allow-expensive-queries, `search.allow_expensive_queries`>> is set to false.
  110. [[ranges-on-dates]]
  111. ===== Using the `range` query with `date` fields
  112. When the `<field>` parameter is a <<date,`date`>> field data type, you can use
  113. <<date-math,date math>> with the following parameters:
  114. * `gt`
  115. * `gte`
  116. * `lt`
  117. * `lte`
  118. For example, the following search returns documents where the `timestamp` field
  119. contains a date between today and yesterday.
  120. [source,console]
  121. ----
  122. GET /_search
  123. {
  124. "query": {
  125. "range": {
  126. "timestamp": {
  127. "gte": "now-1d/d",
  128. "lt": "now/d"
  129. }
  130. }
  131. }
  132. }
  133. ----
  134. [[range-query-date-math-rounding]]
  135. ====== Date math and rounding
  136. {es} rounds <<date-math,date math>> values in parameters as follows:
  137. `gt`::
  138. +
  139. --
  140. Rounds up to the first millisecond not covered by the rounded date.
  141. For example, `2014-11-18||/M` rounds up to `2014-12-01T00:00:00.000`, excluding
  142. the entire month of November.
  143. --
  144. `gte`::
  145. +
  146. --
  147. Rounds down to the first millisecond.
  148. For example, `2014-11-18||/M` rounds down to `2014-11-01T00:00:00.000`, including
  149. the entire month.
  150. --
  151. `lt`::
  152. +
  153. --
  154. Rounds down to the last millisecond before the rounded value.
  155. For example, `2014-11-18||/M` rounds down to `2014-10-31T23:59:59.999`, excluding
  156. the entire month of November.
  157. --
  158. `lte`::
  159. +
  160. --
  161. Rounds up to the latest millisecond in the rounding interval.
  162. For example, `2014-11-18||/M` rounds up to `2014-11-30T23:59:59.999`, including
  163. the entire month.
  164. --
  165. [[range-query-time-zone]]
  166. ===== Example query using `time_zone` parameter
  167. You can use the `time_zone` parameter to convert `date` values to UTC using a
  168. UTC offset. For example:
  169. [source,console]
  170. ----
  171. GET /_search
  172. {
  173. "query": {
  174. "range": {
  175. "timestamp": {
  176. "time_zone": "+01:00", <1>
  177. "gte": "2020-01-01T00:00:00", <2>
  178. "lte": "now" <3>
  179. }
  180. }
  181. }
  182. }
  183. ----
  184. // TEST[continued]
  185. <1> Indicates that `date` values use a UTC offset of `+01:00`.
  186. <2> With a UTC offset of `+01:00`, {es} converts this date to
  187. `2019-12-31T23:00:00 UTC`.
  188. <3> The `time_zone` parameter does not affect the `now` value.