0
0
Fork 0
bleve/search/searchers/search_boolean.go

293 lines
6.9 KiB
Go
Raw Normal View History

2014-04-25 18:45:23 +02:00
// Copyright (c) 2014 Couchbase, Inc.
// 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.
package searchers
2014-04-25 18:45:23 +02:00
import (
"math"
"github.com/blevesearch/bleve/index"
"github.com/blevesearch/bleve/search"
"github.com/blevesearch/bleve/search/scorers"
2014-04-25 18:45:23 +02:00
)
type BooleanSearcher struct {
initialized bool
2014-04-25 18:45:23 +02:00
index index.Index
mustSearcher search.Searcher
shouldSearcher search.Searcher
mustNotSearcher search.Searcher
2014-04-25 18:45:23 +02:00
queryNorm float64
currMust *search.DocumentMatch
currShould *search.DocumentMatch
currMustNot *search.DocumentMatch
2014-09-04 01:32:27 +02:00
currentID string
2014-04-25 18:45:23 +02:00
min uint64
scorer *scorers.ConjunctionQueryScorer
2014-04-25 18:45:23 +02:00
}
func NewBooleanSearcher(index index.Index, mustSearcher search.Searcher, shouldSearcher search.Searcher, mustNotSearcher search.Searcher, explain bool) (*BooleanSearcher, error) {
2014-04-25 18:45:23 +02:00
// build our searcher
rv := BooleanSearcher{
2014-04-25 18:45:23 +02:00
index: index,
mustSearcher: mustSearcher,
shouldSearcher: shouldSearcher,
mustNotSearcher: mustNotSearcher,
scorer: scorers.NewConjunctionQueryScorer(explain),
2014-04-25 18:45:23 +02:00
}
rv.computeQueryNorm()
return &rv, nil
}
func (s *BooleanSearcher) computeQueryNorm() {
2014-04-25 18:45:23 +02:00
// first calculate sum of squared weights
sumOfSquaredWeights := 0.0
if s.mustSearcher != nil {
sumOfSquaredWeights += s.mustSearcher.Weight()
}
if s.shouldSearcher != nil {
sumOfSquaredWeights += s.shouldSearcher.Weight()
}
// now compute query norm from this
s.queryNorm = 1.0 / math.Sqrt(sumOfSquaredWeights)
// finally tell all the downsteam searchers the norm
if s.mustSearcher != nil {
s.mustSearcher.SetQueryNorm(s.queryNorm)
}
if s.shouldSearcher != nil {
s.shouldSearcher.SetQueryNorm(s.queryNorm)
}
}
func (s *BooleanSearcher) initSearchers() error {
2014-04-25 18:45:23 +02:00
var err error
// get all searchers pointing at their first match
if s.mustSearcher != nil {
s.currMust, err = s.mustSearcher.Next()
if err != nil {
return err
}
}
if s.shouldSearcher != nil {
s.currShould, err = s.shouldSearcher.Next()
if err != nil {
return err
}
}
if s.mustNotSearcher != nil {
s.currMustNot, err = s.mustNotSearcher.Next()
if err != nil {
return err
}
}
if s.mustSearcher != nil && s.currMust != nil {
2014-09-04 01:32:27 +02:00
s.currentID = s.currMust.ID
2014-04-25 18:45:23 +02:00
} else if s.mustSearcher == nil && s.currShould != nil {
2014-09-04 01:32:27 +02:00
s.currentID = s.currShould.ID
2014-04-25 18:45:23 +02:00
} else {
2014-09-04 01:32:27 +02:00
s.currentID = ""
2014-04-25 18:45:23 +02:00
}
s.initialized = true
2014-04-25 18:45:23 +02:00
return nil
}
func (s *BooleanSearcher) advanceNextMust() error {
2014-04-25 18:45:23 +02:00
var err error
if s.mustSearcher != nil {
s.currMust, err = s.mustSearcher.Next()
if err != nil {
return err
}
} else if s.mustSearcher == nil {
s.currShould, err = s.shouldSearcher.Next()
if err != nil {
return err
}
}
if s.mustSearcher != nil && s.currMust != nil {
2014-09-04 01:32:27 +02:00
s.currentID = s.currMust.ID
2014-04-25 18:45:23 +02:00
} else if s.mustSearcher == nil && s.currShould != nil {
2014-09-04 01:32:27 +02:00
s.currentID = s.currShould.ID
2014-04-25 18:45:23 +02:00
} else {
2014-09-04 01:32:27 +02:00
s.currentID = ""
2014-04-25 18:45:23 +02:00
}
return nil
}
func (s *BooleanSearcher) Weight() float64 {
2014-04-25 18:45:23 +02:00
var rv float64
if s.mustSearcher != nil {
rv += s.mustSearcher.Weight()
}
if s.shouldSearcher != nil {
rv += s.shouldSearcher.Weight()
}
2014-04-25 18:45:23 +02:00
return rv
}
func (s *BooleanSearcher) SetQueryNorm(qnorm float64) {
if s.mustSearcher != nil {
s.mustSearcher.SetQueryNorm(qnorm)
}
if s.shouldSearcher != nil {
s.shouldSearcher.SetQueryNorm(qnorm)
}
2014-04-25 18:45:23 +02:00
}
func (s *BooleanSearcher) Next() (*search.DocumentMatch, error) {
2014-04-25 18:45:23 +02:00
if !s.initialized {
err := s.initSearchers()
if err != nil {
return nil, err
}
}
2014-04-25 18:45:23 +02:00
var err error
var rv *search.DocumentMatch
2014-04-25 18:45:23 +02:00
2014-09-04 01:32:27 +02:00
for s.currentID != "" {
if s.currMustNot != nil && s.currMustNot.ID < s.currentID {
2014-04-25 18:45:23 +02:00
// advance must not searcher to our candidate entry
2014-09-04 01:32:27 +02:00
s.currMustNot, err = s.mustNotSearcher.Advance(s.currentID)
2014-04-25 18:45:23 +02:00
if err != nil {
return nil, err
}
2014-09-04 01:32:27 +02:00
if s.currMustNot != nil && s.currMustNot.ID == s.currentID {
2014-04-25 18:45:23 +02:00
// the candidate is excluded
s.advanceNextMust()
continue
}
2014-09-04 01:32:27 +02:00
} else if s.currMustNot != nil && s.currMustNot.ID == s.currentID {
2014-04-25 18:45:23 +02:00
// the candidate is excluded
s.advanceNextMust()
continue
}
2014-09-04 01:32:27 +02:00
if s.currShould != nil && s.currShould.ID < s.currentID {
// advance should searcher to our candidate entry
2014-09-04 01:32:27 +02:00
s.currShould, err = s.shouldSearcher.Advance(s.currentID)
2014-04-25 18:45:23 +02:00
if err != nil {
return nil, err
}
2014-09-04 01:32:27 +02:00
if s.currShould != nil && s.currShould.ID == s.currentID {
2014-04-25 18:45:23 +02:00
// score bonus matches should
cons := []*search.DocumentMatch{}
2014-04-25 18:45:23 +02:00
if s.currMust != nil {
cons = append(cons, s.currMust)
}
cons = append(cons, s.currShould)
rv = s.scorer.Score(cons)
s.advanceNextMust()
break
} else if s.shouldSearcher.Min() == 0 {
2014-04-25 18:45:23 +02:00
// match is OK anyway
rv = s.scorer.Score([]*search.DocumentMatch{s.currMust})
2014-04-25 18:45:23 +02:00
s.advanceNextMust()
break
}
2014-09-04 01:32:27 +02:00
} else if s.currShould != nil && s.currShould.ID == s.currentID {
2014-04-25 18:45:23 +02:00
// score bonus matches should
cons := []*search.DocumentMatch{}
2014-04-25 18:45:23 +02:00
if s.currMust != nil {
cons = append(cons, s.currMust)
}
cons = append(cons, s.currShould)
rv = s.scorer.Score(cons)
s.advanceNextMust()
break
} else if s.shouldSearcher == nil || s.shouldSearcher.Min() == 0 {
2014-04-25 18:45:23 +02:00
// match is OK anyway
rv = s.scorer.Score([]*search.DocumentMatch{s.currMust})
2014-04-25 18:45:23 +02:00
s.advanceNextMust()
break
}
s.advanceNextMust()
}
return rv, nil
}
func (s *BooleanSearcher) Advance(ID string) (*search.DocumentMatch, error) {
if !s.initialized {
err := s.initSearchers()
if err != nil {
return nil, err
}
}
var err error
if s.mustSearcher != nil {
s.currMust, err = s.mustSearcher.Advance(ID)
if err != nil {
return nil, err
}
}
if s.shouldSearcher != nil {
s.currShould, err = s.shouldSearcher.Advance(ID)
if err != nil {
return nil, err
}
}
if s.mustNotSearcher != nil {
s.currMustNot, err = s.mustNotSearcher.Advance(ID)
if err != nil {
return nil, err
}
}
if s.mustSearcher != nil && s.currMust != nil {
2014-09-04 01:32:27 +02:00
s.currentID = s.currMust.ID
} else if s.mustSearcher == nil && s.currShould != nil {
2014-09-04 01:32:27 +02:00
s.currentID = s.currShould.ID
} else {
2014-09-04 01:32:27 +02:00
s.currentID = ""
}
2014-04-25 18:45:23 +02:00
return s.Next()
}
func (s *BooleanSearcher) Count() uint64 {
2014-04-25 18:45:23 +02:00
// for now return a worst case
2014-09-04 00:47:02 +02:00
var sum uint64
if s.mustSearcher != nil {
sum += s.mustSearcher.Count()
}
if s.shouldSearcher != nil {
sum += s.shouldSearcher.Count()
}
2014-04-25 18:45:23 +02:00
return sum
}
func (s *BooleanSearcher) Close() {
2014-04-25 18:45:23 +02:00
if s.mustSearcher != nil {
s.mustSearcher.Close()
}
if s.shouldSearcher != nil {
s.shouldSearcher.Close()
}
if s.mustNotSearcher != nil {
s.mustNotSearcher.Close()
}
}
func (s *BooleanSearcher) Min() int {
return 0
}