rescorer.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. // Copyright 2012-present Oliver Eilhard. All rights reserved.
  2. // Use of this source code is governed by a MIT-license.
  3. // See http://olivere.mit-license.org/license.txt for details.
  4. package elastic
  5. type Rescorer interface {
  6. Name() string
  7. Source() (interface{}, error)
  8. }
  9. // -- Query Rescorer --
  10. type QueryRescorer struct {
  11. query Query
  12. rescoreQueryWeight *float64
  13. queryWeight *float64
  14. scoreMode string
  15. }
  16. func NewQueryRescorer(query Query) *QueryRescorer {
  17. return &QueryRescorer{
  18. query: query,
  19. }
  20. }
  21. func (r *QueryRescorer) Name() string {
  22. return "query"
  23. }
  24. func (r *QueryRescorer) RescoreQueryWeight(rescoreQueryWeight float64) *QueryRescorer {
  25. r.rescoreQueryWeight = &rescoreQueryWeight
  26. return r
  27. }
  28. func (r *QueryRescorer) QueryWeight(queryWeight float64) *QueryRescorer {
  29. r.queryWeight = &queryWeight
  30. return r
  31. }
  32. func (r *QueryRescorer) ScoreMode(scoreMode string) *QueryRescorer {
  33. r.scoreMode = scoreMode
  34. return r
  35. }
  36. func (r *QueryRescorer) Source() (interface{}, error) {
  37. rescoreQuery, err := r.query.Source()
  38. if err != nil {
  39. return nil, err
  40. }
  41. source := make(map[string]interface{})
  42. source["rescore_query"] = rescoreQuery
  43. if r.queryWeight != nil {
  44. source["query_weight"] = *r.queryWeight
  45. }
  46. if r.rescoreQueryWeight != nil {
  47. source["rescore_query_weight"] = *r.rescoreQueryWeight
  48. }
  49. if r.scoreMode != "" {
  50. source["score_mode"] = r.scoreMode
  51. }
  52. return source, nil
  53. }