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

110 lines
3.2KB

  1. // Copyright (c) 2018 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 searcher
  15. import (
  16. "fmt"
  17. "github.com/blevesearch/bleve/index"
  18. "github.com/blevesearch/bleve/search"
  19. )
  20. // DisjunctionMaxClauseCount is a compile time setting that applications can
  21. // adjust to non-zero value to cause the DisjunctionSearcher to return an
  22. // error instead of exeucting searches when the size exceeds this value.
  23. var DisjunctionMaxClauseCount = 0
  24. // DisjunctionHeapTakeover is a compile time setting that applications can
  25. // adjust to control when the DisjunctionSearcher will switch from a simple
  26. // slice implementation to a heap implementation.
  27. var DisjunctionHeapTakeover = 10
  28. func NewDisjunctionSearcher(indexReader index.IndexReader,
  29. qsearchers []search.Searcher, min float64, options search.SearcherOptions) (
  30. search.Searcher, error) {
  31. return newDisjunctionSearcher(indexReader, qsearchers, min, options, true)
  32. }
  33. func newDisjunctionSearcher(indexReader index.IndexReader,
  34. qsearchers []search.Searcher, min float64, options search.SearcherOptions,
  35. limit bool) (search.Searcher, error) {
  36. // attempt the "unadorned" disjunction optimization only when we
  37. // do not need extra information like freq-norm's or term vectors
  38. // and the requested min is simple
  39. if len(qsearchers) > 1 && min <= 1 &&
  40. options.Score == "none" && !options.IncludeTermVectors {
  41. rv, err := optimizeCompositeSearcher("disjunction:unadorned",
  42. indexReader, qsearchers, options)
  43. if err != nil || rv != nil {
  44. return rv, err
  45. }
  46. }
  47. if len(qsearchers) > DisjunctionHeapTakeover {
  48. return newDisjunctionHeapSearcher(indexReader, qsearchers, min, options,
  49. limit)
  50. }
  51. return newDisjunctionSliceSearcher(indexReader, qsearchers, min, options,
  52. limit)
  53. }
  54. func optimizeCompositeSearcher(optimizationKind string,
  55. indexReader index.IndexReader, qsearchers []search.Searcher,
  56. options search.SearcherOptions) (search.Searcher, error) {
  57. var octx index.OptimizableContext
  58. for _, searcher := range qsearchers {
  59. o, ok := searcher.(index.Optimizable)
  60. if !ok {
  61. return nil, nil
  62. }
  63. var err error
  64. octx, err = o.Optimize(optimizationKind, octx)
  65. if err != nil {
  66. return nil, err
  67. }
  68. if octx == nil {
  69. return nil, nil
  70. }
  71. }
  72. optimized, err := octx.Finish()
  73. if err != nil || optimized == nil {
  74. return nil, err
  75. }
  76. tfr, ok := optimized.(index.TermFieldReader)
  77. if !ok {
  78. return nil, nil
  79. }
  80. return newTermSearcherFromReader(indexReader, tfr,
  81. []byte(optimizationKind), "*", 1.0, options)
  82. }
  83. func tooManyClauses(count int) bool {
  84. if DisjunctionMaxClauseCount != 0 && count > DisjunctionMaxClauseCount {
  85. return true
  86. }
  87. return false
  88. }
  89. func tooManyClausesErr(count int) error {
  90. return fmt.Errorf("TooManyClauses[%d > maxClauseCount, which is set to %d]",
  91. count, DisjunctionMaxClauseCount)
  92. }
上海开阖软件有限公司 沪ICP备12045867号-1