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.
|
2014-09-02 16:54:50 +02:00
|
|
|
|
2014-09-01 17:15:38 +02:00
|
|
|
package scorers
|
2014-04-25 17:31:28 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2014-09-01 17:15:38 +02:00
|
|
|
|
|
|
|
"github.com/blevesearch/bleve/search"
|
2014-04-25 17:31:28 +02:00
|
|
|
)
|
|
|
|
|
2014-08-07 02:03:41 +02:00
|
|
|
type DisjunctionQueryScorer struct {
|
2014-04-25 17:31:28 +02:00
|
|
|
explain bool
|
|
|
|
}
|
|
|
|
|
2014-08-07 02:03:41 +02:00
|
|
|
func NewDisjunctionQueryScorer(explain bool) *DisjunctionQueryScorer {
|
|
|
|
return &DisjunctionQueryScorer{
|
2014-04-25 17:31:28 +02:00
|
|
|
explain: explain,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-09 04:21:47 +02:00
|
|
|
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
|
2014-09-01 17:15:38 +02:00
|
|
|
var childrenExplanations []*search.Explanation
|
2014-04-25 17:31:28 +02:00
|
|
|
if s.explain {
|
2014-09-01 17:15:38 +02:00
|
|
|
childrenExplanations = make([]*search.Explanation, len(constituents))
|
2014-04-25 17:31:28 +02:00
|
|
|
}
|
|
|
|
|
2014-09-01 17:15:38 +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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-01 17:15:38 +02:00
|
|
|
var rawExpl *search.Explanation
|
2014-04-25 17:31:28 +02:00
|
|
|
if s.explain {
|
2014-09-01 17:15:38 +02:00
|
|
|
rawExpl = &search.Explanation{Value: sum, Message: "sum of:", Children: childrenExplanations}
|
2014-04-25 17:31:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
coord := float64(countMatch) / float64(countTotal)
|
2016-08-09 04:21:47 +02:00
|
|
|
newScore := sum * coord
|
|
|
|
var newExpl *search.Explanation
|
2014-04-25 17:31:28 +02:00
|
|
|
if s.explain {
|
2014-09-01 17:15:38 +02:00
|
|
|
ce := make([]*search.Explanation, 2)
|
2014-04-25 17:31:28 +02:00
|
|
|
ce[0] = rawExpl
|
2014-09-01 17:15:38 +02:00
|
|
|
ce[1] = &search.Explanation{Value: coord, Message: fmt.Sprintf("coord(%d/%d)", countMatch, countTotal)}
|
2016-08-09 04:21:47 +02:00
|
|
|
newExpl = &search.Explanation{Value: newScore, Message: "product of:", Children: ce}
|
2014-04-25 17:31:28 +02:00
|
|
|
}
|
|
|
|
|
2016-08-09 04:21:47 +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 {
|
2014-09-01 17:15:38 +02:00
|
|
|
rv.Locations = search.MergeLocations(locations)
|
2014-04-25 17:31:28 +02:00
|
|
|
}
|
|
|
|
|
2016-08-09 04:21:47 +02:00
|
|
|
return rv
|
2014-04-25 17:31:28 +02:00
|
|
|
}
|