indices_put_mapping_test.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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 TestPutMappingURL(t *testing.T) {
  10. client := setupTestClientAndCreateIndex(t)
  11. tests := []struct {
  12. Indices []string
  13. Type string
  14. Expected string
  15. }{
  16. {
  17. []string{},
  18. "tweet",
  19. "/_mapping/tweet",
  20. },
  21. {
  22. []string{"*"},
  23. "tweet",
  24. "/%2A/_mapping/tweet",
  25. },
  26. {
  27. []string{"store-1", "store-2"},
  28. "tweet",
  29. "/store-1%2Cstore-2/_mapping/tweet",
  30. },
  31. }
  32. for _, test := range tests {
  33. path, _, err := client.PutMapping().Index(test.Indices...).Type(test.Type).buildURL()
  34. if err != nil {
  35. t.Fatal(err)
  36. }
  37. if path != test.Expected {
  38. t.Errorf("expected %q; got: %q", test.Expected, path)
  39. }
  40. }
  41. }
  42. func TestMappingLifecycle(t *testing.T) {
  43. client := setupTestClientAndCreateIndex(t)
  44. mapping := `{
  45. "tweetdoc":{
  46. "properties":{
  47. "field":{
  48. "type":"keyword"
  49. }
  50. }
  51. }
  52. }`
  53. putresp, err := client.PutMapping().Index(testIndexName2).Type("tweetdoc").BodyString(mapping).Do(context.TODO())
  54. if err != nil {
  55. t.Fatalf("expected put mapping to succeed; got: %v", err)
  56. }
  57. if putresp == nil {
  58. t.Fatalf("expected put mapping response; got: %v", putresp)
  59. }
  60. if !putresp.Acknowledged {
  61. t.Fatalf("expected put mapping ack; got: %v", putresp.Acknowledged)
  62. }
  63. getresp, err := client.GetMapping().Index(testIndexName2).Type("tweetdoc").Do(context.TODO())
  64. if err != nil {
  65. t.Fatalf("expected get mapping to succeed; got: %v", err)
  66. }
  67. if getresp == nil {
  68. t.Fatalf("expected get mapping response; got: %v", getresp)
  69. }
  70. props, ok := getresp[testIndexName2]
  71. if !ok {
  72. t.Fatalf("expected JSON root to be of type map[string]interface{}; got: %#v", props)
  73. }
  74. // NOTE There is no Delete Mapping API in Elasticsearch 2.0
  75. }