suggester_completion_test.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 TestCompletionSuggesterSource(t *testing.T) {
  10. s := NewCompletionSuggester("song-suggest").
  11. Text("n").
  12. Field("suggest")
  13. src, err := s.Source(true)
  14. if err != nil {
  15. t.Fatal(err)
  16. }
  17. data, err := json.Marshal(src)
  18. if err != nil {
  19. t.Fatalf("marshaling to JSON failed: %v", err)
  20. }
  21. got := string(data)
  22. expected := `{"song-suggest":{"text":"n","completion":{"field":"suggest"}}}`
  23. if got != expected {
  24. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  25. }
  26. }
  27. func TestCompletionSuggesterPrefixSource(t *testing.T) {
  28. s := NewCompletionSuggester("song-suggest").
  29. Prefix("nir").
  30. Field("suggest")
  31. src, err := s.Source(true)
  32. if err != nil {
  33. t.Fatal(err)
  34. }
  35. data, err := json.Marshal(src)
  36. if err != nil {
  37. t.Fatalf("marshaling to JSON failed: %v", err)
  38. }
  39. got := string(data)
  40. expected := `{"song-suggest":{"prefix":"nir","completion":{"field":"suggest"}}}`
  41. if got != expected {
  42. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  43. }
  44. }
  45. func TestCompletionSuggesterPrefixWithFuzzySource(t *testing.T) {
  46. s := NewCompletionSuggester("song-suggest").
  47. Prefix("nor").
  48. Field("suggest").
  49. FuzzyOptions(NewFuzzyCompletionSuggesterOptions().EditDistance(2))
  50. src, err := s.Source(true)
  51. if err != nil {
  52. t.Fatal(err)
  53. }
  54. data, err := json.Marshal(src)
  55. if err != nil {
  56. t.Fatalf("marshaling to JSON failed: %v", err)
  57. }
  58. got := string(data)
  59. expected := `{"song-suggest":{"prefix":"nor","completion":{"field":"suggest","fuzzy":{"fuzziness":2}}}}`
  60. if got != expected {
  61. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  62. }
  63. }
  64. func TestCompletionSuggesterRegexSource(t *testing.T) {
  65. s := NewCompletionSuggester("song-suggest").
  66. Regex("n[ever|i]r").
  67. Field("suggest")
  68. src, err := s.Source(true)
  69. if err != nil {
  70. t.Fatal(err)
  71. }
  72. data, err := json.Marshal(src)
  73. if err != nil {
  74. t.Fatalf("marshaling to JSON failed: %v", err)
  75. }
  76. got := string(data)
  77. expected := `{"song-suggest":{"regex":"n[ever|i]r","completion":{"field":"suggest"}}}`
  78. if got != expected {
  79. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  80. }
  81. }
  82. func TestCompletionSuggesterSourceWithMultipleContexts(t *testing.T) {
  83. s := NewCompletionSuggester("song-suggest").
  84. Text("n").
  85. Field("suggest").
  86. ContextQueries(
  87. NewSuggesterCategoryQuery("artist", "Sting"),
  88. NewSuggesterCategoryQuery("label", "BMG"),
  89. )
  90. src, err := s.Source(true)
  91. if err != nil {
  92. t.Fatal(err)
  93. }
  94. data, err := json.Marshal(src)
  95. if err != nil {
  96. t.Fatalf("marshaling to JSON failed: %v", err)
  97. }
  98. got := string(data)
  99. expected := `{"song-suggest":{"text":"n","completion":{"contexts":{"artist":[{"context":"Sting"}],"label":[{"context":"BMG"}]},"field":"suggest"}}}`
  100. if got != expected {
  101. t.Errorf("expected %s\n,got:\n%s", expected, got)
  102. }
  103. }