search_queries_bool.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. import (
  6. "encoding/json"
  7. )
  8. // A bool query matches documents matching boolean
  9. // combinations of other queries.
  10. // For more details, see:
  11. // http://www.elasticsearch.org/guide/reference/query-dsl/bool-query.html
  12. type BoolQuery struct {
  13. Query
  14. mustClauses []Query
  15. shouldClauses []Query
  16. mustNotClauses []Query
  17. boost *float32
  18. disableCoord *bool
  19. minimumShouldMatch string
  20. adjustPureNegative *bool
  21. queryName string
  22. QueryStrings string
  23. }
  24. // Creates a new bool query.
  25. func NewBoolQuery() BoolQuery {
  26. q := BoolQuery{
  27. mustClauses: make([]Query, 0),
  28. shouldClauses: make([]Query, 0),
  29. mustNotClauses: make([]Query, 0),
  30. }
  31. return q
  32. }
  33. func (q BoolQuery) Must(queries ...Query) BoolQuery {
  34. q.mustClauses = append(q.mustClauses, queries...)
  35. return q
  36. }
  37. func (q BoolQuery) MustNot(queries ...Query) BoolQuery {
  38. q.mustNotClauses = append(q.mustNotClauses, queries...)
  39. return q
  40. }
  41. func (q BoolQuery) Should(queries ...Query) BoolQuery {
  42. q.shouldClauses = append(q.shouldClauses, queries...)
  43. return q
  44. }
  45. func (q BoolQuery) Boost(boost float32) BoolQuery {
  46. q.boost = &boost
  47. return q
  48. }
  49. func (q BoolQuery) DisableCoord(disableCoord bool) BoolQuery {
  50. q.disableCoord = &disableCoord
  51. return q
  52. }
  53. func (q BoolQuery) MinimumShouldMatch(minimumShouldMatch string) BoolQuery {
  54. q.minimumShouldMatch = minimumShouldMatch
  55. return q
  56. }
  57. func (q BoolQuery) AdjustPureNegative(adjustPureNegative bool) BoolQuery {
  58. q.adjustPureNegative = &adjustPureNegative
  59. return q
  60. }
  61. func (q BoolQuery) QueryName(queryName string) BoolQuery {
  62. q.queryName = queryName
  63. return q
  64. }
  65. // Creates the query source for the bool query.
  66. func (q BoolQuery) Source() interface{} {
  67. // {
  68. // "bool" : {
  69. // "must" : {
  70. // "term" : { "user" : "kimchy" }
  71. // },
  72. // "must_not" : {
  73. // "range" : {
  74. // "age" : { "from" : 10, "to" : 20 }
  75. // }
  76. // },
  77. // "should" : [
  78. // {
  79. // "term" : { "tag" : "wow" }
  80. // },
  81. // {
  82. // "term" : { "tag" : "elasticsearch" }
  83. // }
  84. // ],
  85. // "minimum_number_should_match" : 1,
  86. // "boost" : 1.0
  87. // }
  88. // }
  89. if q.QueryStrings != "" {
  90. m := make(map[string]interface{})
  91. json.Unmarshal([]byte(q.QueryStrings), &m)
  92. return m["query"]
  93. }
  94. query := make(map[string]interface{})
  95. boolClause := make(map[string]interface{})
  96. query["bool"] = boolClause
  97. // must
  98. if len(q.mustClauses) == 1 {
  99. boolClause["must"] = q.mustClauses[0].Source()
  100. } else if len(q.mustClauses) > 1 {
  101. clauses := make([]interface{}, 0)
  102. for _, subQuery := range q.mustClauses {
  103. clauses = append(clauses, subQuery.Source())
  104. }
  105. boolClause["must"] = clauses
  106. }
  107. // must_not
  108. if len(q.mustNotClauses) == 1 {
  109. boolClause["must_not"] = q.mustNotClauses[0].Source()
  110. } else if len(q.mustNotClauses) > 1 {
  111. clauses := make([]interface{}, 0)
  112. for _, subQuery := range q.mustNotClauses {
  113. clauses = append(clauses, subQuery.Source())
  114. }
  115. boolClause["must_not"] = clauses
  116. }
  117. // should
  118. if len(q.shouldClauses) == 1 {
  119. boolClause["should"] = q.shouldClauses[0].Source()
  120. } else if len(q.shouldClauses) > 1 {
  121. clauses := make([]interface{}, 0)
  122. for _, subQuery := range q.shouldClauses {
  123. clauses = append(clauses, subQuery.Source())
  124. }
  125. boolClause["should"] = clauses
  126. }
  127. if q.boost != nil {
  128. boolClause["boost"] = *q.boost
  129. }
  130. if q.disableCoord != nil {
  131. boolClause["disable_coord"] = *q.disableCoord
  132. }
  133. if q.minimumShouldMatch != "" {
  134. boolClause["minimum_should_match"] = q.minimumShouldMatch
  135. }
  136. if q.adjustPureNegative != nil {
  137. boolClause["adjust_pure_negative"] = *q.adjustPureNegative
  138. }
  139. if q.queryName != "" {
  140. boolClause["_name"] = q.queryName
  141. }
  142. return query
  143. }