range-query.asciidoc 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. [[query-dsl-range-query]]
  2. === Range Query
  3. Matches documents with fields that have terms within a certain range.
  4. The type of the Lucene query depends on the field type, for `string`
  5. fields, the `TermRangeQuery`, while for number/date fields, the query is
  6. a `NumericRangeQuery`. The following example returns all documents where
  7. `age` is 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. // AUTOSENSE
  24. The `range` query accepts the following parameters:
  25. [horizontal]
  26. `gte`:: Greater-than or equal to
  27. `gt`:: Greater-than
  28. `lte`:: Less-than or equal to
  29. `lt`:: Less-than
  30. `boost`:: Sets the boost value of the query, defaults to `1.0`
  31. [[ranges-on-dates]]
  32. ==== Ranges on date fields
  33. When running `range` queries on fields of type <<date,`date`>>, ranges can be
  34. specified using <<date-math>>:
  35. [source,js]
  36. --------------------------------------------------
  37. GET _search
  38. {
  39. "query": {
  40. "range" : {
  41. "date" : {
  42. "gte" : "now-1d/d",
  43. "lt" : "now/d"
  44. }
  45. }
  46. }
  47. }
  48. --------------------------------------------------
  49. // AUTOSENSE
  50. ===== Date math and rounding
  51. When using <<date-math,date math>> to round dates to the nearest day, month,
  52. hour, etc, the rounded dates depend on whether the ends of the ranges are
  53. inclusive or exclusive.
  54. Rounding up moves to the last millisecond of the rounding scope, and rounding
  55. down to the first millisecond of the rounding scope. For example:
  56. [horizontal]
  57. `gt`::
  58. Greater than the date rounded up: `2014-11-18||/M` becomes
  59. `2014-11-30T23:59:59.999`, ie excluding the entire month.
  60. `gte`::
  61. Greater than or equal to the date rounded down: `2014-11-18||/M` becomes
  62. `2014-11-01`, ie including the entire month.
  63. `lt`::
  64. Less than the date rounded down: `2014-11-18||/M` becomes `2014-11-01`, ie
  65. excluding the entire month.
  66. `lte`::
  67. Less than or equal to the date rounded up: `2014-11-18||/M` becomes
  68. `2014-11-30T23:59:59.999`, ie including the entire month.
  69. ===== Date format in range queries
  70. Formatted dates will be parsed using the <<mapping-date-format,`format`>>
  71. specified on the <<date,`date`>> field by default, but it can be overridden by
  72. passing the `format` parameter to the `range` query:
  73. [source,js]
  74. --------------------------------------------------
  75. GET _search
  76. {
  77. "query":
  78. "range" : {
  79. "born" : {
  80. "gte": "01/01/2012",
  81. "lte": "2013",
  82. "format": "dd/MM/yyyy||yyyy"
  83. }
  84. }
  85. }
  86. --------------------------------------------------
  87. ===== Time zone in range queries
  88. Dates can be converted from another timezone to UTC either by specifying the
  89. time zone in the date value itself (if the <<mapping-date-format, `format`>>
  90. accepts it), or it can be specified as the `time_zone` parameter:
  91. [source,js]
  92. --------------------------------------------------
  93. GET _search
  94. {
  95. "query":
  96. {
  97. "range" : {
  98. "timestamp" : {
  99. "gte": "2015-01-01 00:00:00", <1>
  100. "lte": "now", <2>
  101. "time_zone": "+01:00"
  102. }
  103. }
  104. }
  105. }
  106. --------------------------------------------------
  107. // AUTOSENSE
  108. <1> This date will be converted to `2014-12-31T23:00:00 UTC`.
  109. <2> `now` is not affected by the `time_zone` parameter (dates must be stored as UTC).