suggester_phrase_test.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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 TestPhraseSuggesterSource(t *testing.T) {
  10. s := NewPhraseSuggester("name").
  11. Text("Xor the Got-Jewel").
  12. Analyzer("body").
  13. Field("bigram").
  14. Size(1).
  15. RealWordErrorLikelihood(0.95).
  16. MaxErrors(0.5).
  17. GramSize(2).
  18. Highlight("<em>", "</em>")
  19. src, err := s.Source(true)
  20. if err != nil {
  21. t.Fatal(err)
  22. }
  23. data, err := json.Marshal(src)
  24. if err != nil {
  25. t.Fatalf("marshaling to JSON failed: %v", err)
  26. }
  27. got := string(data)
  28. expected := `{"name":{"text":"Xor the Got-Jewel","phrase":{"analyzer":"body","field":"bigram","gram_size":2,"highlight":{"post_tag":"\u003c/em\u003e","pre_tag":"\u003cem\u003e"},"max_errors":0.5,"real_word_error_likelihood":0.95,"size":1}}}`
  29. if got != expected {
  30. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  31. }
  32. }
  33. func TestPhraseSuggesterSourceWithContextQuery(t *testing.T) {
  34. geomapQ := NewSuggesterGeoMapping("location").
  35. Precision("1km", "5m").
  36. Neighbors(true).
  37. FieldName("pin").
  38. DefaultLocations(GeoPointFromLatLon(0.0, 0.0))
  39. s := NewPhraseSuggester("name").
  40. Text("Xor the Got-Jewel").
  41. Analyzer("body").
  42. Field("bigram").
  43. Size(1).
  44. RealWordErrorLikelihood(0.95).
  45. MaxErrors(0.5).
  46. GramSize(2).
  47. Highlight("<em>", "</em>").
  48. ContextQuery(geomapQ)
  49. src, err := s.Source(true)
  50. if err != nil {
  51. t.Fatal(err)
  52. }
  53. data, err := json.Marshal(src)
  54. if err != nil {
  55. t.Fatalf("marshaling to JSON failed: %v", err)
  56. }
  57. got := string(data)
  58. expected := `{"name":{"text":"Xor the Got-Jewel","phrase":{"analyzer":"body","context":{"location":{"default":{"lat":0,"lon":0},"neighbors":true,"path":"pin","precision":["1km","5m"],"type":"geo"}},"field":"bigram","gram_size":2,"highlight":{"post_tag":"\u003c/em\u003e","pre_tag":"\u003cem\u003e"},"max_errors":0.5,"real_word_error_likelihood":0.95,"size":1}}}`
  59. if got != expected {
  60. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  61. }
  62. }
  63. func TestPhraseSuggesterComplexSource(t *testing.T) {
  64. g1 := NewDirectCandidateGenerator("body").
  65. SuggestMode("always").
  66. MinWordLength(1)
  67. g2 := NewDirectCandidateGenerator("reverse").
  68. SuggestMode("always").
  69. MinWordLength(1).
  70. PreFilter("reverse").
  71. PostFilter("reverse")
  72. s := NewPhraseSuggester("simple_phrase").
  73. Text("Xor the Got-Jewel").
  74. Analyzer("body").
  75. Field("bigram").
  76. Size(4).
  77. RealWordErrorLikelihood(0.95).
  78. Confidence(2.0).
  79. GramSize(2).
  80. CandidateGenerators(g1, g2).
  81. CollateQuery(`"match":{"{{field_name}}" : "{{suggestion}}"}`).
  82. CollateParams(map[string]interface{}{"field_name": "title"}).
  83. CollatePreference("_primary").
  84. CollatePrune(true)
  85. src, err := s.Source(true)
  86. if err != nil {
  87. t.Fatal(err)
  88. }
  89. data, err := json.Marshal(src)
  90. if err != nil {
  91. t.Fatalf("marshaling to JSON failed: %v", err)
  92. }
  93. got := string(data)
  94. expected := `{"simple_phrase":{"text":"Xor the Got-Jewel","phrase":{"analyzer":"body","collate":{"params":{"field_name":"title"},"preference":"_primary","prune":true,"query":"\"match\":{\"{{field_name}}\" : \"{{suggestion}}\"}"},"confidence":2,"direct_generator":[{"field":"body","min_word_length":1,"suggest_mode":"always"},{"field":"reverse","min_word_length":1,"post_filter":"reverse","pre_filter":"reverse","suggest_mode":"always"}],"field":"bigram","gram_size":2,"real_word_error_likelihood":0.95,"size":4}}}`
  95. if got != expected {
  96. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  97. }
  98. }
  99. func TestPhraseStupidBackoffSmoothingModel(t *testing.T) {
  100. s := NewStupidBackoffSmoothingModel(0.42)
  101. src, err := s.Source()
  102. if err != nil {
  103. t.Fatal(err)
  104. }
  105. data, err := json.Marshal(src)
  106. if err != nil {
  107. t.Fatalf("marshaling to JSON failed: %v", err)
  108. }
  109. got := string(data)
  110. // The source does NOT include the smoothing model type!
  111. expected := `{"discount":0.42}`
  112. if got != expected {
  113. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  114. }
  115. if s.Type() != "stupid_backoff" {
  116. t.Errorf("expected %q, got: %q", "stupid_backoff", s.Type())
  117. }
  118. }
  119. func TestPhraseLaplaceSmoothingModel(t *testing.T) {
  120. s := NewLaplaceSmoothingModel(0.63)
  121. src, err := s.Source()
  122. if err != nil {
  123. t.Fatal(err)
  124. }
  125. data, err := json.Marshal(src)
  126. if err != nil {
  127. t.Fatalf("marshaling to JSON failed: %v", err)
  128. }
  129. got := string(data)
  130. // The source does NOT include the smoothing model type!
  131. expected := `{"alpha":0.63}`
  132. if got != expected {
  133. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  134. }
  135. if s.Type() != "laplace" {
  136. t.Errorf("expected %q, got: %q", "laplace", s.Type())
  137. }
  138. }
  139. func TestLinearInterpolationSmoothingModel(t *testing.T) {
  140. s := NewLinearInterpolationSmoothingModel(0.3, 0.2, 0.05)
  141. src, err := s.Source()
  142. if err != nil {
  143. t.Fatal(err)
  144. }
  145. data, err := json.Marshal(src)
  146. if err != nil {
  147. t.Fatalf("marshaling to JSON failed: %v", err)
  148. }
  149. got := string(data)
  150. // The source does NOT include the smoothing model type!
  151. expected := `{"bigram_lambda":0.2,"trigram_lambda":0.3,"unigram_lambda":0.05}`
  152. if got != expected {
  153. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  154. }
  155. if s.Type() != "linear_interpolation" {
  156. t.Errorf("expected %q, got: %q", "linear_interpolation", s.Type())
  157. }
  158. }