range-query.asciidoc 5.9 KB

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