From 77101ae424bb385095b8094dc9fd1cd116a3de8c Mon Sep 17 00:00:00 2001 From: Marty Schoch Date: Wed, 31 May 2017 12:30:32 -0400 Subject: [PATCH] filter numeric range terms against the term dictionary 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. --- search/searcher/search_numeric_range.go | 38 +++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/search/searcher/search_numeric_range.go b/search/searcher/search_numeric_range.go index 224cc9e2..333fbe58 100644 --- a/search/searcher/search_numeric_range.go +++ b/search/searcher/search_numeric_range.go @@ -17,6 +17,7 @@ package searcher import ( "bytes" "math" + "sort" "github.com/blevesearch/bleve/index" "github.com/blevesearch/bleve/numeric" @@ -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() } @@ -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