Skip to content

Commit 65146f5

Browse files
author
Sreekanth Sivasankaran
authored
Merge pull request #1288 from sreekanth-cb/score_none
compute any score only when it is really needed
2 parents bd14714 + f84273e commit 65146f5

File tree

1 file changed

+45
-42
lines changed

1 file changed

+45
-42
lines changed

search/scorer/scorer_term.go

Lines changed: 45 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -115,58 +115,61 @@ func (s *TermQueryScorer) SetQueryNorm(qnorm float64) {
115115
}
116116

117117
func (s *TermQueryScorer) Score(ctx *search.SearchContext, termMatch *index.TermFieldDoc) *search.DocumentMatch {
118-
var scoreExplanation *search.Explanation
119-
120-
// need to compute score
121-
var tf float64
122-
if termMatch.Freq < MaxSqrtCache {
123-
tf = SqrtCache[int(termMatch.Freq)]
124-
} else {
125-
tf = math.Sqrt(float64(termMatch.Freq))
126-
}
127-
score := tf * termMatch.Norm * s.idf
128-
129-
if s.options.Explain {
130-
childrenExplanations := make([]*search.Explanation, 3)
131-
childrenExplanations[0] = &search.Explanation{
132-
Value: tf,
133-
Message: fmt.Sprintf("tf(termFreq(%s:%s)=%d", s.queryField, s.queryTerm, termMatch.Freq),
134-
}
135-
childrenExplanations[1] = &search.Explanation{
136-
Value: termMatch.Norm,
137-
Message: fmt.Sprintf("fieldNorm(field=%s, doc=%s)", s.queryField, termMatch.ID),
138-
}
139-
childrenExplanations[2] = s.idfExplanation
140-
scoreExplanation = &search.Explanation{
141-
Value: score,
142-
Message: fmt.Sprintf("fieldWeight(%s:%s in %s), product of:", s.queryField, s.queryTerm, termMatch.ID),
143-
Children: childrenExplanations,
118+
rv := ctx.DocumentMatchPool.Get()
119+
// perform any score computations only when needed
120+
if s.includeScore || s.options.Explain {
121+
var scoreExplanation *search.Explanation
122+
var tf float64
123+
if termMatch.Freq < MaxSqrtCache {
124+
tf = SqrtCache[int(termMatch.Freq)]
125+
} else {
126+
tf = math.Sqrt(float64(termMatch.Freq))
144127
}
145-
}
128+
score := tf * termMatch.Norm * s.idf
146129

147-
// if the query weight isn't 1, multiply
148-
if s.queryWeight != 1.0 {
149-
score = score * s.queryWeight
150130
if s.options.Explain {
151-
childExplanations := make([]*search.Explanation, 2)
152-
childExplanations[0] = s.queryWeightExplanation
153-
childExplanations[1] = scoreExplanation
131+
childrenExplanations := make([]*search.Explanation, 3)
132+
childrenExplanations[0] = &search.Explanation{
133+
Value: tf,
134+
Message: fmt.Sprintf("tf(termFreq(%s:%s)=%d", s.queryField, s.queryTerm, termMatch.Freq),
135+
}
136+
childrenExplanations[1] = &search.Explanation{
137+
Value: termMatch.Norm,
138+
Message: fmt.Sprintf("fieldNorm(field=%s, doc=%s)", s.queryField, termMatch.ID),
139+
}
140+
childrenExplanations[2] = s.idfExplanation
154141
scoreExplanation = &search.Explanation{
155142
Value: score,
156-
Message: fmt.Sprintf("weight(%s:%s^%f in %s), product of:", s.queryField, s.queryTerm, s.queryBoost, termMatch.ID),
157-
Children: childExplanations,
143+
Message: fmt.Sprintf("fieldWeight(%s:%s in %s), product of:", s.queryField, s.queryTerm, termMatch.ID),
144+
Children: childrenExplanations,
158145
}
159146
}
147+
148+
// if the query weight isn't 1, multiply
149+
if s.queryWeight != 1.0 {
150+
score = score * s.queryWeight
151+
if s.options.Explain {
152+
childExplanations := make([]*search.Explanation, 2)
153+
childExplanations[0] = s.queryWeightExplanation
154+
childExplanations[1] = scoreExplanation
155+
scoreExplanation = &search.Explanation{
156+
Value: score,
157+
Message: fmt.Sprintf("weight(%s:%s^%f in %s), product of:", s.queryField, s.queryTerm, s.queryBoost, termMatch.ID),
158+
Children: childExplanations,
159+
}
160+
}
161+
}
162+
163+
if s.includeScore {
164+
rv.Score = score
165+
}
166+
167+
if s.options.Explain {
168+
rv.Expl = scoreExplanation
169+
}
160170
}
161171

162-
rv := ctx.DocumentMatchPool.Get()
163172
rv.IndexInternalID = append(rv.IndexInternalID, termMatch.ID...)
164-
if s.includeScore {
165-
rv.Score = score
166-
}
167-
if s.options.Explain {
168-
rv.Expl = scoreExplanation
169-
}
170173

171174
if len(termMatch.Vectors) > 0 {
172175
if cap(rv.FieldTermLocations) < len(termMatch.Vectors) {

0 commit comments

Comments
 (0)