get_template_test.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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. "context"
  7. "testing"
  8. )
  9. func TestGetPutDeleteTemplate(t *testing.T) {
  10. client := setupTestClientAndCreateIndex(t)
  11. // This is a search template, not an index template!
  12. tmpl := `{
  13. "template": {
  14. "query" : { "term" : { "{{my_field}}" : "{{my_value}}" } },
  15. "size" : "{{my_size}}"
  16. },
  17. "params":{
  18. "my_field" : "user",
  19. "my_value" : "olivere",
  20. "my_size" : 5
  21. }
  22. }`
  23. putres, err := client.PutTemplate().Id("elastic-template").BodyString(tmpl).Do(context.TODO())
  24. if err != nil {
  25. t.Fatalf("expected no error; got: %v", err)
  26. }
  27. if putres == nil {
  28. t.Fatalf("expected response; got: %v", putres)
  29. }
  30. if !putres.Acknowledged {
  31. t.Fatalf("expected template creation to be acknowledged; got: %v", putres.Acknowledged)
  32. }
  33. // Always delete template
  34. defer client.DeleteTemplate().Id("elastic-template").Do(context.TODO())
  35. // Get template
  36. getres, err := client.GetTemplate().Id("elastic-template").Do(context.TODO())
  37. if err != nil {
  38. t.Fatalf("expected no error; got: %v", err)
  39. }
  40. if getres == nil {
  41. t.Fatalf("expected response; got: %v", getres)
  42. }
  43. if getres.Template == "" {
  44. t.Errorf("expected template %q; got: %q", tmpl, getres.Template)
  45. }
  46. }