2014-08-03 01:05:58 +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-08-03 01:05:58 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"math"
|
|
|
|
|
2014-08-28 21:38:57 +02:00
|
|
|
"github.com/blevesearch/bleve/index"
|
2016-09-30 17:35:22 +02:00
|
|
|
"github.com/blevesearch/bleve/numeric"
|
2014-09-01 17:15:38 +02:00
|
|
|
"github.com/blevesearch/bleve/search"
|
2014-08-03 01:05:58 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type NumericRangeSearcher struct {
|
2014-09-12 23:21:35 +02:00
|
|
|
indexReader index.IndexReader
|
|
|
|
min *float64
|
|
|
|
max *float64
|
|
|
|
field string
|
2017-01-06 02:49:45 +01:00
|
|
|
options search.SearcherOptions
|
2014-09-12 23:21:35 +02:00
|
|
|
searcher *DisjunctionSearcher
|
2014-08-03 01:05:58 +02:00
|
|
|
}
|
|
|
|
|
2017-01-06 02:49:45 +01:00
|
|
|
func NewNumericRangeSearcher(indexReader index.IndexReader, min *float64, max *float64, inclusiveMin, inclusiveMax *bool, field string, boost float64, options search.SearcherOptions) (*NumericRangeSearcher, error) {
|
2014-08-03 01:05:58 +02:00
|
|
|
// account for unbounded edges
|
|
|
|
if min == nil {
|
|
|
|
negInf := math.Inf(-1)
|
|
|
|
min = &negInf
|
|
|
|
}
|
|
|
|
if max == nil {
|
|
|
|
Inf := math.Inf(1)
|
|
|
|
max = &Inf
|
|
|
|
}
|
2014-08-25 23:47:27 +02:00
|
|
|
if inclusiveMin == nil {
|
|
|
|
defaultInclusiveMin := true
|
|
|
|
inclusiveMin = &defaultInclusiveMin
|
|
|
|
}
|
|
|
|
if inclusiveMax == nil {
|
|
|
|
defaultInclusiveMax := false
|
|
|
|
inclusiveMax = &defaultInclusiveMax
|
|
|
|
}
|
2014-08-03 01:05:58 +02:00
|
|
|
// find all the ranges
|
2016-09-30 17:35:22 +02:00
|
|
|
minInt64 := numeric.Float64ToInt64(*min)
|
2014-08-25 23:47:27 +02:00
|
|
|
if !*inclusiveMin && minInt64 != math.MaxInt64 {
|
2014-09-04 00:47:02 +02:00
|
|
|
minInt64++
|
2014-08-25 23:47:27 +02:00
|
|
|
}
|
2016-09-30 17:35:22 +02:00
|
|
|
maxInt64 := numeric.Float64ToInt64(*max)
|
2014-08-25 23:47:27 +02:00
|
|
|
if !*inclusiveMax && maxInt64 != math.MinInt64 {
|
2014-09-04 00:47:02 +02:00
|
|
|
maxInt64--
|
2014-08-25 23:47:27 +02:00
|
|
|
}
|
2014-12-18 18:43:12 +01:00
|
|
|
// FIXME hard-coded precision, should match field declaration
|
2014-08-03 01:05:58 +02:00
|
|
|
termRanges := splitInt64Range(minInt64, maxInt64, 4)
|
|
|
|
terms := termRanges.Enumerate()
|
2016-04-18 16:06:34 +02:00
|
|
|
if tooManyClauses(len(terms)) {
|
|
|
|
return nil, tooManyClausesErr()
|
|
|
|
}
|
2014-08-03 01:05:58 +02:00
|
|
|
// enumerate all the terms in the range
|
2014-09-01 17:15:38 +02:00
|
|
|
qsearchers := make([]search.Searcher, len(terms))
|
2016-09-30 01:51:42 +02:00
|
|
|
qsearchersClose := func() {
|
|
|
|
for _, searcher := range qsearchers {
|
|
|
|
if searcher != nil {
|
|
|
|
_ = searcher.Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-08-03 01:05:58 +02:00
|
|
|
for i, term := range terms {
|
|
|
|
var err error
|
2017-01-06 02:49:45 +01:00
|
|
|
qsearchers[i], err = NewTermSearcher(indexReader, string(term), field, boost, options)
|
2014-08-03 01:05:58 +02:00
|
|
|
if err != nil {
|
2016-09-30 01:51:42 +02:00
|
|
|
qsearchersClose()
|
2014-08-03 01:05:58 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// build disjunction searcher of these ranges
|
2017-01-06 02:49:45 +01:00
|
|
|
searcher, err := NewDisjunctionSearcher(indexReader, qsearchers, 0, options)
|
2014-08-03 01:05:58 +02:00
|
|
|
if err != nil {
|
2016-09-30 01:51:42 +02:00
|
|
|
qsearchersClose()
|
2014-08-03 01:05:58 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &NumericRangeSearcher{
|
2014-09-12 23:21:35 +02:00
|
|
|
indexReader: indexReader,
|
|
|
|
min: min,
|
|
|
|
max: max,
|
|
|
|
field: field,
|
2017-01-06 02:49:45 +01:00
|
|
|
options: options,
|
2014-09-12 23:21:35 +02:00
|
|
|
searcher: searcher,
|
2014-08-03 01:05:58 +02:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *NumericRangeSearcher) Count() uint64 {
|
|
|
|
return s.searcher.Count()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *NumericRangeSearcher) Weight() float64 {
|
|
|
|
return s.searcher.Weight()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *NumericRangeSearcher) SetQueryNorm(qnorm float64) {
|
|
|
|
s.searcher.SetQueryNorm(qnorm)
|
|
|
|
}
|
|
|
|
|
2016-08-09 04:21:47 +02:00
|
|
|
func (s *NumericRangeSearcher) Next(ctx *search.SearchContext) (*search.DocumentMatch, error) {
|
|
|
|
return s.searcher.Next(ctx)
|
2014-08-03 01:05:58 +02:00
|
|
|
}
|
|
|
|
|
2016-08-09 04:21:47 +02:00
|
|
|
func (s *NumericRangeSearcher) Advance(ctx *search.SearchContext, ID index.IndexInternalID) (*search.DocumentMatch, error) {
|
|
|
|
return s.searcher.Advance(ctx, ID)
|
2014-08-03 01:05:58 +02:00
|
|
|
}
|
|
|
|
|
2015-03-06 20:46:29 +01:00
|
|
|
func (s *NumericRangeSearcher) Close() error {
|
|
|
|
return s.searcher.Close()
|
2014-08-03 01:05:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type termRange struct {
|
|
|
|
startTerm []byte
|
|
|
|
endTerm []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *termRange) Enumerate() [][]byte {
|
2016-10-02 17:54:04 +02:00
|
|
|
var rv [][]byte
|
2014-08-03 01:05:58 +02:00
|
|
|
next := t.startTerm
|
|
|
|
for bytes.Compare(next, t.endTerm) <= 0 {
|
|
|
|
rv = append(rv, next)
|
|
|
|
next = incrementBytes(next)
|
|
|
|
}
|
|
|
|
return rv
|
|
|
|
}
|
|
|
|
|
|
|
|
func incrementBytes(in []byte) []byte {
|
|
|
|
rv := make([]byte, len(in))
|
|
|
|
copy(rv, in)
|
|
|
|
for i := len(rv) - 1; i >= 0; i-- {
|
|
|
|
rv[i] = rv[i] + 1
|
|
|
|
if rv[i] != 0 {
|
2016-01-14 23:46:27 +01:00
|
|
|
// didn't overflow, so stop
|
2014-08-03 01:05:58 +02:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return rv
|
|
|
|
}
|
|
|
|
|
|
|
|
type termRanges []*termRange
|
|
|
|
|
|
|
|
func (tr termRanges) Enumerate() [][]byte {
|
2016-10-02 17:54:04 +02:00
|
|
|
var rv [][]byte
|
2014-08-03 01:05:58 +02:00
|
|
|
for _, tri := range tr {
|
|
|
|
trie := tri.Enumerate()
|
|
|
|
rv = append(rv, trie...)
|
|
|
|
}
|
|
|
|
return rv
|
|
|
|
}
|
|
|
|
|
|
|
|
func splitInt64Range(minBound, maxBound int64, precisionStep uint) termRanges {
|
|
|
|
rv := make(termRanges, 0)
|
|
|
|
if minBound > maxBound {
|
|
|
|
return rv
|
|
|
|
}
|
|
|
|
|
|
|
|
for shift := uint(0); ; shift += precisionStep {
|
|
|
|
|
|
|
|
diff := int64(1) << (shift + precisionStep)
|
|
|
|
mask := ((int64(1) << precisionStep) - int64(1)) << shift
|
|
|
|
hasLower := (minBound & mask) != int64(0)
|
|
|
|
hasUpper := (maxBound & mask) != mask
|
|
|
|
|
|
|
|
var nextMinBound int64
|
|
|
|
if hasLower {
|
|
|
|
nextMinBound = (minBound + diff) &^ mask
|
|
|
|
} else {
|
|
|
|
nextMinBound = minBound &^ mask
|
|
|
|
}
|
|
|
|
var nextMaxBound int64
|
|
|
|
if hasUpper {
|
|
|
|
nextMaxBound = (maxBound - diff) &^ mask
|
|
|
|
} else {
|
|
|
|
nextMaxBound = maxBound &^ mask
|
|
|
|
}
|
|
|
|
|
|
|
|
lowerWrapped := nextMinBound < minBound
|
|
|
|
upperWrapped := nextMaxBound > maxBound
|
|
|
|
|
|
|
|
if shift+precisionStep >= 64 || nextMinBound > nextMaxBound || lowerWrapped || upperWrapped {
|
|
|
|
// We are in the lowest precision or the next precision is not available.
|
|
|
|
rv = append(rv, newRange(minBound, maxBound, shift))
|
|
|
|
// exit the split recursion loop
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
if hasLower {
|
|
|
|
rv = append(rv, newRange(minBound, minBound|mask, shift))
|
|
|
|
}
|
|
|
|
if hasUpper {
|
|
|
|
rv = append(rv, newRange(maxBound&^mask, maxBound, shift))
|
|
|
|
}
|
|
|
|
|
|
|
|
// recurse to next precision
|
|
|
|
minBound = nextMinBound
|
|
|
|
maxBound = nextMaxBound
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv
|
|
|
|
}
|
|
|
|
|
|
|
|
func newRange(minBound, maxBound int64, shift uint) *termRange {
|
|
|
|
maxBound |= (int64(1) << shift) - int64(1)
|
2016-09-30 17:35:22 +02:00
|
|
|
minBytes := numeric.MustNewPrefixCodedInt64(minBound, shift)
|
|
|
|
maxBytes := numeric.MustNewPrefixCodedInt64(maxBound, shift)
|
2014-08-03 01:05:58 +02:00
|
|
|
return newRangeBytes(minBytes, maxBytes)
|
|
|
|
}
|
|
|
|
|
|
|
|
func newRangeBytes(minBytes, maxBytes []byte) *termRange {
|
|
|
|
return &termRange{
|
|
|
|
startTerm: minBytes,
|
|
|
|
endTerm: maxBytes,
|
|
|
|
}
|
|
|
|
}
|
2014-08-30 00:14:12 +02:00
|
|
|
|
|
|
|
func (s *NumericRangeSearcher) Min() int {
|
|
|
|
return 0
|
|
|
|
}
|
2016-08-09 04:21:47 +02:00
|
|
|
|
|
|
|
func (s *NumericRangeSearcher) DocumentMatchPoolSize() int {
|
|
|
|
return s.searcher.DocumentMatchPoolSize()
|
|
|
|
}
|