search_queries_range.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. // Copyright 2012-present Oliver Eilhard. All rights reserved.
  2. // Use of this source code is governed by a MIT-license.
  3. // See http://olivere.mit-license.org/license.txt for details.
  4. package elastic
  5. // RangeQuery matches documents with fields that have terms within a certain range.
  6. //
  7. // For details, see
  8. // https://www.elastic.co/guide/en/elasticsearch/reference/5.2/query-dsl-range-query.html
  9. type RangeQuery struct {
  10. name string
  11. from interface{}
  12. to interface{}
  13. timeZone string
  14. includeLower bool
  15. includeUpper bool
  16. boost *float64
  17. queryName string
  18. format string
  19. relation string
  20. }
  21. // NewRangeQuery creates and initializes a new RangeQuery.
  22. func NewRangeQuery(name string) *RangeQuery {
  23. return &RangeQuery{name: name, includeLower: true, includeUpper: true}
  24. }
  25. // From indicates the from part of the RangeQuery.
  26. // Use nil to indicate an unbounded from part.
  27. func (q *RangeQuery) From(from interface{}) *RangeQuery {
  28. q.from = from
  29. return q
  30. }
  31. // Gt indicates a greater-than value for the from part.
  32. // Use nil to indicate an unbounded from part.
  33. func (q *RangeQuery) Gt(from interface{}) *RangeQuery {
  34. q.from = from
  35. q.includeLower = false
  36. return q
  37. }
  38. // Gte indicates a greater-than-or-equal value for the from part.
  39. // Use nil to indicate an unbounded from part.
  40. func (q *RangeQuery) Gte(from interface{}) *RangeQuery {
  41. q.from = from
  42. q.includeLower = true
  43. return q
  44. }
  45. // To indicates the to part of the RangeQuery.
  46. // Use nil to indicate an unbounded to part.
  47. func (q *RangeQuery) To(to interface{}) *RangeQuery {
  48. q.to = to
  49. return q
  50. }
  51. // Lt indicates a less-than value for the to part.
  52. // Use nil to indicate an unbounded to part.
  53. func (q *RangeQuery) Lt(to interface{}) *RangeQuery {
  54. q.to = to
  55. q.includeUpper = false
  56. return q
  57. }
  58. // Lte indicates a less-than-or-equal value for the to part.
  59. // Use nil to indicate an unbounded to part.
  60. func (q *RangeQuery) Lte(to interface{}) *RangeQuery {
  61. q.to = to
  62. q.includeUpper = true
  63. return q
  64. }
  65. // IncludeLower indicates whether the lower bound should be included or not.
  66. // Defaults to true.
  67. func (q *RangeQuery) IncludeLower(includeLower bool) *RangeQuery {
  68. q.includeLower = includeLower
  69. return q
  70. }
  71. // IncludeUpper indicates whether the upper bound should be included or not.
  72. // Defaults to true.
  73. func (q *RangeQuery) IncludeUpper(includeUpper bool) *RangeQuery {
  74. q.includeUpper = includeUpper
  75. return q
  76. }
  77. // Boost sets the boost for this query.
  78. func (q *RangeQuery) Boost(boost float64) *RangeQuery {
  79. q.boost = &boost
  80. return q
  81. }
  82. // QueryName sets the query name for the filter that can be used when
  83. // searching for matched_filters per hit.
  84. func (q *RangeQuery) QueryName(queryName string) *RangeQuery {
  85. q.queryName = queryName
  86. return q
  87. }
  88. // TimeZone is used for date fields. In that case, we can adjust the
  89. // from/to fields using a timezone.
  90. func (q *RangeQuery) TimeZone(timeZone string) *RangeQuery {
  91. q.timeZone = timeZone
  92. return q
  93. }
  94. // Format is used for date fields. In that case, we can set the format
  95. // to be used instead of the mapper format.
  96. func (q *RangeQuery) Format(format string) *RangeQuery {
  97. q.format = format
  98. return q
  99. }
  100. // Relation is used for range fields. which can be one of
  101. // "within", "contains", "intersects" (default) and "disjoint".
  102. func (q *RangeQuery) Relation(relation string) *RangeQuery {
  103. q.relation = relation
  104. return q
  105. }
  106. // Source returns JSON for the query.
  107. func (q *RangeQuery) Source() (interface{}, error) {
  108. source := make(map[string]interface{})
  109. rangeQ := make(map[string]interface{})
  110. source["range"] = rangeQ
  111. params := make(map[string]interface{})
  112. rangeQ[q.name] = params
  113. params["from"] = q.from
  114. params["to"] = q.to
  115. if q.timeZone != "" {
  116. params["time_zone"] = q.timeZone
  117. }
  118. if q.format != "" {
  119. params["format"] = q.format
  120. }
  121. if q.relation != "" {
  122. params["relation"] = q.relation
  123. }
  124. if q.boost != nil {
  125. params["boost"] = *q.boost
  126. }
  127. params["include_lower"] = q.includeLower
  128. params["include_upper"] = q.includeUpper
  129. if q.queryName != "" {
  130. rangeQ["_name"] = q.queryName
  131. }
  132. return source, nil
  133. }