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_conjunction.go 1.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. "github.com/blevesearch/bleve/search"
  17. )
  18. type ConjunctionQueryScorer struct {
  19. options search.SearcherOptions
  20. }
  21. func NewConjunctionQueryScorer(options search.SearcherOptions) *ConjunctionQueryScorer {
  22. return &ConjunctionQueryScorer{
  23. options: options,
  24. }
  25. }
  26. func (s *ConjunctionQueryScorer) Score(ctx *search.SearchContext, constituents []*search.DocumentMatch) *search.DocumentMatch {
  27. var sum float64
  28. var childrenExplanations []*search.Explanation
  29. if s.options.Explain {
  30. childrenExplanations = make([]*search.Explanation, len(constituents))
  31. }
  32. locations := []search.FieldTermLocationMap{}
  33. for i, docMatch := range constituents {
  34. sum += docMatch.Score
  35. if s.options.Explain {
  36. childrenExplanations[i] = docMatch.Expl
  37. }
  38. if docMatch.Locations != nil {
  39. locations = append(locations, docMatch.Locations)
  40. }
  41. }
  42. newScore := sum
  43. var newExpl *search.Explanation
  44. if s.options.Explain {
  45. newExpl = &search.Explanation{Value: sum, Message: "sum of:", Children: childrenExplanations}
  46. }
  47. // reuse constituents[0] as the return value
  48. rv := constituents[0]
  49. rv.Score = newScore
  50. rv.Expl = newExpl
  51. if len(locations) == 1 {
  52. rv.Locations = locations[0]
  53. } else if len(locations) > 1 {
  54. rv.Locations = search.MergeLocations(locations)
  55. }
  56. return rv
  57. }