indices_get_aliases_test.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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 TestAliasesBuildURL(t *testing.T) {
  10. client := setupTestClient(t)
  11. tests := []struct {
  12. Indices []string
  13. Expected string
  14. }{
  15. {
  16. []string{},
  17. "/_aliases",
  18. },
  19. {
  20. []string{"index1"},
  21. "/index1/_aliases",
  22. },
  23. {
  24. []string{"index1", "index2"},
  25. "/index1%2Cindex2/_aliases",
  26. },
  27. }
  28. for i, test := range tests {
  29. path, _, err := client.Aliases().Index(test.Indices...).buildURL()
  30. if err != nil {
  31. t.Errorf("case #%d: %v", i+1, err)
  32. continue
  33. }
  34. if path != test.Expected {
  35. t.Errorf("case #%d: expected %q; got: %q", i+1, test.Expected, path)
  36. }
  37. }
  38. }
  39. func TestAliases(t *testing.T) {
  40. var err error
  41. client := setupTestClientAndCreateIndex(t)
  42. // Some tweets
  43. tweet1 := tweet{User: "olivere", Message: "Welcome to Golang and Elasticsearch."}
  44. tweet2 := tweet{User: "sandrae", Message: "Cycling is fun."}
  45. tweet3 := tweet{User: "olivere", Message: "Another unrelated topic."}
  46. // Add tweets to first index
  47. _, err = client.Index().Index(testIndexName).Type("tweet").Id("1").BodyJson(&tweet1).Do(context.TODO())
  48. if err != nil {
  49. t.Fatal(err)
  50. }
  51. _, err = client.Index().Index(testIndexName).Type("tweet").Id("2").BodyJson(&tweet2).Do(context.TODO())
  52. if err != nil {
  53. t.Fatal(err)
  54. }
  55. // Add tweets to second index
  56. _, err = client.Index().Index(testIndexName2).Type("tweet").Id("3").BodyJson(&tweet3).Do(context.TODO())
  57. if err != nil {
  58. t.Fatal(err)
  59. }
  60. // Flush
  61. _, err = client.Flush().Index(testIndexName).Do(context.TODO())
  62. if err != nil {
  63. t.Fatal(err)
  64. }
  65. _, err = client.Flush().Index(testIndexName2).Do(context.TODO())
  66. if err != nil {
  67. t.Fatal(err)
  68. }
  69. // Alias should not yet exist
  70. aliasesResult1, err := client.Aliases().
  71. Index(testIndexName, testIndexName2).
  72. //Pretty(true).
  73. Do(context.TODO())
  74. if err != nil {
  75. t.Fatal(err)
  76. }
  77. if len(aliasesResult1.Indices) != 2 {
  78. t.Errorf("expected len(AliasesResult.Indices) = %d; got %d", 2, len(aliasesResult1.Indices))
  79. }
  80. for indexName, indexDetails := range aliasesResult1.Indices {
  81. if len(indexDetails.Aliases) != 0 {
  82. t.Errorf("expected len(AliasesResult.Indices[%s].Aliases) = %d; got %d", indexName, 0, len(indexDetails.Aliases))
  83. }
  84. }
  85. // Add both indices to a new alias
  86. aliasCreate, err := client.Alias().
  87. Add(testIndexName, testAliasName).
  88. Add(testIndexName2, testAliasName).
  89. //Pretty(true).
  90. Do(context.TODO())
  91. if err != nil {
  92. t.Fatal(err)
  93. }
  94. if !aliasCreate.Acknowledged {
  95. t.Errorf("expected AliasResult.Acknowledged %v; got %v", true, aliasCreate.Acknowledged)
  96. }
  97. // Alias should now exist
  98. aliasesResult2, err := client.Aliases().
  99. Index(testIndexName, testIndexName2).
  100. //Pretty(true).
  101. Do(context.TODO())
  102. if err != nil {
  103. t.Fatal(err)
  104. }
  105. if len(aliasesResult2.Indices) != 2 {
  106. t.Errorf("expected len(AliasesResult.Indices) = %d; got %d", 2, len(aliasesResult2.Indices))
  107. }
  108. for indexName, indexDetails := range aliasesResult2.Indices {
  109. if len(indexDetails.Aliases) != 1 {
  110. t.Errorf("expected len(AliasesResult.Indices[%s].Aliases) = %d; got %d", indexName, 1, len(indexDetails.Aliases))
  111. }
  112. }
  113. // Check the reverse function:
  114. indexInfo1, found := aliasesResult2.Indices[testIndexName]
  115. if !found {
  116. t.Errorf("expected info about index %s = %v; got %v", testIndexName, true, found)
  117. }
  118. aliasFound := indexInfo1.HasAlias(testAliasName)
  119. if !aliasFound {
  120. t.Errorf("expected alias %s to include index %s; got %v", testAliasName, testIndexName, aliasFound)
  121. }
  122. // Check the reverse function:
  123. indexInfo2, found := aliasesResult2.Indices[testIndexName2]
  124. if !found {
  125. t.Errorf("expected info about index %s = %v; got %v", testIndexName, true, found)
  126. }
  127. aliasFound = indexInfo2.HasAlias(testAliasName)
  128. if !aliasFound {
  129. t.Errorf("expected alias %s to include index %s; got %v", testAliasName, testIndexName2, aliasFound)
  130. }
  131. // Remove first index should remove two tweets, so should only yield 1
  132. aliasRemove1, err := client.Alias().
  133. Remove(testIndexName, testAliasName).
  134. //Pretty(true).
  135. Do(context.TODO())
  136. if err != nil {
  137. t.Fatal(err)
  138. }
  139. if !aliasRemove1.Acknowledged {
  140. t.Errorf("expected AliasResult.Acknowledged %v; got %v", true, aliasRemove1.Acknowledged)
  141. }
  142. // Alias should now exist only for index 2
  143. aliasesResult3, err := client.Aliases().Index(testIndexName, testIndexName2).Do(context.TODO())
  144. if err != nil {
  145. t.Fatal(err)
  146. }
  147. if len(aliasesResult3.Indices) != 2 {
  148. t.Errorf("expected len(AliasesResult.Indices) = %d; got %d", 2, len(aliasesResult3.Indices))
  149. }
  150. for indexName, indexDetails := range aliasesResult3.Indices {
  151. if indexName == testIndexName {
  152. if len(indexDetails.Aliases) != 0 {
  153. t.Errorf("expected len(AliasesResult.Indices[%s].Aliases) = %d; got %d", indexName, 0, len(indexDetails.Aliases))
  154. }
  155. } else if indexName == testIndexName2 {
  156. if len(indexDetails.Aliases) != 1 {
  157. t.Errorf("expected len(AliasesResult.Indices[%s].Aliases) = %d; got %d", indexName, 1, len(indexDetails.Aliases))
  158. }
  159. } else {
  160. t.Errorf("got index %s", indexName)
  161. }
  162. }
  163. }