range-query.asciidoc 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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,js]
  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. // CONSOLE
  27. [[range-query-top-level-params]]
  28. ==== Top-level parameters for `range`
  29. `<field>`::
  30. +
  31. --
  32. (Required, object) Field you wish to search.
  33. --
  34. [[range-query-field-params]]
  35. ==== Parameters for `<field>`
  36. `gt`::
  37. (Optional) Greater than.
  38. `gte`::
  39. (Optional) Greater than or equal to.
  40. `lt`::
  41. (Optional) Less than.
  42. `lte`::
  43. (Optional) Less than or equal to.
  44. `format`::
  45. +
  46. --
  47. (Optional, string) Date format used to convert `date` values in the query.
  48. By default, {es} uses the <<mapping-date-format,date `format`>> provided in the
  49. `<field>`'s mapping. This value overrides that mapping format.
  50. For valid syntax, see <<mapping-date-format,`format`>>.
  51. [WARNING]
  52. ====
  53. If a `format` and `date` value are incomplete, {es} replaces any missing year,
  54. month, or date component with the start of
  55. https://en.wikipedia.org/wiki/Unix_time[Unix time], which is January 1st, 1970.
  56. For example, if the `format` value is `dd`, {es} converts a `gte` value of `10`
  57. to `1970-01-10T00:00:00.000Z`.
  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. https://en.wikipedia.org/wiki/List_of_UTC_time_offsets[Coordinated Universal
  78. Time (UTC) offset] or
  79. https://en.wikipedia.org/wiki/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. <<query-filter-context, 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-dates]]
  108. ===== Using the `range` query with `date` fields
  109. When the `<field>` parameter is a <<date,`date`>> field datatype, you can use
  110. <<date-math,date math>> with the following parameters:
  111. * `gt`
  112. * `gte`
  113. * `lt`
  114. * `lte`
  115. For example, the following search returns documents where the `timestamp` field
  116. contains a date between today and yesterday.
  117. [source,js]
  118. ----
  119. GET _search
  120. {
  121. "query": {
  122. "range" : {
  123. "timestamp" : {
  124. "gte" : "now-1d/d",
  125. "lt" : "now/d"
  126. }
  127. }
  128. }
  129. }
  130. ----
  131. // CONSOLE
  132. [[range-query-date-math-rounding]]
  133. ====== Date math and rounding
  134. {es} rounds <<date-math,date math>> values in parameters as follows:
  135. `gt`::
  136. +
  137. --
  138. Rounds up to the lastest millisecond.
  139. For example, `2014-11-18||/M` rounds up to `2014-11-30T23:59:59.999`, including
  140. the entire month.
  141. --
  142. `gte`::
  143. +
  144. --
  145. Rounds down to the first millisecond.
  146. For example, `2014-11-18||/M` rounds down to `2014-11-01`, excluding
  147. the entire month.
  148. --
  149. `lt`::
  150. +
  151. --
  152. Rounds down to the first millisecond.
  153. For example, `2014-11-18||/M` rounds down to `2014-11-01`, excluding
  154. the entire month.
  155. --
  156. `lte`::
  157. +
  158. --
  159. Rounds up to the lastest millisecond.
  160. For example, `2014-11-18||/M` rounds up to `2014-11-30T23:59:59.999`, including
  161. the entire month.
  162. --
  163. [[range-query-time-zone]]
  164. ===== Example query using `time_zone` parameter
  165. You can use the `time_zone` parameter to convert `date` values to UTC using a
  166. UTC offset. For example:
  167. [source,js]
  168. ----
  169. GET _search
  170. {
  171. "query": {
  172. "range" : {
  173. "timestamp" : {
  174. "time_zone": "+01:00", <1>
  175. "gte": "2015-01-01 00:00:00", <2>
  176. "lte": "now" <3>
  177. }
  178. }
  179. }
  180. }
  181. ----
  182. // CONSOLE
  183. <1> Indicates that `date` values use a UTC offset of `+01:00`.
  184. <2> With a UTC offset of `+01:00`, {es} converts this date to
  185. `2014-12-31T23:00:00 UTC`.
  186. <3> The `time_zone` parameter does not affect the `now` value.