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.

disjunction.go 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. "fmt"
  18. "github.com/blevesearch/bleve/index"
  19. "github.com/blevesearch/bleve/mapping"
  20. "github.com/blevesearch/bleve/search"
  21. "github.com/blevesearch/bleve/search/searcher"
  22. )
  23. type DisjunctionQuery struct {
  24. Disjuncts []Query `json:"disjuncts"`
  25. BoostVal *Boost `json:"boost,omitempty"`
  26. Min float64 `json:"min"`
  27. queryStringMode bool
  28. }
  29. // NewDisjunctionQuery creates a new compound Query.
  30. // Result documents satisfy at least one Query.
  31. func NewDisjunctionQuery(disjuncts []Query) *DisjunctionQuery {
  32. return &DisjunctionQuery{
  33. Disjuncts: disjuncts,
  34. }
  35. }
  36. func (q *DisjunctionQuery) SetBoost(b float64) {
  37. boost := Boost(b)
  38. q.BoostVal = &boost
  39. }
  40. func (q *DisjunctionQuery) Boost() float64 {
  41. return q.BoostVal.Value()
  42. }
  43. func (q *DisjunctionQuery) AddQuery(aq ...Query) {
  44. for _, aaq := range aq {
  45. q.Disjuncts = append(q.Disjuncts, aaq)
  46. }
  47. }
  48. func (q *DisjunctionQuery) SetMin(m float64) {
  49. q.Min = m
  50. }
  51. func (q *DisjunctionQuery) Searcher(i index.IndexReader, m mapping.IndexMapping, options search.SearcherOptions) (search.Searcher, error) {
  52. ss := make([]search.Searcher, 0, len(q.Disjuncts))
  53. for _, disjunct := range q.Disjuncts {
  54. sr, err := disjunct.Searcher(i, m, options)
  55. if err != nil {
  56. for _, searcher := range ss {
  57. if searcher != nil {
  58. _ = searcher.Close()
  59. }
  60. }
  61. return nil, err
  62. }
  63. if _, ok := sr.(*searcher.MatchNoneSearcher); ok && q.queryStringMode {
  64. // in query string mode, skip match none
  65. continue
  66. }
  67. ss = append(ss, sr)
  68. }
  69. if len(ss) < 1 {
  70. return searcher.NewMatchNoneSearcher(i)
  71. }
  72. return searcher.NewDisjunctionSearcher(i, ss, q.Min, options)
  73. }
  74. func (q *DisjunctionQuery) Validate() error {
  75. if int(q.Min) > len(q.Disjuncts) {
  76. return fmt.Errorf("disjunction query has fewer than the minimum number of clauses to satisfy")
  77. }
  78. for _, q := range q.Disjuncts {
  79. if q, ok := q.(ValidatableQuery); ok {
  80. err := q.Validate()
  81. if err != nil {
  82. return err
  83. }
  84. }
  85. }
  86. return nil
  87. }
  88. func (q *DisjunctionQuery) UnmarshalJSON(data []byte) error {
  89. tmp := struct {
  90. Disjuncts []json.RawMessage `json:"disjuncts"`
  91. Boost *Boost `json:"boost,omitempty"`
  92. Min float64 `json:"min"`
  93. }{}
  94. err := json.Unmarshal(data, &tmp)
  95. if err != nil {
  96. return err
  97. }
  98. q.Disjuncts = make([]Query, len(tmp.Disjuncts))
  99. for i, term := range tmp.Disjuncts {
  100. query, err := ParseQuery(term)
  101. if err != nil {
  102. return err
  103. }
  104. q.Disjuncts[i] = query
  105. }
  106. q.BoostVal = tmp.Boost
  107. q.Min = tmp.Min
  108. return nil
  109. }