Skip to content

Commit

Permalink
filter numeric range terms against the term dictionary
Browse files Browse the repository at this point in the history
previously, all numeric terms required to implement a numeric
range search were passed to the disjunction query (possibly
exceeding the disjunction clause limit)

now, after producing the list of terms, we filter them against
the terms which actually exist in the term dictionary.  the
theory is that this will often greatly reduce the number of terms
and therefore reduce the likelihood that you would run into the
disjunction term limit in practice.

because the term dictionary interface does not have a seek API
and we're reluctant to add that now, i chose to do a binary
search of the terms, which either finds the term, or not. then
subsequent binary searches can proceed from that position,
since both the list of terms and the term dictionary are sorted.
  • Loading branch information
mschoch committed May 31, 2017
1 parent cd5b307 commit 77101ae
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions search/searcher/search_numeric_range.go
Expand Up @@ -17,6 +17,7 @@ package searcher
import (
"bytes"
"math"
"sort"

"github.com/blevesearch/bleve/index"
"github.com/blevesearch/bleve/numeric"
Expand Down Expand Up @@ -55,6 +56,17 @@ func NewNumericRangeSearcher(indexReader index.IndexReader,
// FIXME hard-coded precision, should match field declaration
termRanges := splitInt64Range(minInt64, maxInt64, 4)
terms := termRanges.Enumerate()
if len(terms) < 1 {
return NewMatchNoneSearcher(indexReader)
}
var err error
terms, err = filterCandidateTerms(indexReader, terms, field)
if err != nil {
return nil, err
}
if len(terms) < 1 {
return NewMatchNoneSearcher(indexReader)
}
if tooManyClauses(len(terms)) {
return nil, tooManyClausesErr()
}
Expand All @@ -63,6 +75,32 @@ func NewNumericRangeSearcher(indexReader index.IndexReader,
true)
}

func filterCandidateTerms(indexReader index.IndexReader,
terms [][]byte, field string) (rv [][]byte, err error) {
fieldDict, err := indexReader.FieldDictRange(field, terms[0], terms[len(terms)-1])
if err != nil {
return nil, err
}

// enumerate the terms and check against list of terms
tfd, err := fieldDict.Next()
for err == nil && tfd != nil {
termBytes := []byte(tfd.Term)
i := sort.Search(len(terms), func(i int) bool { return bytes.Compare(terms[i], termBytes) >= 0 })
if i < len(terms) && bytes.Compare(terms[i], termBytes) == 0 {
rv = append(rv, terms[i])
}
terms = terms[i:]
tfd, err = fieldDict.Next()
}

if cerr := fieldDict.Close(); cerr != nil && err == nil {
err = cerr
}

return rv, err
}

type termRange struct {
startTerm []byte
endTerm []byte
Expand Down

0 comments on commit 77101ae

Please sign in to comment.