indices_exists_template_test.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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 TestIndexExistsTemplate(t *testing.T) {
  10. client := setupTestClientAndCreateIndex(t)
  11. tmpl := `{
  12. "template":"elastic-test*",
  13. "settings":{
  14. "number_of_shards":1,
  15. "number_of_replicas":0
  16. },
  17. "mappings":{
  18. "tweet":{
  19. "properties":{
  20. "tags":{
  21. "type":"keyword"
  22. },
  23. "location":{
  24. "type":"geo_point"
  25. },
  26. "suggest_field":{
  27. "type":"completion"
  28. }
  29. }
  30. }
  31. }
  32. }`
  33. putres, err := client.IndexPutTemplate("elastic-template").BodyString(tmpl).Do(context.TODO())
  34. if err != nil {
  35. t.Fatalf("expected no error; got: %v", err)
  36. }
  37. if putres == nil {
  38. t.Fatalf("expected response; got: %v", putres)
  39. }
  40. if !putres.Acknowledged {
  41. t.Fatalf("expected index template to be ack'd; got: %v", putres.Acknowledged)
  42. }
  43. // Always delete template
  44. defer client.IndexDeleteTemplate("elastic-template").Do(context.TODO())
  45. // Check if template exists
  46. exists, err := client.IndexTemplateExists("elastic-template").Do(context.TODO())
  47. if err != nil {
  48. t.Fatalf("expected no error; got: %v", err)
  49. }
  50. if !exists {
  51. t.Fatalf("expected index template %q to exist; got: %v", "elastic-template", exists)
  52. }
  53. // Get template
  54. getres, err := client.IndexGetTemplate("elastic-template").Do(context.TODO())
  55. if err != nil {
  56. t.Fatalf("expected no error; got: %v", err)
  57. }
  58. if getres == nil {
  59. t.Fatalf("expected to get index template %q; got: %v", "elastic-template", getres)
  60. }
  61. }