Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Public scorer #1448

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions search/scorer/scorer_conjunction.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
23 changes: 15 additions & 8 deletions search/scorer/scorer_constant.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand All @@ -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,
Expand All @@ -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
Expand All @@ -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
Expand Down
17 changes: 11 additions & 6 deletions search/scorer/scorer_disjunction.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
23 changes: 15 additions & 8 deletions search/scorer/scorer_term.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)

Expand All @@ -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,
Expand All @@ -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
Expand All @@ -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 {
Expand Down
6 changes: 5 additions & 1 deletion search/searcher/search_boolean.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -448,3 +448,7 @@ func (s *BooleanSearcher) DocumentMatchPoolSize() int {
}
return rv
}

func (s *BooleanSearcher) SetScorer(scorer scorer.ConjunctionQueryScorer) {
s.scorer = scorer
}
6 changes: 5 additions & 1 deletion search/searcher/search_conjunction.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -282,3 +282,7 @@ func (s *ConjunctionSearcher) DocumentMatchPoolSize() int {
}
return rv
}

func (s *ConjunctionSearcher) SetScorer(scorer scorer.ConjunctionQueryScorer) {
s.scorer = scorer
}
6 changes: 5 additions & 1 deletion search/searcher/search_disjunction_heap.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ type DisjunctionHeapSearcher struct {
indexReader index.IndexReader

numSearchers int
scorer *scorer.DisjunctionQueryScorer
scorer scorer.DisjunctionQueryScorer
min int
queryNorm float64
initialized bool
Expand Down Expand Up @@ -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
}
6 changes: 5 additions & 1 deletion search/searcher/search_disjunction_slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion search/searcher/search_docid.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}
6 changes: 5 additions & 1 deletion search/searcher/search_match_all.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func init() {
type MatchAllSearcher struct {
indexReader index.IndexReader
reader index.DocIDReader
scorer *scorer.ConstantScorer
scorer scorer.ConstantScorer
count uint64
}

Expand Down Expand Up @@ -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
}
6 changes: 5 additions & 1 deletion search/searcher/search_term.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func init() {
type TermSearcher struct {
indexReader index.IndexReader
reader index.TermFieldReader
scorer *scorer.TermQueryScorer
scorer scorer.TermQueryScorer
tfd index.TermFieldDoc
}

Expand Down Expand Up @@ -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
}