search_queries_bool.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. import "fmt"
  6. // A bool query matches documents matching boolean
  7. // combinations of other queries.
  8. // For more details, see:
  9. // https://www.elastic.co/guide/en/elasticsearch/reference/5.2/query-dsl-bool-query.html
  10. type BoolQuery struct {
  11. Query
  12. mustClauses []Query
  13. mustNotClauses []Query
  14. filterClauses []Query
  15. shouldClauses []Query
  16. boost *float64
  17. disableCoord *bool
  18. minimumShouldMatch string
  19. adjustPureNegative *bool
  20. queryName string
  21. }
  22. // Creates a new bool query.
  23. func NewBoolQuery() *BoolQuery {
  24. return &BoolQuery{
  25. mustClauses: make([]Query, 0),
  26. mustNotClauses: make([]Query, 0),
  27. filterClauses: make([]Query, 0),
  28. shouldClauses: make([]Query, 0),
  29. }
  30. }
  31. func (q *BoolQuery) Must(queries ...Query) *BoolQuery {
  32. q.mustClauses = append(q.mustClauses, queries...)
  33. return q
  34. }
  35. func (q *BoolQuery) MustNot(queries ...Query) *BoolQuery {
  36. q.mustNotClauses = append(q.mustNotClauses, queries...)
  37. return q
  38. }
  39. func (q *BoolQuery) Filter(filters ...Query) *BoolQuery {
  40. q.filterClauses = append(q.filterClauses, filters...)
  41. return q
  42. }
  43. func (q *BoolQuery) Should(queries ...Query) *BoolQuery {
  44. q.shouldClauses = append(q.shouldClauses, queries...)
  45. return q
  46. }
  47. func (q *BoolQuery) Boost(boost float64) *BoolQuery {
  48. q.boost = &boost
  49. return q
  50. }
  51. func (q *BoolQuery) DisableCoord(disableCoord bool) *BoolQuery {
  52. q.disableCoord = &disableCoord
  53. return q
  54. }
  55. func (q *BoolQuery) MinimumShouldMatch(minimumShouldMatch string) *BoolQuery {
  56. q.minimumShouldMatch = minimumShouldMatch
  57. return q
  58. }
  59. func (q *BoolQuery) MinimumNumberShouldMatch(minimumNumberShouldMatch int) *BoolQuery {
  60. q.minimumShouldMatch = fmt.Sprintf("%d", minimumNumberShouldMatch)
  61. return q
  62. }
  63. func (q *BoolQuery) AdjustPureNegative(adjustPureNegative bool) *BoolQuery {
  64. q.adjustPureNegative = &adjustPureNegative
  65. return q
  66. }
  67. func (q *BoolQuery) QueryName(queryName string) *BoolQuery {
  68. q.queryName = queryName
  69. return q
  70. }
  71. // Creates the query source for the bool query.
  72. func (q *BoolQuery) Source() (interface{}, error) {
  73. // {
  74. // "bool" : {
  75. // "must" : {
  76. // "term" : { "user" : "kimchy" }
  77. // },
  78. // "must_not" : {
  79. // "range" : {
  80. // "age" : { "from" : 10, "to" : 20 }
  81. // }
  82. // },
  83. // "filter" : [
  84. // ...
  85. // ]
  86. // "should" : [
  87. // {
  88. // "term" : { "tag" : "wow" }
  89. // },
  90. // {
  91. // "term" : { "tag" : "elasticsearch" }
  92. // }
  93. // ],
  94. // "minimum_number_should_match" : 1,
  95. // "boost" : 1.0
  96. // }
  97. // }
  98. query := make(map[string]interface{})
  99. boolClause := make(map[string]interface{})
  100. query["bool"] = boolClause
  101. // must
  102. if len(q.mustClauses) == 1 {
  103. src, err := q.mustClauses[0].Source()
  104. if err != nil {
  105. return nil, err
  106. }
  107. boolClause["must"] = src
  108. } else if len(q.mustClauses) > 1 {
  109. var clauses []interface{}
  110. for _, subQuery := range q.mustClauses {
  111. src, err := subQuery.Source()
  112. if err != nil {
  113. return nil, err
  114. }
  115. clauses = append(clauses, src)
  116. }
  117. boolClause["must"] = clauses
  118. }
  119. // must_not
  120. if len(q.mustNotClauses) == 1 {
  121. src, err := q.mustNotClauses[0].Source()
  122. if err != nil {
  123. return nil, err
  124. }
  125. boolClause["must_not"] = src
  126. } else if len(q.mustNotClauses) > 1 {
  127. var clauses []interface{}
  128. for _, subQuery := range q.mustNotClauses {
  129. src, err := subQuery.Source()
  130. if err != nil {
  131. return nil, err
  132. }
  133. clauses = append(clauses, src)
  134. }
  135. boolClause["must_not"] = clauses
  136. }
  137. // filter
  138. if len(q.filterClauses) == 1 {
  139. src, err := q.filterClauses[0].Source()
  140. if err != nil {
  141. return nil, err
  142. }
  143. boolClause["filter"] = src
  144. } else if len(q.filterClauses) > 1 {
  145. var clauses []interface{}
  146. for _, subQuery := range q.filterClauses {
  147. src, err := subQuery.Source()
  148. if err != nil {
  149. return nil, err
  150. }
  151. clauses = append(clauses, src)
  152. }
  153. boolClause["filter"] = clauses
  154. }
  155. // should
  156. if len(q.shouldClauses) == 1 {
  157. src, err := q.shouldClauses[0].Source()
  158. if err != nil {
  159. return nil, err
  160. }
  161. boolClause["should"] = src
  162. } else if len(q.shouldClauses) > 1 {
  163. var clauses []interface{}
  164. for _, subQuery := range q.shouldClauses {
  165. src, err := subQuery.Source()
  166. if err != nil {
  167. return nil, err
  168. }
  169. clauses = append(clauses, src)
  170. }
  171. boolClause["should"] = clauses
  172. }
  173. if q.boost != nil {
  174. boolClause["boost"] = *q.boost
  175. }
  176. if q.disableCoord != nil {
  177. boolClause["disable_coord"] = *q.disableCoord
  178. }
  179. if q.minimumShouldMatch != "" {
  180. boolClause["minimum_should_match"] = q.minimumShouldMatch
  181. }
  182. if q.adjustPureNegative != nil {
  183. boolClause["adjust_pure_negative"] = *q.adjustPureNegative
  184. }
  185. if q.queryName != "" {
  186. boolClause["_name"] = q.queryName
  187. }
  188. return query, nil
  189. }