From c73de3e59865af3e13fd5d650e8e747ea56d55af Mon Sep 17 00:00:00 2001 From: Pavel Bazika Date: Wed, 19 Aug 2020 13:21:44 +0200 Subject: [PATCH 1/2] Scorers turned to interfaces --- search/scorer/scorer_conjunction.go | 17 +++++++++------ search/scorer/scorer_constant.go | 23 ++++++++++++++------- search/scorer/scorer_disjunction.go | 17 +++++++++------ search/scorer/scorer_term.go | 23 ++++++++++++++------- search/searcher/search_boolean.go | 2 +- search/searcher/search_conjunction.go | 2 +- search/searcher/search_disjunction_heap.go | 2 +- search/searcher/search_disjunction_slice.go | 2 +- search/searcher/search_docid.go | 2 +- search/searcher/search_match_all.go | 2 +- search/searcher/search_term.go | 2 +- 11 files changed, 59 insertions(+), 35 deletions(-) diff --git a/search/scorer/scorer_conjunction.go b/search/scorer/scorer_conjunction.go index 48cdf3ae9..5092403b2 100644 --- a/search/scorer/scorer_conjunction.go +++ b/search/scorer/scorer_conjunction.go @@ -24,25 +24,30 @@ import ( var reflectStaticSizeConjunctionQueryScorer int func init() { - var cqs ConjunctionQueryScorer + var cqs conjunctionQueryScorer reflectStaticSizeConjunctionQueryScorer = int(reflect.TypeOf(cqs).Size()) } -type ConjunctionQueryScorer struct { +type ConjunctionQueryScorer interface { + Size() int + Score(ctx *search.SearchContext, constituents []*search.DocumentMatch) *search.DocumentMatch +} + +type conjunctionQueryScorer struct { options search.SearcherOptions } -func (s *ConjunctionQueryScorer) Size() int { +func (s *conjunctionQueryScorer) Size() int { return reflectStaticSizeConjunctionQueryScorer + size.SizeOfPtr } -func NewConjunctionQueryScorer(options search.SearcherOptions) *ConjunctionQueryScorer { - return &ConjunctionQueryScorer{ +func NewConjunctionQueryScorer(options search.SearcherOptions) ConjunctionQueryScorer { + return &conjunctionQueryScorer{ options: options, } } -func (s *ConjunctionQueryScorer) Score(ctx *search.SearchContext, constituents []*search.DocumentMatch) *search.DocumentMatch { +func (s *conjunctionQueryScorer) Score(ctx *search.SearchContext, constituents []*search.DocumentMatch) *search.DocumentMatch { var sum float64 var childrenExplanations []*search.Explanation if s.options.Explain { diff --git a/search/scorer/scorer_constant.go b/search/scorer/scorer_constant.go index dc10fdaa4..22ff85d39 100644 --- a/search/scorer/scorer_constant.go +++ b/search/scorer/scorer_constant.go @@ -26,11 +26,18 @@ import ( var reflectStaticSizeConstantScorer int func init() { - var cs ConstantScorer + var cs constantScorer reflectStaticSizeConstantScorer = int(reflect.TypeOf(cs).Size()) } -type ConstantScorer struct { +type ConstantScorer interface { + Size() int + Weight() float64 + SetQueryNorm(qnorm float64) + Score(ctx *search.SearchContext, id index.IndexInternalID) *search.DocumentMatch +} + +type constantScorer struct { constant float64 boost float64 options search.SearcherOptions @@ -39,7 +46,7 @@ type ConstantScorer struct { queryWeightExplanation *search.Explanation } -func (s *ConstantScorer) Size() int { +func (s *constantScorer) Size() int { sizeInBytes := reflectStaticSizeConstantScorer + size.SizeOfPtr if s.queryWeightExplanation != nil { @@ -49,8 +56,8 @@ func (s *ConstantScorer) Size() int { return sizeInBytes } -func NewConstantScorer(constant float64, boost float64, options search.SearcherOptions) *ConstantScorer { - rv := ConstantScorer{ +func NewConstantScorer(constant float64, boost float64, options search.SearcherOptions) ConstantScorer { + rv := constantScorer{ options: options, queryWeight: 1.0, constant: constant, @@ -60,12 +67,12 @@ func NewConstantScorer(constant float64, boost float64, options search.SearcherO return &rv } -func (s *ConstantScorer) Weight() float64 { +func (s *constantScorer) Weight() float64 { sum := s.boost return sum * sum } -func (s *ConstantScorer) SetQueryNorm(qnorm float64) { +func (s *constantScorer) SetQueryNorm(qnorm float64) { s.queryNorm = qnorm // update the query weight @@ -89,7 +96,7 @@ func (s *ConstantScorer) SetQueryNorm(qnorm float64) { } } -func (s *ConstantScorer) Score(ctx *search.SearchContext, id index.IndexInternalID) *search.DocumentMatch { +func (s *constantScorer) Score(ctx *search.SearchContext, id index.IndexInternalID) *search.DocumentMatch { var scoreExplanation *search.Explanation score := s.constant diff --git a/search/scorer/scorer_disjunction.go b/search/scorer/scorer_disjunction.go index 7a955e168..a07a02523 100644 --- a/search/scorer/scorer_disjunction.go +++ b/search/scorer/scorer_disjunction.go @@ -25,25 +25,30 @@ import ( var reflectStaticSizeDisjunctionQueryScorer int func init() { - var dqs DisjunctionQueryScorer + var dqs disjunctionQueryScorer reflectStaticSizeDisjunctionQueryScorer = int(reflect.TypeOf(dqs).Size()) } -type DisjunctionQueryScorer struct { +type DisjunctionQueryScorer interface { + Size() int + Score(ctx *search.SearchContext, constituents []*search.DocumentMatch, countMatch, countTotal int) *search.DocumentMatch +} + +type disjunctionQueryScorer struct { options search.SearcherOptions } -func (s *DisjunctionQueryScorer) Size() int { +func (s *disjunctionQueryScorer) Size() int { return reflectStaticSizeDisjunctionQueryScorer + size.SizeOfPtr } -func NewDisjunctionQueryScorer(options search.SearcherOptions) *DisjunctionQueryScorer { - return &DisjunctionQueryScorer{ +func NewDisjunctionQueryScorer(options search.SearcherOptions) DisjunctionQueryScorer { + return &disjunctionQueryScorer{ options: options, } } -func (s *DisjunctionQueryScorer) Score(ctx *search.SearchContext, constituents []*search.DocumentMatch, countMatch, countTotal int) *search.DocumentMatch { +func (s *disjunctionQueryScorer) Score(ctx *search.SearchContext, constituents []*search.DocumentMatch, countMatch, countTotal int) *search.DocumentMatch { var sum float64 var childrenExplanations []*search.Explanation if s.options.Explain { diff --git a/search/scorer/scorer_term.go b/search/scorer/scorer_term.go index 718de2ea5..c8d4f2d60 100644 --- a/search/scorer/scorer_term.go +++ b/search/scorer/scorer_term.go @@ -27,11 +27,18 @@ import ( var reflectStaticSizeTermQueryScorer int func init() { - var tqs TermQueryScorer + var tqs termQueryScorer reflectStaticSizeTermQueryScorer = int(reflect.TypeOf(tqs).Size()) } -type TermQueryScorer struct { +type TermQueryScorer interface { + Size() int + Weight() float64 + SetQueryNorm(qnorm float64) + Score(ctx *search.SearchContext, termMatch *index.TermFieldDoc) *search.DocumentMatch +} + +type termQueryScorer struct { queryTerm string queryField string queryBoost float64 @@ -46,7 +53,7 @@ type TermQueryScorer struct { queryWeightExplanation *search.Explanation } -func (s *TermQueryScorer) Size() int { +func (s *termQueryScorer) Size() int { sizeInBytes := reflectStaticSizeTermQueryScorer + size.SizeOfPtr + len(s.queryTerm) + len(s.queryField) @@ -61,8 +68,8 @@ func (s *TermQueryScorer) Size() int { return sizeInBytes } -func NewTermQueryScorer(queryTerm []byte, queryField string, queryBoost float64, docTotal, docTerm uint64, options search.SearcherOptions) *TermQueryScorer { - rv := TermQueryScorer{ +func NewTermQueryScorer(queryTerm []byte, queryField string, queryBoost float64, docTotal, docTerm uint64, options search.SearcherOptions) TermQueryScorer { + rv := termQueryScorer{ queryTerm: string(queryTerm), queryField: queryField, queryBoost: queryBoost, @@ -84,12 +91,12 @@ func NewTermQueryScorer(queryTerm []byte, queryField string, queryBoost float64, return &rv } -func (s *TermQueryScorer) Weight() float64 { +func (s *termQueryScorer) Weight() float64 { sum := s.queryBoost * s.idf return sum * sum } -func (s *TermQueryScorer) SetQueryNorm(qnorm float64) { +func (s *termQueryScorer) SetQueryNorm(qnorm float64) { s.queryNorm = qnorm // update the query weight @@ -114,7 +121,7 @@ func (s *TermQueryScorer) SetQueryNorm(qnorm float64) { } } -func (s *TermQueryScorer) Score(ctx *search.SearchContext, termMatch *index.TermFieldDoc) *search.DocumentMatch { +func (s *termQueryScorer) Score(ctx *search.SearchContext, termMatch *index.TermFieldDoc) *search.DocumentMatch { rv := ctx.DocumentMatchPool.Get() // perform any score computations only when needed if s.includeScore || s.options.Explain { diff --git a/search/searcher/search_boolean.go b/search/searcher/search_boolean.go index 7f0bfa424..e827da66c 100644 --- a/search/searcher/search_boolean.go +++ b/search/searcher/search_boolean.go @@ -42,7 +42,7 @@ type BooleanSearcher struct { currMustNot *search.DocumentMatch currentID index.IndexInternalID min uint64 - scorer *scorer.ConjunctionQueryScorer + scorer scorer.ConjunctionQueryScorer matches []*search.DocumentMatch initialized bool done bool diff --git a/search/searcher/search_conjunction.go b/search/searcher/search_conjunction.go index ac737bccd..2a65c1946 100644 --- a/search/searcher/search_conjunction.go +++ b/search/searcher/search_conjunction.go @@ -38,7 +38,7 @@ type ConjunctionSearcher struct { queryNorm float64 currs []*search.DocumentMatch maxIDIdx int - scorer *scorer.ConjunctionQueryScorer + scorer scorer.ConjunctionQueryScorer initialized bool options search.SearcherOptions } diff --git a/search/searcher/search_disjunction_heap.go b/search/searcher/search_disjunction_heap.go index 00b09fcb9..6492f98ea 100644 --- a/search/searcher/search_disjunction_heap.go +++ b/search/searcher/search_disjunction_heap.go @@ -46,7 +46,7 @@ type DisjunctionHeapSearcher struct { indexReader index.IndexReader numSearchers int - scorer *scorer.DisjunctionQueryScorer + scorer scorer.DisjunctionQueryScorer min int queryNorm float64 initialized bool diff --git a/search/searcher/search_disjunction_slice.go b/search/searcher/search_disjunction_slice.go index 464878bc6..d2c128273 100644 --- a/search/searcher/search_disjunction_slice.go +++ b/search/searcher/search_disjunction_slice.go @@ -38,7 +38,7 @@ type DisjunctionSliceSearcher struct { numSearchers int queryNorm float64 currs []*search.DocumentMatch - scorer *scorer.DisjunctionQueryScorer + scorer scorer.DisjunctionQueryScorer min int matching []*search.DocumentMatch matchingIdxs []int diff --git a/search/searcher/search_docid.go b/search/searcher/search_docid.go index 3b258a580..683c98cbf 100644 --- a/search/searcher/search_docid.go +++ b/search/searcher/search_docid.go @@ -33,7 +33,7 @@ func init() { // DocIDSearcher returns documents matching a predefined set of identifiers. type DocIDSearcher struct { reader index.DocIDReader - scorer *scorer.ConstantScorer + scorer scorer.ConstantScorer count int } diff --git a/search/searcher/search_match_all.go b/search/searcher/search_match_all.go index bb6640122..1df093724 100644 --- a/search/searcher/search_match_all.go +++ b/search/searcher/search_match_all.go @@ -33,7 +33,7 @@ func init() { type MatchAllSearcher struct { indexReader index.IndexReader reader index.DocIDReader - scorer *scorer.ConstantScorer + scorer scorer.ConstantScorer count uint64 } diff --git a/search/searcher/search_term.go b/search/searcher/search_term.go index c1af74c76..4bcf4420c 100644 --- a/search/searcher/search_term.go +++ b/search/searcher/search_term.go @@ -33,7 +33,7 @@ func init() { type TermSearcher struct { indexReader index.IndexReader reader index.TermFieldReader - scorer *scorer.TermQueryScorer + scorer scorer.TermQueryScorer tfd index.TermFieldDoc } From 2c43fc7ac4be6f797bfe8dc7d414f7881ce826c9 Mon Sep 17 00:00:00 2001 From: Pavel Bazika Date: Wed, 19 Aug 2020 13:22:17 +0200 Subject: [PATCH 2/2] SetScorer method added to searchers --- search/searcher/search_boolean.go | 4 ++++ search/searcher/search_conjunction.go | 4 ++++ search/searcher/search_disjunction_heap.go | 4 ++++ search/searcher/search_disjunction_slice.go | 4 ++++ search/searcher/search_docid.go | 4 ++++ search/searcher/search_match_all.go | 4 ++++ search/searcher/search_term.go | 4 ++++ 7 files changed, 28 insertions(+) diff --git a/search/searcher/search_boolean.go b/search/searcher/search_boolean.go index e827da66c..6f5f934d4 100644 --- a/search/searcher/search_boolean.go +++ b/search/searcher/search_boolean.go @@ -448,3 +448,7 @@ func (s *BooleanSearcher) DocumentMatchPoolSize() int { } return rv } + +func (s *BooleanSearcher) SetScorer(scorer scorer.ConjunctionQueryScorer) { + s.scorer = scorer +} diff --git a/search/searcher/search_conjunction.go b/search/searcher/search_conjunction.go index 2a65c1946..5e5a7a895 100644 --- a/search/searcher/search_conjunction.go +++ b/search/searcher/search_conjunction.go @@ -282,3 +282,7 @@ func (s *ConjunctionSearcher) DocumentMatchPoolSize() int { } return rv } + +func (s *ConjunctionSearcher) SetScorer(scorer scorer.ConjunctionQueryScorer) { + s.scorer = scorer +} diff --git a/search/searcher/search_disjunction_heap.go b/search/searcher/search_disjunction_heap.go index 6492f98ea..3d5aa92dc 100644 --- a/search/searcher/search_disjunction_heap.go +++ b/search/searcher/search_disjunction_heap.go @@ -341,3 +341,7 @@ func (s *DisjunctionHeapSearcher) Pop() interface{} { s.heap = old[0 : n-1] return x } + +func (s *DisjunctionHeapSearcher) SetScorer(scorer scorer.DisjunctionQueryScorer) { + s.scorer = scorer +} diff --git a/search/searcher/search_disjunction_slice.go b/search/searcher/search_disjunction_slice.go index d2c128273..3cb77a28f 100644 --- a/search/searcher/search_disjunction_slice.go +++ b/search/searcher/search_disjunction_slice.go @@ -282,6 +282,10 @@ func (s *DisjunctionSliceSearcher) DocumentMatchPoolSize() int { return rv } +func (s *DisjunctionSliceSearcher) SetScorer(scorer scorer.DisjunctionQueryScorer) { + s.scorer = scorer +} + // a disjunction searcher implements the index.Optimizable interface // but only activates on an edge case where the disjunction is a // wrapper around a single Optimizable child searcher diff --git a/search/searcher/search_docid.go b/search/searcher/search_docid.go index 683c98cbf..08aa9f16c 100644 --- a/search/searcher/search_docid.go +++ b/search/searcher/search_docid.go @@ -107,3 +107,7 @@ func (s *DocIDSearcher) Min() int { func (s *DocIDSearcher) DocumentMatchPoolSize() int { return 1 } + +func (s *DocIDSearcher) SetScorer(scorer scorer.ConstantScorer) { + s.scorer = scorer +} diff --git a/search/searcher/search_match_all.go b/search/searcher/search_match_all.go index 1df093724..14a912d22 100644 --- a/search/searcher/search_match_all.go +++ b/search/searcher/search_match_all.go @@ -119,3 +119,7 @@ func (s *MatchAllSearcher) Min() int { func (s *MatchAllSearcher) DocumentMatchPoolSize() int { return 1 } + +func (s *MatchAllSearcher) SetScorer(scorer scorer.ConstantScorer) { + s.scorer = scorer +} diff --git a/search/searcher/search_term.go b/search/searcher/search_term.go index 4bcf4420c..c963f6c45 100644 --- a/search/searcher/search_term.go +++ b/search/searcher/search_term.go @@ -139,3 +139,7 @@ func (s *TermSearcher) Optimize(kind string, octx index.OptimizableContext) ( return octx, nil } + +func (s *TermSearcher) SetScorer(scorer scorer.TermQueryScorer) { + s.scorer = scorer +}