range-query.asciidoc 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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 day component with the
  54. {wikipedia}/Unix_time[Unix epoch], which is January 1st, 1970.
  55. For example, if the `format` value is `dd`, {es} converts a `gte` value of `22`
  56. to `1970-01-22T00:00:00.000Z`. This date uses `22` as the day of the month but
  57. uses the Unix epoch's year (`1970`) and month (`01`).
  58. ====
  59. --
  60. [[querying-range-fields]]
  61. `relation`::
  62. +
  63. --
  64. (Optional, string) Indicates how the range query matches values for `range`
  65. fields. Valid values are:
  66. `INTERSECTS` (Default)::
  67. Matches documents with a range field value that intersects the query's range.
  68. `CONTAINS`::
  69. Matches documents with a range field value that entirely contains the query's range.
  70. `WITHIN`::
  71. Matches documents with a range field value entirely within the query's range.
  72. --
  73. `time_zone`::
  74. +
  75. --
  76. (Optional, string)
  77. {wikipedia}/List_of_UTC_time_offsets[Coordinated Universal
  78. Time (UTC) offset] or
  79. {wikipedia}/List_of_tz_database_time_zones[IANA time zone]
  80. used to convert `date` values in the query to UTC.
  81. Valid values are ISO 8601 UTC offsets, such as `+01:00` or -`08:00`, and IANA
  82. time zone IDs, such as `America/Los_Angeles`.
  83. For an example query using the `time_zone` parameter, see
  84. <<range-query-time-zone,Time zone in `range` queries>>.
  85. [NOTE]
  86. ====
  87. The `time_zone` parameter does **not** affect the <<date-math,date math>> value
  88. of `now`. `now` is always the current system time in UTC.
  89. However, the `time_zone` parameter does convert dates calculated using `now` and
  90. <<date-math,date math rounding>>. For example, the `time_zone` parameter will
  91. convert a value of `now/d`.
  92. ====
  93. --
  94. `boost`::
  95. +
  96. --
  97. (Optional, float) Floating point number used to decrease or increase the
  98. <<relevance-scores,relevance scores>> of a query. Defaults to `1.0`.
  99. You can use the `boost` parameter to adjust relevance scores for searches
  100. containing two or more queries.
  101. Boost values are relative to the default value of `1.0`. A boost value between
  102. `0` and `1.0` decreases the relevance score. A value greater than `1.0`
  103. increases the relevance score.
  104. --
  105. [[range-query-notes]]
  106. ==== Notes
  107. [[ranges-on-text-and-keyword]]
  108. ===== Using the `range` query with `text` and `keyword` fields
  109. Range queries on <<text, `text`>> or <<keyword, `keyword`>> fields will not be executed if
  110. <<query-dsl-allow-expensive-queries, `search.allow_expensive_queries`>> is set to false.
  111. [[ranges-on-dates]]
  112. ===== Using the `range` query with `date` fields
  113. When the `<field>` parameter is a <<date,`date`>> field data type, you can use
  114. <<date-math,date math>> with the following parameters:
  115. * `gt`
  116. * `gte`
  117. * `lt`
  118. * `lte`
  119. For example, the following search returns documents where the `timestamp` field
  120. contains a date between today and yesterday.
  121. [source,console]
  122. ----
  123. GET /_search
  124. {
  125. "query": {
  126. "range": {
  127. "timestamp": {
  128. "gte": "now-1d/d",
  129. "lt": "now/d"
  130. }
  131. }
  132. }
  133. }
  134. ----
  135. [[range-query-date-math-rounding]]
  136. ====== Date math and rounding
  137. {es} rounds <<date-math,date math>> values in parameters as follows:
  138. `gt`::
  139. +
  140. --
  141. Rounds up to the first millisecond not covered by the rounded date.
  142. For example, `2014-11-18||/M` rounds up to `2014-12-01T00:00:00.000`, excluding
  143. the entire month of November.
  144. --
  145. `gte`::
  146. +
  147. --
  148. Rounds down to the first millisecond.
  149. For example, `2014-11-18||/M` rounds down to `2014-11-01T00:00:00.000`, including
  150. the entire month.
  151. --
  152. `lt`::
  153. +
  154. --
  155. Rounds down to the last millisecond before the rounded value.
  156. For example, `2014-11-18||/M` rounds down to `2014-10-31T23:59:59.999`, excluding
  157. the entire month of November.
  158. --
  159. `lte`::
  160. +
  161. --
  162. Rounds up to the latest millisecond in the rounding interval.
  163. For example, `2014-11-18||/M` rounds up to `2014-11-30T23:59:59.999`, including
  164. the entire month.
  165. --
  166. [[range-query-time-zone]]
  167. ===== Example query using `time_zone` parameter
  168. You can use the `time_zone` parameter to convert `date` values to UTC using a
  169. UTC offset. For example:
  170. [source,console]
  171. ----
  172. GET /_search
  173. {
  174. "query": {
  175. "range": {
  176. "timestamp": {
  177. "time_zone": "+01:00", <1>
  178. "gte": "2020-01-01T00:00:00", <2>
  179. "lte": "now" <3>
  180. }
  181. }
  182. }
  183. }
  184. ----
  185. // TEST[continued]
  186. <1> Indicates that `date` values use a UTC offset of `+01:00`.
  187. <2> With a UTC offset of `+01:00`, {es} converts this date to
  188. `2019-12-31T23:00:00 UTC`.
  189. <3> The `time_zone` parameter does not affect the `now` value.