2014-04-25 17:31:28 +02:00
|
|
|
// Copyright (c) 2014 Couchbase, Inc.
|
2016-10-02 16:13:14 +02:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
2014-09-02 16:54:50 +02:00
|
|
|
|
2016-10-02 16:29:39 +02:00
|
|
|
package searcher
|
2014-04-25 17:31:28 +02:00
|
|
|
|
|
|
|
import (
|
2016-02-08 23:19:45 +01:00
|
|
|
"fmt"
|
2014-04-25 17:31:28 +02:00
|
|
|
"math"
|
2018-03-02 02:12:16 +01:00
|
|
|
"reflect"
|
2014-04-25 17:31:28 +02:00
|
|
|
"sort"
|
|
|
|
|
2014-08-28 21:38:57 +02:00
|
|
|
"github.com/blevesearch/bleve/index"
|
2014-09-01 17:15:38 +02:00
|
|
|
"github.com/blevesearch/bleve/search"
|
2016-10-01 23:20:59 +02:00
|
|
|
"github.com/blevesearch/bleve/search/scorer"
|
2018-03-02 02:12:16 +01:00
|
|
|
"github.com/blevesearch/bleve/size"
|
2014-04-25 17:31:28 +02:00
|
|
|
)
|
|
|
|
|
2018-03-02 02:12:16 +01:00
|
|
|
var reflectStaticSizeDisjunctionSearcher int
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
var ds DisjunctionSearcher
|
|
|
|
reflectStaticSizeDisjunctionSearcher = int(reflect.TypeOf(ds).Size())
|
|
|
|
}
|
|
|
|
|
2016-02-08 23:19:45 +01:00
|
|
|
// DisjunctionMaxClauseCount is a compile time setting that applications can
|
|
|
|
// adjust to non-zero value to cause the DisjunctionSearcher to return an
|
|
|
|
// error instead of exeucting searches when the size exceeds this value.
|
|
|
|
var DisjunctionMaxClauseCount = 0
|
|
|
|
|
2014-08-07 02:03:41 +02:00
|
|
|
type DisjunctionSearcher struct {
|
2016-09-18 19:09:40 +02:00
|
|
|
indexReader index.IndexReader
|
|
|
|
searchers OrderedSearcherList
|
2016-10-11 07:41:28 +02:00
|
|
|
numSearchers int
|
2016-09-18 19:09:40 +02:00
|
|
|
queryNorm float64
|
|
|
|
currs []*search.DocumentMatch
|
2016-10-02 16:29:39 +02:00
|
|
|
scorer *scorer.DisjunctionQueryScorer
|
2016-09-21 03:51:40 +02:00
|
|
|
min int
|
2016-09-18 19:09:40 +02:00
|
|
|
matching []*search.DocumentMatch
|
|
|
|
matchingIdxs []int
|
|
|
|
initialized bool
|
2014-04-25 17:31:28 +02:00
|
|
|
}
|
|
|
|
|
2016-04-18 16:06:34 +02:00
|
|
|
func tooManyClauses(count int) bool {
|
|
|
|
if DisjunctionMaxClauseCount != 0 && count > DisjunctionMaxClauseCount {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func tooManyClausesErr() error {
|
2017-04-04 16:46:57 +02:00
|
|
|
return fmt.Errorf("TooManyClauses[maxClauseCount is set to %d]",
|
|
|
|
DisjunctionMaxClauseCount)
|
2016-04-18 16:06:34 +02:00
|
|
|
}
|
|
|
|
|
2017-04-04 16:46:57 +02:00
|
|
|
func NewDisjunctionSearcher(indexReader index.IndexReader,
|
|
|
|
qsearchers []search.Searcher, min float64, options search.SearcherOptions) (
|
|
|
|
*DisjunctionSearcher, error) {
|
|
|
|
return newDisjunctionSearcher(indexReader, qsearchers, min, options,
|
|
|
|
true)
|
|
|
|
}
|
|
|
|
|
|
|
|
func newDisjunctionSearcher(indexReader index.IndexReader,
|
|
|
|
qsearchers []search.Searcher, min float64, options search.SearcherOptions,
|
|
|
|
limit bool) (
|
|
|
|
*DisjunctionSearcher, error) {
|
|
|
|
if limit && tooManyClauses(len(qsearchers)) {
|
2016-04-18 16:06:34 +02:00
|
|
|
return nil, tooManyClausesErr()
|
2016-02-08 23:19:45 +01:00
|
|
|
}
|
2014-12-18 18:43:12 +01:00
|
|
|
// build the downstream searchers
|
2014-07-30 18:30:38 +02:00
|
|
|
searchers := make(OrderedSearcherList, len(qsearchers))
|
|
|
|
for i, searcher := range qsearchers {
|
2014-04-25 17:31:28 +02:00
|
|
|
searchers[i] = searcher
|
|
|
|
}
|
|
|
|
// sort the searchers
|
|
|
|
sort.Sort(sort.Reverse(searchers))
|
|
|
|
// build our searcher
|
2014-08-07 02:03:41 +02:00
|
|
|
rv := DisjunctionSearcher{
|
2016-09-18 19:09:40 +02:00
|
|
|
indexReader: indexReader,
|
|
|
|
searchers: searchers,
|
2016-10-11 07:41:28 +02:00
|
|
|
numSearchers: len(searchers),
|
2016-09-18 19:09:40 +02:00
|
|
|
currs: make([]*search.DocumentMatch, len(searchers)),
|
2017-01-06 02:49:45 +01:00
|
|
|
scorer: scorer.NewDisjunctionQueryScorer(options),
|
2016-09-21 03:51:40 +02:00
|
|
|
min: int(min),
|
2016-09-18 19:09:40 +02:00
|
|
|
matching: make([]*search.DocumentMatch, len(searchers)),
|
|
|
|
matchingIdxs: make([]int, len(searchers)),
|
2014-04-25 17:31:28 +02:00
|
|
|
}
|
|
|
|
rv.computeQueryNorm()
|
|
|
|
return &rv, nil
|
|
|
|
}
|
|
|
|
|
2018-03-02 02:12:16 +01:00
|
|
|
func (s *DisjunctionSearcher) Size() int {
|
|
|
|
sizeInBytes := reflectStaticSizeDisjunctionSearcher + size.SizeOfPtr +
|
|
|
|
s.scorer.Size()
|
|
|
|
|
|
|
|
for _, entry := range s.searchers {
|
|
|
|
sizeInBytes += entry.Size()
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, entry := range s.currs {
|
|
|
|
if entry != nil {
|
|
|
|
sizeInBytes += entry.Size()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, entry := range s.matching {
|
|
|
|
if entry != nil {
|
|
|
|
sizeInBytes += entry.Size()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sizeInBytes += len(s.matchingIdxs) * size.SizeOfInt
|
|
|
|
|
|
|
|
return sizeInBytes
|
|
|
|
}
|
|
|
|
|
2014-08-07 02:03:41 +02:00
|
|
|
func (s *DisjunctionSearcher) computeQueryNorm() {
|
2014-04-25 17:31:28 +02:00
|
|
|
// first calculate sum of squared weights
|
|
|
|
sumOfSquaredWeights := 0.0
|
2017-12-22 01:45:36 +01:00
|
|
|
for _, searcher := range s.searchers {
|
|
|
|
sumOfSquaredWeights += searcher.Weight()
|
2014-04-25 17:31:28 +02:00
|
|
|
}
|
|
|
|
// now compute query norm from this
|
|
|
|
s.queryNorm = 1.0 / math.Sqrt(sumOfSquaredWeights)
|
2014-12-18 18:43:12 +01:00
|
|
|
// finally tell all the downstream searchers the norm
|
2017-12-22 01:45:36 +01:00
|
|
|
for _, searcher := range s.searchers {
|
|
|
|
searcher.SetQueryNorm(s.queryNorm)
|
2014-04-25 17:31:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-09 04:21:47 +02:00
|
|
|
func (s *DisjunctionSearcher) initSearchers(ctx *search.SearchContext) error {
|
2014-04-25 17:31:28 +02:00
|
|
|
var err error
|
|
|
|
// get all searchers pointing at their first match
|
2017-12-22 01:45:36 +01:00
|
|
|
for i, searcher := range s.searchers {
|
2016-08-09 04:21:47 +02:00
|
|
|
if s.currs[i] != nil {
|
|
|
|
ctx.DocumentMatchPool.Put(s.currs[i])
|
|
|
|
}
|
2017-12-22 01:45:36 +01:00
|
|
|
s.currs[i], err = searcher.Next(ctx)
|
2014-04-25 17:31:28 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-11 07:41:28 +02:00
|
|
|
err = s.updateMatches()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-08-07 01:36:23 +02:00
|
|
|
s.initialized = true
|
2014-04-25 17:31:28 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-10-11 07:41:28 +02:00
|
|
|
func (s *DisjunctionSearcher) updateMatches() error {
|
2016-09-21 04:22:37 +02:00
|
|
|
matching := s.matching[:0]
|
|
|
|
matchingIdxs := s.matchingIdxs[:0]
|
|
|
|
|
2016-10-11 07:41:28 +02:00
|
|
|
for i := 0; i < len(s.currs); i++ {
|
|
|
|
curr := s.currs[i]
|
2016-09-21 04:22:37 +02:00
|
|
|
if curr == nil {
|
|
|
|
continue
|
2014-04-25 17:31:28 +02:00
|
|
|
}
|
2016-09-21 04:22:37 +02:00
|
|
|
|
|
|
|
if len(matching) > 0 {
|
|
|
|
cmp := curr.IndexInternalID.Compare(matching[0].IndexInternalID)
|
|
|
|
if cmp > 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if cmp < 0 {
|
|
|
|
matching = matching[:0]
|
|
|
|
matchingIdxs = matchingIdxs[:0]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
matching = append(matching, curr)
|
|
|
|
matchingIdxs = append(matchingIdxs, i)
|
2014-04-25 17:31:28 +02:00
|
|
|
}
|
2016-09-21 04:22:37 +02:00
|
|
|
|
|
|
|
s.matching = matching
|
|
|
|
s.matchingIdxs = matchingIdxs
|
2016-10-11 07:41:28 +02:00
|
|
|
|
|
|
|
return nil
|
2014-04-25 17:31:28 +02:00
|
|
|
}
|
|
|
|
|
2014-08-07 02:03:41 +02:00
|
|
|
func (s *DisjunctionSearcher) Weight() float64 {
|
2014-04-25 17:31:28 +02:00
|
|
|
var rv float64
|
|
|
|
for _, searcher := range s.searchers {
|
|
|
|
rv += searcher.Weight()
|
|
|
|
}
|
|
|
|
return rv
|
|
|
|
}
|
|
|
|
|
2014-08-07 02:03:41 +02:00
|
|
|
func (s *DisjunctionSearcher) SetQueryNorm(qnorm float64) {
|
2014-04-25 17:31:28 +02:00
|
|
|
for _, searcher := range s.searchers {
|
|
|
|
searcher.SetQueryNorm(qnorm)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-04 16:46:57 +02:00
|
|
|
func (s *DisjunctionSearcher) Next(ctx *search.SearchContext) (
|
|
|
|
*search.DocumentMatch, error) {
|
2014-08-07 01:36:23 +02:00
|
|
|
if !s.initialized {
|
2016-08-09 04:21:47 +02:00
|
|
|
err := s.initSearchers(ctx)
|
2014-08-07 01:36:23 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2014-04-25 17:31:28 +02:00
|
|
|
var err error
|
2016-08-01 20:58:02 +02:00
|
|
|
var rv *search.DocumentMatch
|
2014-04-25 17:31:28 +02:00
|
|
|
|
|
|
|
found := false
|
2016-09-21 04:22:37 +02:00
|
|
|
for !found && len(s.matching) > 0 {
|
|
|
|
if len(s.matching) >= s.min {
|
2014-04-25 17:31:28 +02:00
|
|
|
found = true
|
|
|
|
// score this match
|
2016-10-11 07:41:28 +02:00
|
|
|
rv = s.scorer.Score(ctx, s.matching, len(s.matching), s.numSearchers)
|
2014-04-25 17:31:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// invoke next on all the matching searchers
|
2016-09-21 04:22:37 +02:00
|
|
|
for _, i := range s.matchingIdxs {
|
2016-09-18 19:09:40 +02:00
|
|
|
searcher := s.searchers[i]
|
|
|
|
if s.currs[i] != rv {
|
|
|
|
ctx.DocumentMatchPool.Put(s.currs[i])
|
|
|
|
}
|
|
|
|
s.currs[i], err = searcher.Next(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2014-04-25 17:31:28 +02:00
|
|
|
}
|
|
|
|
}
|
2016-09-21 04:22:37 +02:00
|
|
|
|
2016-10-11 07:41:28 +02:00
|
|
|
err = s.updateMatches()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-04-25 17:31:28 +02:00
|
|
|
}
|
|
|
|
return rv, nil
|
|
|
|
}
|
|
|
|
|
2017-04-04 16:46:57 +02:00
|
|
|
func (s *DisjunctionSearcher) Advance(ctx *search.SearchContext,
|
|
|
|
ID index.IndexInternalID) (*search.DocumentMatch, error) {
|
2014-08-07 01:36:23 +02:00
|
|
|
if !s.initialized {
|
2016-08-09 04:21:47 +02:00
|
|
|
err := s.initSearchers(ctx)
|
2014-08-07 01:36:23 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2014-04-25 17:31:28 +02:00
|
|
|
// get all searchers pointing at their first match
|
|
|
|
var err error
|
2017-12-22 01:45:36 +01:00
|
|
|
for i, searcher := range s.searchers {
|
2016-08-09 04:21:47 +02:00
|
|
|
if s.currs[i] != nil {
|
scorch fix disjunction searcher Advance()
Found with "versus" test (TestScorchVersusUpsideDownBoltSmallMNSAM),
which had a boolean query with a MustNot that was the same as the Must
parameters. This replicates a situation found by
Aruna/Mihir/testrunner/RQG (MB-27291). Example:
"query": {
"must_not": {"disjuncts": [
{"field": "body", "match": "hello"}
]},
"must": {"conjuncts": [
{"field": "body", "match": "hello"}
]}
}
The nested searchers along the MustNot pathway would end up looking
roughly like...
booleanSearcher
MustNot
=> disjunctionSearcher
=> disjunctionSearcher
=> termSearcher
On the first Next() call by the collector, the two disjunction
searchers would run through their respective Next() method processing,
which includes their initSearcher() processing on the first time.
This has the effect of driving the leaf termSearcher through two
Next() invocations.
That is, if there were 3 docs (doc-1, doc-2, doc-3), the leaf
termSearcher would at this point have moved to point to doc-3, while
the topmost MustNot would have received doc-1.
Next, the booleanSearcher's Must searcher would produce doc-2, so the
booleanSearcher would try to Advance() the MustNot searcher to doc-2.
But, in scorch, the leafmost termSearcher had already gotten past
doc-2 and would return its doc-3.
In upsidedown, in contrast, the leaf termSearcher would then drive the
KVStore iterator with a Seek(doc-2), and the KVStore iterator would
perform a backwards seek to reach doc-2.
In scorch, however, backwards iteration seeking isn't supported.
So, this fix checks the state of the disjunction searcher to see if we
already have the necessary state so that we don't have to perform
actual Advance()'es on the underlying searchers. This not only fixes
the behavior w.r.t. scorch, but also can have an effect of potentially
making upsidedown slightly faster as we're avoiding some backwards
KVStore iterator seeks.
2017-12-22 02:49:55 +01:00
|
|
|
if s.currs[i].IndexInternalID.Compare(ID) >= 0 {
|
|
|
|
continue
|
|
|
|
}
|
2016-08-09 04:21:47 +02:00
|
|
|
ctx.DocumentMatchPool.Put(s.currs[i])
|
|
|
|
}
|
2017-12-22 01:45:36 +01:00
|
|
|
s.currs[i], err = searcher.Advance(ctx, ID)
|
2014-04-25 17:31:28 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-11 07:41:28 +02:00
|
|
|
err = s.updateMatches()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-04-25 17:31:28 +02:00
|
|
|
|
2016-08-09 04:21:47 +02:00
|
|
|
return s.Next(ctx)
|
2014-04-25 17:31:28 +02:00
|
|
|
}
|
|
|
|
|
2014-08-07 02:03:41 +02:00
|
|
|
func (s *DisjunctionSearcher) Count() uint64 {
|
2014-04-25 17:31:28 +02:00
|
|
|
// for now return a worst case
|
2014-09-04 00:47:02 +02:00
|
|
|
var sum uint64
|
2014-04-25 17:31:28 +02:00
|
|
|
for _, searcher := range s.searchers {
|
|
|
|
sum += searcher.Count()
|
|
|
|
}
|
|
|
|
return sum
|
|
|
|
}
|
|
|
|
|
2016-09-30 01:51:42 +02:00
|
|
|
func (s *DisjunctionSearcher) Close() (rv error) {
|
2014-04-25 17:31:28 +02:00
|
|
|
for _, searcher := range s.searchers {
|
2015-03-06 20:46:29 +01:00
|
|
|
err := searcher.Close()
|
2016-09-30 01:51:42 +02:00
|
|
|
if err != nil && rv == nil {
|
|
|
|
rv = err
|
2015-03-06 20:46:29 +01:00
|
|
|
}
|
2014-04-25 17:31:28 +02:00
|
|
|
}
|
2016-09-30 01:51:42 +02:00
|
|
|
return rv
|
2014-04-25 17:31:28 +02:00
|
|
|
}
|
2014-08-30 00:14:12 +02:00
|
|
|
|
|
|
|
func (s *DisjunctionSearcher) Min() int {
|
2016-09-21 03:51:40 +02:00
|
|
|
return s.min
|
2014-08-30 00:14:12 +02:00
|
|
|
}
|
2016-08-09 04:21:47 +02:00
|
|
|
|
|
|
|
func (s *DisjunctionSearcher) DocumentMatchPoolSize() int {
|
|
|
|
rv := len(s.currs)
|
|
|
|
for _, s := range s.searchers {
|
|
|
|
rv += s.DocumentMatchPoolSize()
|
|
|
|
}
|
|
|
|
return rv
|
|
|
|
}
|