range-query.asciidoc 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. {
  11. "range" : {
  12. "age" : {
  13. "gte" : 10,
  14. "lte" : 20,
  15. "boost" : 2.0
  16. }
  17. }
  18. }
  19. --------------------------------------------------
  20. The `range` query accepts the following parameters:
  21. [horizontal]
  22. `gte`:: Greater-than or equal to
  23. `gt`:: Greater-than
  24. `lte`:: Less-than or equal to
  25. `lt`:: Less-than
  26. `boost`:: Sets the boost value of the query, defaults to `1.0`
  27. [[ranges-on-dates]]
  28. ==== Ranges on date fields
  29. When running `range` queries on fields of type <<date,`date`>>, ranges can be
  30. specified using <<date-math>>:
  31. [source,js]
  32. --------------------------------------------------
  33. {
  34. "range" : {
  35. "date" : {
  36. "gte" : "now-1d/d",
  37. "lt" : "now/d"
  38. }
  39. }
  40. }
  41. --------------------------------------------------
  42. ===== Date math and rounding
  43. When using <<date-math,date math>> to round dates to the nearest day, month,
  44. hour, etc, the rounded dates depend on whether the ends of the ranges are
  45. inclusive or exclusive.
  46. Rounding up moves to the last millisecond of the rounding scope, and rounding
  47. down to the first millisecond of the rounding scope. For example:
  48. [horizontal]
  49. `gt`::
  50. Greater than the date rounded up: `2014-11-18||/M` becomes
  51. `2014-11-30T23:59:59.999`, ie excluding the entire month.
  52. `gte`::
  53. Greater than or equal to the date rounded down: `2014-11-18||/M` becomes
  54. `2014-11-01`, ie including the entire month.
  55. `lt`::
  56. Less than the date rounded down: `2014-11-18||/M` becomes `2014-11-01`, ie
  57. excluding the entire month.
  58. `lte`::
  59. Less than or equal to the date rounded up: `2014-11-18||/M` becomes
  60. `2014-11-30T23:59:59.999`, ie including the entire month.
  61. ===== Date format in range queries
  62. Formatted dates will be parsed using the <<mapping-date-format,`format`>>
  63. specified on the <<date,`date`>> field by default, but it can be overridden by
  64. passing the `format` parameter to the `range` query:
  65. [source,js]
  66. --------------------------------------------------
  67. {
  68. "range" : {
  69. "born" : {
  70. "gte": "01/01/2012",
  71. "lte": "2013",
  72. "format": "dd/MM/yyyy||yyyy"
  73. }
  74. }
  75. }
  76. --------------------------------------------------
  77. ===== Time zone in range queries
  78. Dates can be converted from another timezone to UTC either by specifying the
  79. time zone in the date value itself (if the <<mapping-date-format, `format`>>
  80. accepts it), or it can be specified as the `time_zone` parameter:
  81. [source,js]
  82. --------------------------------------------------
  83. {
  84. "range" : {
  85. "timestamp" : {
  86. "gte": "2015-01-01 00:00:00", <1>
  87. "lte": "now", <2>
  88. "time_zone": "+01:00"
  89. }
  90. }
  91. }
  92. --------------------------------------------------
  93. <1> This date will be converted to `2014-12-31T23:00:00 UTC`.
  94. <2> `now` is not affected by the `time_zone` parameter (dates must be stored as UTC).