suggester_term_test.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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 TestTermSuggesterSource(t *testing.T) {
  10. s := NewTermSuggester("name").
  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 := `{"name":{"text":"n","term":{"field":"suggest"}}}`
  23. if got != expected {
  24. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  25. }
  26. }
  27. func TestTermSuggesterWithPrefixLengthSource(t *testing.T) {
  28. s := NewTermSuggester("name").
  29. Text("n").
  30. Field("suggest").
  31. PrefixLength(0)
  32. src, err := s.Source(true)
  33. if err != nil {
  34. t.Fatal(err)
  35. }
  36. data, err := json.Marshal(src)
  37. if err != nil {
  38. t.Fatalf("marshaling to JSON failed: %v", err)
  39. }
  40. got := string(data)
  41. expected := `{"name":{"text":"n","term":{"field":"suggest","prefix_length":0}}}`
  42. if got != expected {
  43. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  44. }
  45. }