range-query.asciidoc 5.0 KB

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