1515package searcher
1616
1717import (
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
4740type FilteringSearcher struct {
48- child search.Searcher
49- accept FilterFunc
50- constraint ConstraintFunc
41+ child search.Searcher
42+ accept FilterFunc
5143}
5244
5345func 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
7852func (f * FilteringSearcher ) Size () int {
7953 return reflectStaticSizeFilteringSearcher + size .SizeOfPtr +
@@ -83,18 +57,14 @@ func (f *FilteringSearcher) Size() int {
8357func (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-
9868func (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 )
0 commit comments