2014-08-07 19:45:39 +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.
|
2014-09-02 16:54:50 +02:00
|
|
|
|
2014-09-01 17:15:38 +02:00
|
|
|
package searchers
|
2014-08-07 19:45:39 +02:00
|
|
|
|
|
|
|
import (
|
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"
|
2014-08-07 19:45:39 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type TermPrefixSearcher struct {
|
2014-09-12 23:21:35 +02:00
|
|
|
indexReader index.IndexReader
|
|
|
|
prefix string
|
|
|
|
field string
|
|
|
|
explain bool
|
|
|
|
searcher *DisjunctionSearcher
|
2014-08-07 19:45:39 +02:00
|
|
|
}
|
|
|
|
|
2014-09-12 23:21:35 +02:00
|
|
|
func NewTermPrefixSearcher(indexReader index.IndexReader, prefix string, field string, boost float64, explain bool) (*TermPrefixSearcher, error) {
|
2014-08-07 19:45:39 +02:00
|
|
|
// find the terms with this prefix
|
2015-03-10 21:22:19 +01:00
|
|
|
fieldDict, err := indexReader.FieldDictPrefix(field, []byte(prefix))
|
2014-08-07 19:45:39 +02:00
|
|
|
|
|
|
|
// enumerate all the terms in the range
|
2014-09-01 17:15:38 +02:00
|
|
|
qsearchers := make([]search.Searcher, 0, 25)
|
2015-03-10 21:22:19 +01:00
|
|
|
tfd, err := fieldDict.Next()
|
2014-08-07 19:45:39 +02:00
|
|
|
for err == nil && tfd != nil {
|
2014-09-12 23:21:35 +02:00
|
|
|
qsearcher, err := NewTermSearcher(indexReader, string(tfd.Term), field, 1.0, explain)
|
2014-08-07 19:45:39 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
qsearchers = append(qsearchers, qsearcher)
|
2015-03-10 21:22:19 +01:00
|
|
|
tfd, err = fieldDict.Next()
|
2014-08-07 19:45:39 +02:00
|
|
|
}
|
2016-02-20 21:41:12 +01:00
|
|
|
|
|
|
|
err = fieldDict.Close()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2014-08-07 19:45:39 +02:00
|
|
|
// build disjunction searcher of these ranges
|
2014-09-12 23:21:35 +02:00
|
|
|
searcher, err := NewDisjunctionSearcher(indexReader, qsearchers, 0, explain)
|
2014-08-07 19:45:39 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &TermPrefixSearcher{
|
2014-09-12 23:21:35 +02:00
|
|
|
indexReader: indexReader,
|
|
|
|
prefix: prefix,
|
|
|
|
field: field,
|
|
|
|
explain: explain,
|
|
|
|
searcher: searcher,
|
2014-08-07 19:45:39 +02:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
func (s *TermPrefixSearcher) Count() uint64 {
|
|
|
|
return s.searcher.Count()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *TermPrefixSearcher) Weight() float64 {
|
|
|
|
return s.searcher.Weight()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *TermPrefixSearcher) SetQueryNorm(qnorm float64) {
|
|
|
|
s.searcher.SetQueryNorm(qnorm)
|
|
|
|
}
|
|
|
|
|
2014-09-01 17:15:38 +02:00
|
|
|
func (s *TermPrefixSearcher) Next() (*search.DocumentMatch, error) {
|
2014-08-07 19:45:39 +02:00
|
|
|
return s.searcher.Next()
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-09-01 17:15:38 +02:00
|
|
|
func (s *TermPrefixSearcher) Advance(ID string) (*search.DocumentMatch, error) {
|
2014-08-07 19:45:39 +02:00
|
|
|
return s.searcher.Next()
|
|
|
|
}
|
|
|
|
|
2015-03-06 20:46:29 +01:00
|
|
|
func (s *TermPrefixSearcher) Close() error {
|
|
|
|
return s.searcher.Close()
|
2014-08-07 19:45:39 +02:00
|
|
|
}
|
2014-08-30 00:14:12 +02:00
|
|
|
|
|
|
|
func (s *TermPrefixSearcher) Min() int {
|
|
|
|
return 0
|
|
|
|
}
|