本站源代码
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.

131 lines
3.3KB

  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,
  52. options search.SearcherOptions) (search.Searcher, error) {
  53. ss := make([]search.Searcher, 0, len(q.Disjuncts))
  54. for _, disjunct := range q.Disjuncts {
  55. sr, err := disjunct.Searcher(i, m, options)
  56. if err != nil {
  57. for _, searcher := range ss {
  58. if searcher != nil {
  59. _ = searcher.Close()
  60. }
  61. }
  62. return nil, err
  63. }
  64. if _, ok := sr.(*searcher.MatchNoneSearcher); ok && q.queryStringMode {
  65. // in query string mode, skip match none
  66. continue
  67. }
  68. ss = append(ss, sr)
  69. }
  70. if len(ss) < 1 {
  71. return searcher.NewMatchNoneSearcher(i)
  72. } else if len(ss) == 1 && int(q.Min) == ss[0].Min() {
  73. // apply optimization only if both conditions below are satisfied:
  74. // - disjunction searcher has only 1 child searcher
  75. // - parent searcher's min setting is equal to child searcher's min
  76. return ss[0], nil
  77. }
  78. return searcher.NewDisjunctionSearcher(i, ss, q.Min, options)
  79. }
  80. func (q *DisjunctionQuery) Validate() error {
  81. if int(q.Min) > len(q.Disjuncts) {
  82. return fmt.Errorf("disjunction query has fewer than the minimum number of clauses to satisfy")
  83. }
  84. for _, q := range q.Disjuncts {
  85. if q, ok := q.(ValidatableQuery); ok {
  86. err := q.Validate()
  87. if err != nil {
  88. return err
  89. }
  90. }
  91. }
  92. return nil
  93. }
  94. func (q *DisjunctionQuery) UnmarshalJSON(data []byte) error {
  95. tmp := struct {
  96. Disjuncts []json.RawMessage `json:"disjuncts"`
  97. Boost *Boost `json:"boost,omitempty"`
  98. Min float64 `json:"min"`
  99. }{}
  100. err := json.Unmarshal(data, &tmp)
  101. if err != nil {
  102. return err
  103. }
  104. q.Disjuncts = make([]Query, len(tmp.Disjuncts))
  105. for i, term := range tmp.Disjuncts {
  106. query, err := ParseQuery(term)
  107. if err != nil {
  108. return err
  109. }
  110. q.Disjuncts[i] = query
  111. }
  112. q.BoostVal = tmp.Boost
  113. q.Min = tmp.Min
  114. return nil
  115. }
上海开阖软件有限公司 沪ICP备12045867号-1