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_constant.go 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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/index"
  18. "github.com/blevesearch/bleve/search"
  19. )
  20. type ConstantScorer struct {
  21. constant float64
  22. boost float64
  23. options search.SearcherOptions
  24. queryNorm float64
  25. queryWeight float64
  26. queryWeightExplanation *search.Explanation
  27. }
  28. func NewConstantScorer(constant float64, boost float64, options search.SearcherOptions) *ConstantScorer {
  29. rv := ConstantScorer{
  30. options: options,
  31. queryWeight: 1.0,
  32. constant: constant,
  33. boost: boost,
  34. }
  35. return &rv
  36. }
  37. func (s *ConstantScorer) Weight() float64 {
  38. sum := s.boost
  39. return sum * sum
  40. }
  41. func (s *ConstantScorer) SetQueryNorm(qnorm float64) {
  42. s.queryNorm = qnorm
  43. // update the query weight
  44. s.queryWeight = s.boost * s.queryNorm
  45. if s.options.Explain {
  46. childrenExplanations := make([]*search.Explanation, 2)
  47. childrenExplanations[0] = &search.Explanation{
  48. Value: s.boost,
  49. Message: "boost",
  50. }
  51. childrenExplanations[1] = &search.Explanation{
  52. Value: s.queryNorm,
  53. Message: "queryNorm",
  54. }
  55. s.queryWeightExplanation = &search.Explanation{
  56. Value: s.queryWeight,
  57. Message: fmt.Sprintf("ConstantScore()^%f, product of:", s.boost),
  58. Children: childrenExplanations,
  59. }
  60. }
  61. }
  62. func (s *ConstantScorer) Score(ctx *search.SearchContext, id index.IndexInternalID) *search.DocumentMatch {
  63. var scoreExplanation *search.Explanation
  64. score := s.constant
  65. if s.options.Explain {
  66. scoreExplanation = &search.Explanation{
  67. Value: score,
  68. Message: fmt.Sprintf("ConstantScore()"),
  69. }
  70. }
  71. // if the query weight isn't 1, multiply
  72. if s.queryWeight != 1.0 {
  73. score = score * s.queryWeight
  74. if s.options.Explain {
  75. childExplanations := make([]*search.Explanation, 2)
  76. childExplanations[0] = s.queryWeightExplanation
  77. childExplanations[1] = scoreExplanation
  78. scoreExplanation = &search.Explanation{
  79. Value: score,
  80. Message: fmt.Sprintf("weight(^%f), product of:", s.boost),
  81. Children: childExplanations,
  82. }
  83. }
  84. }
  85. rv := ctx.DocumentMatchPool.Get()
  86. rv.IndexInternalID = id
  87. rv.Score = score
  88. if s.options.Explain {
  89. rv.Expl = scoreExplanation
  90. }
  91. return rv
  92. }