You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

conjunction.go 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. // Copyright (c) 2014 Couchbase, Inc.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package query
  15. import (
  16. "encoding/json"
  17. "github.com/blevesearch/bleve/index"
  18. "github.com/blevesearch/bleve/mapping"
  19. "github.com/blevesearch/bleve/search"
  20. "github.com/blevesearch/bleve/search/searcher"
  21. )
  22. type ConjunctionQuery struct {
  23. Conjuncts []Query `json:"conjuncts"`
  24. BoostVal *Boost `json:"boost,omitempty"`
  25. queryStringMode bool
  26. }
  27. // NewConjunctionQuery creates a new compound Query.
  28. // Result documents must satisfy all of the queries.
  29. func NewConjunctionQuery(conjuncts []Query) *ConjunctionQuery {
  30. return &ConjunctionQuery{
  31. Conjuncts: conjuncts,
  32. }
  33. }
  34. func (q *ConjunctionQuery) SetBoost(b float64) {
  35. boost := Boost(b)
  36. q.BoostVal = &boost
  37. }
  38. func (q *ConjunctionQuery) Boost() float64 {
  39. return q.BoostVal.Value()
  40. }
  41. func (q *ConjunctionQuery) AddQuery(aq ...Query) {
  42. for _, aaq := range aq {
  43. q.Conjuncts = append(q.Conjuncts, aaq)
  44. }
  45. }
  46. func (q *ConjunctionQuery) Searcher(i index.IndexReader, m mapping.IndexMapping, options search.SearcherOptions) (search.Searcher, error) {
  47. ss := make([]search.Searcher, 0, len(q.Conjuncts))
  48. for _, conjunct := range q.Conjuncts {
  49. sr, err := conjunct.Searcher(i, m, options)
  50. if err != nil {
  51. for _, searcher := range ss {
  52. if searcher != nil {
  53. _ = searcher.Close()
  54. }
  55. }
  56. return nil, err
  57. }
  58. if _, ok := sr.(*searcher.MatchNoneSearcher); ok && q.queryStringMode {
  59. // in query string mode, skip match none
  60. continue
  61. }
  62. ss = append(ss, sr)
  63. }
  64. if len(ss) < 1 {
  65. return searcher.NewMatchNoneSearcher(i)
  66. }
  67. return searcher.NewConjunctionSearcher(i, ss, options)
  68. }
  69. func (q *ConjunctionQuery) Validate() error {
  70. for _, q := range q.Conjuncts {
  71. if q, ok := q.(ValidatableQuery); ok {
  72. err := q.Validate()
  73. if err != nil {
  74. return err
  75. }
  76. }
  77. }
  78. return nil
  79. }
  80. func (q *ConjunctionQuery) UnmarshalJSON(data []byte) error {
  81. tmp := struct {
  82. Conjuncts []json.RawMessage `json:"conjuncts"`
  83. Boost *Boost `json:"boost,omitempty"`
  84. }{}
  85. err := json.Unmarshal(data, &tmp)
  86. if err != nil {
  87. return err
  88. }
  89. q.Conjuncts = make([]Query, len(tmp.Conjuncts))
  90. for i, term := range tmp.Conjuncts {
  91. query, err := ParseQuery(term)
  92. if err != nil {
  93. return err
  94. }
  95. q.Conjuncts[i] = query
  96. }
  97. q.BoostVal = tmp.Boost
  98. return nil
  99. }