search_queries_fsq_test.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. import (
  6. "encoding/json"
  7. "testing"
  8. )
  9. func TestFunctionScoreQuery(t *testing.T) {
  10. q := NewFunctionScoreQuery().
  11. Query(NewTermQuery("name.last", "banon")).
  12. Add(NewTermQuery("name.last", "banon"), NewWeightFactorFunction(1.5)).
  13. AddScoreFunc(NewWeightFactorFunction(3)).
  14. AddScoreFunc(NewRandomFunction()).
  15. Boost(3).
  16. MaxBoost(10).
  17. ScoreMode("avg")
  18. src, err := q.Source()
  19. if err != nil {
  20. t.Fatal(err)
  21. }
  22. data, err := json.Marshal(src)
  23. if err != nil {
  24. t.Fatalf("marshaling to JSON failed: %v", err)
  25. }
  26. got := string(data)
  27. expected := `{"function_score":{"boost":3,"functions":[{"filter":{"term":{"name.last":"banon"}},"weight":1.5},{"weight":3},{"random_score":{}}],"max_boost":10,"query":{"term":{"name.last":"banon"}},"score_mode":"avg"}}`
  28. if got != expected {
  29. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  30. }
  31. }
  32. func TestFunctionScoreQueryWithNilFilter(t *testing.T) {
  33. q := NewFunctionScoreQuery().
  34. Query(NewTermQuery("tag", "wow")).
  35. AddScoreFunc(NewRandomFunction()).
  36. Boost(2.0).
  37. MaxBoost(12.0).
  38. BoostMode("multiply").
  39. ScoreMode("max")
  40. src, err := q.Source()
  41. if err != nil {
  42. t.Fatal(err)
  43. }
  44. data, err := json.Marshal(src)
  45. if err != nil {
  46. t.Fatalf("marshaling to JSON failed: %v", err)
  47. }
  48. got := string(data)
  49. expected := `{"function_score":{"boost":2,"boost_mode":"multiply","functions":[{"random_score":{}}],"max_boost":12,"query":{"term":{"tag":"wow"}},"score_mode":"max"}}`
  50. if got != expected {
  51. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  52. }
  53. }
  54. func TestFieldValueFactor(t *testing.T) {
  55. q := NewFunctionScoreQuery().
  56. Query(NewTermQuery("name.last", "banon")).
  57. AddScoreFunc(NewFieldValueFactorFunction().Modifier("sqrt").Factor(2).Field("income")).
  58. Boost(2.0).
  59. MaxBoost(12.0).
  60. BoostMode("multiply").
  61. ScoreMode("max")
  62. src, err := q.Source()
  63. if err != nil {
  64. t.Fatal(err)
  65. }
  66. data, err := json.Marshal(src)
  67. if err != nil {
  68. t.Fatalf("marshaling to JSON failed: %v", err)
  69. }
  70. got := string(data)
  71. expected := `{"function_score":{"boost":2,"boost_mode":"multiply","functions":[{"field_value_factor":{"factor":2,"field":"income","modifier":"sqrt"}}],"max_boost":12,"query":{"term":{"name.last":"banon"}},"score_mode":"max"}}`
  72. if got != expected {
  73. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  74. }
  75. }
  76. func TestFieldValueFactorWithWeight(t *testing.T) {
  77. q := NewFunctionScoreQuery().
  78. Query(NewTermQuery("name.last", "banon")).
  79. AddScoreFunc(NewFieldValueFactorFunction().Modifier("sqrt").Factor(2).Field("income").Weight(2.5)).
  80. Boost(2.0).
  81. MaxBoost(12.0).
  82. BoostMode("multiply").
  83. ScoreMode("max")
  84. src, err := q.Source()
  85. if err != nil {
  86. t.Fatal(err)
  87. }
  88. data, err := json.Marshal(src)
  89. if err != nil {
  90. t.Fatalf("marshaling to JSON failed: %v", err)
  91. }
  92. got := string(data)
  93. expected := `{"function_score":{"boost":2,"boost_mode":"multiply","functions":[{"field_value_factor":{"factor":2,"field":"income","modifier":"sqrt"},"weight":2.5}],"max_boost":12,"query":{"term":{"name.last":"banon"}},"score_mode":"max"}}`
  94. if got != expected {
  95. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  96. }
  97. }
  98. func TestFieldValueFactorWithMultipleScoreFuncsAndWeights(t *testing.T) {
  99. q := NewFunctionScoreQuery().
  100. Query(NewTermQuery("name.last", "banon")).
  101. AddScoreFunc(NewFieldValueFactorFunction().Modifier("sqrt").Factor(2).Field("income").Weight(2.5)).
  102. AddScoreFunc(NewScriptFunction(NewScript("_score * doc['my_numeric_field'].value")).Weight(1.25)).
  103. AddScoreFunc(NewWeightFactorFunction(0.5)).
  104. Boost(2.0).
  105. MaxBoost(12.0).
  106. BoostMode("multiply").
  107. ScoreMode("max")
  108. src, err := q.Source()
  109. if err != nil {
  110. t.Fatal(err)
  111. }
  112. data, err := json.Marshal(src)
  113. if err != nil {
  114. t.Fatalf("marshaling to JSON failed: %v", err)
  115. }
  116. got := string(data)
  117. expected := `{"function_score":{"boost":2,"boost_mode":"multiply","functions":[{"field_value_factor":{"factor":2,"field":"income","modifier":"sqrt"},"weight":2.5},{"script_score":{"script":{"inline":"_score * doc['my_numeric_field'].value"}},"weight":1.25},{"weight":0.5}],"max_boost":12,"query":{"term":{"name.last":"banon"}},"score_mode":"max"}}`
  118. if got != expected {
  119. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  120. }
  121. }
  122. func TestFunctionScoreQueryWithGaussScoreFunc(t *testing.T) {
  123. q := NewFunctionScoreQuery().
  124. Query(NewTermQuery("name.last", "banon")).
  125. AddScoreFunc(NewGaussDecayFunction().FieldName("pin.location").Origin("11, 12").Scale("2km").Offset("0km").Decay(0.33))
  126. src, err := q.Source()
  127. if err != nil {
  128. t.Fatal(err)
  129. }
  130. data, err := json.Marshal(src)
  131. if err != nil {
  132. t.Fatalf("marshaling to JSON failed: %v", err)
  133. }
  134. got := string(data)
  135. expected := `{"function_score":{"functions":[{"gauss":{"pin.location":{"decay":0.33,"offset":"0km","origin":"11, 12","scale":"2km"}}}],"query":{"term":{"name.last":"banon"}}}}`
  136. if got != expected {
  137. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  138. }
  139. }
  140. func TestFunctionScoreQueryWithGaussScoreFuncAndMultiValueMode(t *testing.T) {
  141. q := NewFunctionScoreQuery().
  142. Query(NewTermQuery("name.last", "banon")).
  143. AddScoreFunc(NewGaussDecayFunction().FieldName("pin.location").Origin("11, 12").Scale("2km").Offset("0km").Decay(0.33).MultiValueMode("avg"))
  144. src, err := q.Source()
  145. if err != nil {
  146. t.Fatal(err)
  147. }
  148. data, err := json.Marshal(src)
  149. if err != nil {
  150. t.Fatalf("marshaling to JSON failed: %v", err)
  151. }
  152. got := string(data)
  153. expected := `{"function_score":{"functions":[{"gauss":{"multi_value_mode":"avg","pin.location":{"decay":0.33,"offset":"0km","origin":"11, 12","scale":"2km"}}}],"query":{"term":{"name.last":"banon"}}}}`
  154. if got != expected {
  155. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  156. }
  157. }
  158. func TestFunctionScoreQueryBug660(t *testing.T) {
  159. q := NewFunctionScoreQuery().
  160. Query(NewBoolQuery().Filter(NewTermsQuery("prod", 8199))).
  161. AddScoreFunc(
  162. NewGaussDecayFunction().
  163. FieldName("update").
  164. Origin(1501747403).
  165. Scale(1209600).
  166. Offset("259200").
  167. Decay(0.6).
  168. Weight(1),
  169. ).
  170. BoostMode("sum").
  171. ScoreMode("sum")
  172. src, err := q.Source()
  173. if err != nil {
  174. t.Fatal(err)
  175. }
  176. data, err := json.Marshal(src)
  177. if err != nil {
  178. t.Fatalf("marshaling to JSON failed: %v", err)
  179. }
  180. got := string(data)
  181. expected := `{"function_score":{"boost_mode":"sum","functions":[{"gauss":{"update":{"decay":0.6,"offset":"259200","origin":1501747403,"scale":1209600}},"weight":1}],"query":{"bool":{"filter":{"terms":{"prod":[8199]}}}},"score_mode":"sum"}}`
  182. if got != expected {
  183. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  184. }
  185. }