suggester_completion_fuzzy_test.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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 TestFuzzyCompletionSuggesterSource(t *testing.T) {
  10. s := NewFuzzyCompletionSuggester("song-suggest").
  11. Text("n").
  12. Field("suggest").
  13. Fuzziness(2)
  14. src, err := s.Source(true)
  15. if err != nil {
  16. t.Fatal(err)
  17. }
  18. data, err := json.Marshal(src)
  19. if err != nil {
  20. t.Fatalf("marshaling to JSON failed: %v", err)
  21. }
  22. got := string(data)
  23. expected := `{"song-suggest":{"text":"n","completion":{"field":"suggest","fuzzy":{"fuzziness":2}}}}`
  24. if got != expected {
  25. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  26. }
  27. }
  28. func TestFuzzyCompletionSuggesterWithStringFuzzinessSource(t *testing.T) {
  29. s := NewFuzzyCompletionSuggester("song-suggest").
  30. Text("n").
  31. Field("suggest").
  32. Fuzziness("1..4")
  33. src, err := s.Source(true)
  34. if err != nil {
  35. t.Fatal(err)
  36. }
  37. data, err := json.Marshal(src)
  38. if err != nil {
  39. t.Fatalf("marshaling to JSON failed: %v", err)
  40. }
  41. got := string(data)
  42. expected := `{"song-suggest":{"text":"n","completion":{"field":"suggest","fuzzy":{"fuzziness":"1..4"}}}}`
  43. if got != expected {
  44. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  45. }
  46. }