search_queries_range.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. // Copyright 2012-2015 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. // Matches documents with fields that have terms within a certain range.
  6. // For details, see:
  7. // http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-range-query.html
  8. type RangeQuery struct {
  9. Query
  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. }
  19. func NewRangeQuery(name string) RangeQuery {
  20. q := RangeQuery{name: name, includeLower: true, includeUpper: true}
  21. return q
  22. }
  23. func (f RangeQuery) TimeZone(timeZone string) RangeQuery {
  24. f.timeZone = timeZone
  25. return f
  26. }
  27. func (q RangeQuery) From(from interface{}) RangeQuery {
  28. q.from = &from
  29. return q
  30. }
  31. func (q RangeQuery) Gt(from interface{}) RangeQuery {
  32. q.from = &from
  33. q.includeLower = false
  34. return q
  35. }
  36. func (q RangeQuery) Gte(from interface{}) RangeQuery {
  37. q.from = &from
  38. q.includeLower = true
  39. return q
  40. }
  41. func (q RangeQuery) To(to interface{}) RangeQuery {
  42. q.to = &to
  43. return q
  44. }
  45. func (q RangeQuery) Lt(to interface{}) RangeQuery {
  46. q.to = &to
  47. q.includeUpper = false
  48. return q
  49. }
  50. func (q RangeQuery) Lte(to interface{}) RangeQuery {
  51. q.to = &to
  52. q.includeUpper = true
  53. return q
  54. }
  55. func (q RangeQuery) IncludeLower(includeLower bool) RangeQuery {
  56. q.includeLower = includeLower
  57. return q
  58. }
  59. func (q RangeQuery) IncludeUpper(includeUpper bool) RangeQuery {
  60. q.includeUpper = includeUpper
  61. return q
  62. }
  63. func (q RangeQuery) Boost(boost float64) RangeQuery {
  64. q.boost = &boost
  65. return q
  66. }
  67. func (q RangeQuery) QueryName(queryName string) RangeQuery {
  68. q.queryName = queryName
  69. return q
  70. }
  71. func (q RangeQuery) Source() interface{} {
  72. // {
  73. // "range" : {
  74. // "name" : {
  75. // "..." : "..."
  76. // }
  77. // }
  78. // }
  79. source := make(map[string]interface{})
  80. rangeQ := make(map[string]interface{})
  81. source["range"] = rangeQ
  82. params := make(map[string]interface{})
  83. rangeQ[q.name] = params
  84. params["from"] = q.from
  85. params["to"] = q.to
  86. if q.timeZone != "" {
  87. params["time_zone"] = q.timeZone
  88. }
  89. params["include_lower"] = q.includeLower
  90. params["include_upper"] = q.includeUpper
  91. if q.boost != nil {
  92. rangeQ["boost"] = *q.boost
  93. }
  94. if q.queryName != "" {
  95. rangeQ["_name"] = q.queryName
  96. }
  97. return source
  98. }