0
0
Fork 0
bleve/search/scorers/scorer_disjunction.go

73 lines
2.2 KiB
Go
Raw Normal View History

2014-04-25 17:31:28 +02:00
// Copyright (c) 2014 Couchbase, Inc.
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
// except in compliance with the License. You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software distributed under the
// License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
// either express or implied. See the License for the specific language governing permissions
// and limitations under the License.
package scorers
2014-04-25 17:31:28 +02:00
import (
"fmt"
"github.com/blevesearch/bleve/search"
2014-04-25 17:31:28 +02:00
)
type DisjunctionQueryScorer struct {
2014-04-25 17:31:28 +02:00
explain bool
}
func NewDisjunctionQueryScorer(explain bool) *DisjunctionQueryScorer {
return &DisjunctionQueryScorer{
2014-04-25 17:31:28 +02:00
explain: explain,
}
}
func (s *DisjunctionQueryScorer) Score(ctx *search.SearchContext, constituents []*search.DocumentMatch, countMatch, countTotal int) *search.DocumentMatch {
2014-04-25 17:31:28 +02:00
var sum float64
var childrenExplanations []*search.Explanation
2014-04-25 17:31:28 +02:00
if s.explain {
childrenExplanations = make([]*search.Explanation, len(constituents))
2014-04-25 17:31:28 +02:00
}
locations := []search.FieldTermLocationMap{}
2014-04-25 17:31:28 +02:00
for i, docMatch := range constituents {
sum += docMatch.Score
if s.explain {
childrenExplanations[i] = docMatch.Expl
}
if docMatch.Locations != nil {
locations = append(locations, docMatch.Locations)
}
}
var rawExpl *search.Explanation
2014-04-25 17:31:28 +02:00
if s.explain {
rawExpl = &search.Explanation{Value: sum, Message: "sum of:", Children: childrenExplanations}
2014-04-25 17:31:28 +02:00
}
coord := float64(countMatch) / float64(countTotal)
newScore := sum * coord
var newExpl *search.Explanation
2014-04-25 17:31:28 +02:00
if s.explain {
ce := make([]*search.Explanation, 2)
2014-04-25 17:31:28 +02:00
ce[0] = rawExpl
ce[1] = &search.Explanation{Value: coord, Message: fmt.Sprintf("coord(%d/%d)", countMatch, countTotal)}
newExpl = &search.Explanation{Value: newScore, Message: "product of:", Children: ce}
2014-04-25 17:31:28 +02:00
}
// reuse constituents[0] as the return value
rv := constituents[0]
rv.Score = newScore
rv.Expl = newExpl
2014-04-25 17:31:28 +02:00
if len(locations) == 1 {
rv.Locations = locations[0]
} else if len(locations) > 1 {
rv.Locations = search.MergeLocations(locations)
2014-04-25 17:31:28 +02:00
}
return rv
2014-04-25 17:31:28 +02:00
}