Skip to content

Commit 231baf2

Browse files
refactor
1 parent 3540165 commit 231baf2

File tree

6 files changed

+27
-44
lines changed

6 files changed

+27
-44
lines changed

search/query/boolean.go

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package query
1616

1717
import (
18+
"bytes"
1819
"context"
1920
"encoding/json"
2021
"fmt"
@@ -183,7 +184,7 @@ func (q *BooleanQuery) Searcher(ctx context.Context, i index.IndexReader, m mapp
183184
}
184185
}
185186

186-
var filterSearcher search.Searcher
187+
var filterFunc searcher.FilterFunc
187188
if q.Filter != nil {
188189
// create a new searcher options with disabled scoring, since filter should not affect scoring
189190
// and we don't want to pay the cost of scoring if we don't need it, also disable term vectors
@@ -193,13 +194,25 @@ func (q *BooleanQuery) Searcher(ctx context.Context, i index.IndexReader, m mapp
193194
IncludeTermVectors: false,
194195
Score: "none",
195196
}
196-
filterSearcher, err = q.Filter.Searcher(ctx, i, m, filterOptions)
197+
filterSearcher, err := q.Filter.Searcher(ctx, i, m, filterOptions)
197198
if err != nil {
198199
return nil, err
199200
}
201+
filterFunc = func(sctx *search.SearchContext, d *search.DocumentMatch) bool {
202+
// Attempt to advance the filter searcher to the document identified by
203+
// the base searcher's (unfiltered boolean) current result (d.IndexInternalID).
204+
//
205+
// If the filter searcher successfully finds a document with the same
206+
// internal ID, it means the document satisfies the filter and should be kept.
207+
//
208+
// If the filter searcher returns an error, does not find a matching document,
209+
// or finds a document with a different internal ID, the document should be discarded.
210+
dm, err := filterSearcher.Advance(sctx, d.IndexInternalID)
211+
return err == nil && dm != nil && bytes.Equal(dm.IndexInternalID, d.IndexInternalID)
212+
}
200213
}
201214
// optimization, if only should searcher, just return it instead
202-
if mustSearcher == nil && shouldSearcher != nil && mustNotSearcher == nil && filterSearcher == nil {
215+
if mustSearcher == nil && shouldSearcher != nil && mustNotSearcher == nil && filterFunc == nil {
203216
return shouldSearcher, nil
204217
}
205218

@@ -208,8 +221,8 @@ func (q *BooleanQuery) Searcher(ctx context.Context, i index.IndexReader, m mapp
208221
return nil, err
209222
}
210223

211-
if filterSearcher != nil {
212-
return searcher.NewConstrainedSearcher(ctx, bs, filterSearcher), nil
224+
if filterFunc != nil {
225+
return searcher.NewFilteringSearcher(ctx, bs, filterFunc), nil
213226
}
214227
return bs, nil
215228
}

search/searcher/search_filter.go

Lines changed: 5 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
package searcher
1616

1717
import (
18-
"bytes"
1918
"context"
2019
"reflect"
2120

@@ -34,20 +33,13 @@ func init() {
3433
// FilterFunc defines a function which can filter documents
3534
// returning true means keep the document
3635
// returning false means do not keep the document
37-
type FilterFunc func(d *search.DocumentMatch) bool
38-
39-
// ConstraintFunc defines a function which can constrain documents
40-
// returning true means keep the document
41-
// returning false means do not keep the document
42-
// This is similar to FilterFunc, but takes a SearchContext
43-
type ConstraintFunc func(sctx *search.SearchContext, d *search.DocumentMatch) bool
36+
type FilterFunc func(sctx *search.SearchContext, d *search.DocumentMatch) bool
4437

4538
// FilteringSearcher wraps any other searcher, but checks any Next/Advance
4639
// call against the supplied FilterFunc
4740
type FilteringSearcher struct {
48-
child search.Searcher
49-
accept FilterFunc
50-
constraint ConstraintFunc
41+
child search.Searcher
42+
accept FilterFunc
5143
}
5244

5345
func NewFilteringSearcher(ctx context.Context, s search.Searcher, filter FilterFunc) *FilteringSearcher {
@@ -56,24 +48,6 @@ func NewFilteringSearcher(ctx context.Context, s search.Searcher, filter FilterF
5648
accept: filter,
5749
}
5850
}
59-
func NewConstrainedSearcher(ctx context.Context, baseSearcher search.Searcher, constraint search.Searcher) *FilteringSearcher {
60-
constraintFunc := func(sctx *search.SearchContext, d *search.DocumentMatch) bool {
61-
// Attempt to advance the constraint searcher to the document identified by
62-
// the base searcher's current result (d.IndexInternalID).
63-
//
64-
// If the constraint searcher successfully finds a document with the same
65-
// internal ID, it means the document satisfies the constraint and should be kept.
66-
//
67-
// If the constraint searcher returns an error, does not find a matching document,
68-
// or finds a document with a different internal ID, the document should be discarded.
69-
dm, err := constraint.Advance(sctx, d.IndexInternalID)
70-
return err == nil && dm != nil && bytes.Equal(dm.IndexInternalID, d.IndexInternalID)
71-
}
72-
return &FilteringSearcher{
73-
child: baseSearcher,
74-
constraint: constraintFunc,
75-
}
76-
}
7751

7852
func (f *FilteringSearcher) Size() int {
7953
return reflectStaticSizeFilteringSearcher + size.SizeOfPtr +
@@ -83,18 +57,14 @@ func (f *FilteringSearcher) Size() int {
8357
func (f *FilteringSearcher) Next(ctx *search.SearchContext) (*search.DocumentMatch, error) {
8458
next, err := f.child.Next(ctx)
8559
for next != nil && err == nil {
86-
if f.passesFilter(ctx, next) {
60+
if f.accept(ctx, next) {
8761
return next, nil
8862
}
8963
next, err = f.child.Next(ctx)
9064
}
9165
return nil, err
9266
}
9367

94-
func (f *FilteringSearcher) passesFilter(ctx *search.SearchContext, d *search.DocumentMatch) bool {
95-
return (f.accept == nil || f.accept(d)) && (f.constraint == nil || f.constraint(ctx, d))
96-
}
97-
9868
func (f *FilteringSearcher) Advance(ctx *search.SearchContext, ID index.IndexInternalID) (*search.DocumentMatch, error) {
9969
adv, err := f.child.Advance(ctx, ID)
10070
if err != nil {
@@ -103,7 +73,7 @@ func (f *FilteringSearcher) Advance(ctx *search.SearchContext, ID index.IndexInt
10373
if adv == nil {
10474
return nil, nil
10575
}
106-
if f.passesFilter(ctx, adv) {
76+
if f.accept(ctx, adv) {
10777
return adv, nil
10878
}
10979
return f.Next(ctx)

search/searcher/search_geoboundingbox.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ func buildIsIndexedFunc(ctx context.Context, indexReader index.IndexReader, fiel
203203

204204
func buildRectFilter(ctx context.Context, dvReader index.DocValueReader, field string,
205205
minLon, minLat, maxLon, maxLat float64) FilterFunc {
206-
return func(d *search.DocumentMatch) bool {
206+
return func(sctx *search.SearchContext, d *search.DocumentMatch) bool {
207207
// check geo matches against all numeric type terms indexed
208208
var lons, lats []float64
209209
var found bool

search/searcher/search_geopointdistance.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ func boxSearcher(ctx context.Context, indexReader index.IndexReader,
115115

116116
func buildDistFilter(ctx context.Context, dvReader index.DocValueReader, field string,
117117
centerLon, centerLat, maxDist float64) FilterFunc {
118-
return func(d *search.DocumentMatch) bool {
118+
return func(sctx *search.SearchContext, d *search.DocumentMatch) bool {
119119
// check geo matches against all numeric type terms indexed
120120
var lons, lats []float64
121121
var found bool

search/searcher/search_geopolygon.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ func almostEqual(a, b float64) bool {
8585
// here: https://wrf.ecse.rpi.edu/nikola/pubdetails/pnpoly.html
8686
func buildPolygonFilter(ctx context.Context, dvReader index.DocValueReader, field string,
8787
coordinates []geo.Point) FilterFunc {
88-
return func(d *search.DocumentMatch) bool {
88+
return func(sctx *search.SearchContext, d *search.DocumentMatch) bool {
8989
// check geo matches against all numeric type terms indexed
9090
var lons, lats []float64
9191
var found bool

search/searcher/search_geoshape.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ func buildRelationFilterOnShapes(ctx context.Context, dvReader index.DocValueRea
7777
bufPool = ctx.Value(search.GeoBufferPoolCallbackKey).(search.GeoBufferPoolCallbackFunc)()
7878
}
7979

80-
return func(d *search.DocumentMatch) bool {
80+
return func(sctx *search.SearchContext, d *search.DocumentMatch) bool {
8181
var found bool
8282

8383
err := dvReader.VisitDocValues(d.IndexInternalID,

0 commit comments

Comments
 (0)