search_queries_fsq_test.go 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // Copyright 2012-2015 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. import (
  6. "encoding/json"
  7. "testing"
  8. )
  9. func TestFunctionScoreQuery(t *testing.T) {
  10. q := NewFunctionScoreQuery().
  11. Query(NewTermQuery("name.last", "banon")).
  12. Add(NewTermFilter("name.last", "banon"), NewFactorFunction().BoostFactor(3)).
  13. AddScoreFunc(NewFactorFunction().BoostFactor(3)).
  14. AddScoreFunc(NewFactorFunction().BoostFactor(3)).
  15. Boost(3).
  16. MaxBoost(10).
  17. ScoreMode("avg")
  18. data, err := json.Marshal(q.Source())
  19. if err != nil {
  20. t.Fatalf("marshaling to JSON failed: %v", err)
  21. }
  22. got := string(data)
  23. expected := `{"function_score":{"boost":3,"functions":[{"boost_factor":3,"filter":{"term":{"name.last":"banon"}}},{"boost_factor":3},{"boost_factor":3}],"max_boost":10,"query":{"term":{"name.last":"banon"}},"score_mode":"avg"}}`
  24. if got != expected {
  25. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  26. }
  27. }
  28. func TestFunctionScoreQueryWithNilFilter(t *testing.T) {
  29. q := NewFunctionScoreQuery().
  30. Query(NewTermQuery("tag", "wow")).
  31. AddScoreFunc(NewRandomFunction()).
  32. Boost(2.0).
  33. MaxBoost(12.0).
  34. BoostMode("multiply").
  35. ScoreMode("max")
  36. data, err := json.Marshal(q.Source())
  37. if err != nil {
  38. t.Fatalf("marshaling to JSON failed: %v", err)
  39. }
  40. got := string(data)
  41. expected := `{"function_score":{"boost":2,"boost_mode":"multiply","max_boost":12,"query":{"term":{"tag":"wow"}},"random_score":{},"score_mode":"max"}}`
  42. if got != expected {
  43. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  44. }
  45. }
  46. func TestFieldValueFactor(t *testing.T) {
  47. q := NewFunctionScoreQuery().
  48. Query(NewTermQuery("name.last", "banon")).
  49. AddScoreFunc(NewFieldValueFactorFunction().Modifier("sqrt").Factor(2).Field("income")).
  50. Boost(2.0).
  51. MaxBoost(12.0).
  52. BoostMode("multiply").
  53. ScoreMode("max")
  54. data, err := json.Marshal(q.Source())
  55. if err != nil {
  56. t.Fatalf("marshaling to JSON failed: %v", err)
  57. }
  58. got := string(data)
  59. expected := `{"function_score":{"boost":2,"boost_mode":"multiply","field_value_factor":{"factor":2,"field":"income","modifier":"sqrt"},"max_boost":12,"query":{"term":{"name.last":"banon"}},"score_mode":"max"}}`
  60. if got != expected {
  61. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  62. }
  63. }
  64. func TestFunctionScoreQueryWithGaussScoreFunc(t *testing.T) {
  65. q := NewFunctionScoreQuery().
  66. Query(NewTermQuery("name.last", "banon")).
  67. AddScoreFunc(NewGaussDecayFunction().FieldName("pin.location").Origin("11, 12").Scale("2km").Offset("0km").Decay(0.33))
  68. data, err := json.Marshal(q.Source())
  69. if err != nil {
  70. t.Fatalf("marshaling to JSON failed: %v", err)
  71. }
  72. got := string(data)
  73. expected := `{"function_score":{"gauss":{"pin.location":{"decay":0.33,"offset":"0km","origin":"11, 12","scale":"2km"}},"query":{"term":{"name.last":"banon"}}}}`
  74. if got != expected {
  75. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  76. }
  77. }