You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

scorer_disjunction.go 2.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. // Copyright (c) 2014 Couchbase, Inc.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package scorer
  15. import (
  16. "fmt"
  17. "github.com/blevesearch/bleve/search"
  18. )
  19. type DisjunctionQueryScorer struct {
  20. options search.SearcherOptions
  21. }
  22. func NewDisjunctionQueryScorer(options search.SearcherOptions) *DisjunctionQueryScorer {
  23. return &DisjunctionQueryScorer{
  24. options: options,
  25. }
  26. }
  27. func (s *DisjunctionQueryScorer) Score(ctx *search.SearchContext, constituents []*search.DocumentMatch, countMatch, countTotal int) *search.DocumentMatch {
  28. var sum float64
  29. var childrenExplanations []*search.Explanation
  30. if s.options.Explain {
  31. childrenExplanations = make([]*search.Explanation, len(constituents))
  32. }
  33. var locations []search.FieldTermLocationMap
  34. for i, docMatch := range constituents {
  35. sum += docMatch.Score
  36. if s.options.Explain {
  37. childrenExplanations[i] = docMatch.Expl
  38. }
  39. if docMatch.Locations != nil {
  40. locations = append(locations, docMatch.Locations)
  41. }
  42. }
  43. var rawExpl *search.Explanation
  44. if s.options.Explain {
  45. rawExpl = &search.Explanation{Value: sum, Message: "sum of:", Children: childrenExplanations}
  46. }
  47. coord := float64(countMatch) / float64(countTotal)
  48. newScore := sum * coord
  49. var newExpl *search.Explanation
  50. if s.options.Explain {
  51. ce := make([]*search.Explanation, 2)
  52. ce[0] = rawExpl
  53. ce[1] = &search.Explanation{Value: coord, Message: fmt.Sprintf("coord(%d/%d)", countMatch, countTotal)}
  54. newExpl = &search.Explanation{Value: newScore, Message: "product of:", Children: ce}
  55. }
  56. // reuse constituents[0] as the return value
  57. rv := constituents[0]
  58. rv.Score = newScore
  59. rv.Expl = newExpl
  60. if len(locations) == 1 {
  61. rv.Locations = locations[0]
  62. } else if len(locations) > 1 {
  63. rv.Locations = search.MergeLocations(locations)
  64. }
  65. return rv
  66. }