indices_put_alias_test.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. "encoding/json"
  8. "testing"
  9. )
  10. const (
  11. testAliasName = "elastic-test-alias"
  12. )
  13. func TestAliasLifecycle(t *testing.T) {
  14. var err error
  15. client := setupTestClientAndCreateIndex(t)
  16. // Some tweets
  17. tweet1 := tweet{User: "olivere", Message: "Welcome to Golang and Elasticsearch."}
  18. tweet2 := tweet{User: "sandrae", Message: "Cycling is fun."}
  19. tweet3 := tweet{User: "olivere", Message: "Another unrelated topic."}
  20. // Add tweets to first index
  21. _, err = client.Index().Index(testIndexName).Type("tweet").Id("1").BodyJson(&tweet1).Do(context.TODO())
  22. if err != nil {
  23. t.Fatal(err)
  24. }
  25. _, err = client.Index().Index(testIndexName).Type("tweet").Id("2").BodyJson(&tweet2).Do(context.TODO())
  26. if err != nil {
  27. t.Fatal(err)
  28. }
  29. // Add tweets to second index
  30. _, err = client.Index().Index(testIndexName2).Type("tweet").Id("3").BodyJson(&tweet3).Do(context.TODO())
  31. if err != nil {
  32. t.Fatal(err)
  33. }
  34. // Flush
  35. _, err = client.Flush().Index(testIndexName).Do(context.TODO())
  36. if err != nil {
  37. t.Fatal(err)
  38. }
  39. _, err = client.Flush().Index(testIndexName2).Do(context.TODO())
  40. if err != nil {
  41. t.Fatal(err)
  42. }
  43. // Add both indices to a new alias
  44. aliasCreate, err := client.Alias().
  45. Add(testIndexName, testAliasName).
  46. Action(NewAliasAddAction(testAliasName).Index(testIndexName2)).
  47. //Pretty(true).
  48. Do(context.TODO())
  49. if err != nil {
  50. t.Fatal(err)
  51. }
  52. if !aliasCreate.Acknowledged {
  53. t.Errorf("expected AliasResult.Acknowledged %v; got %v", true, aliasCreate.Acknowledged)
  54. }
  55. // Search should return all 3 tweets
  56. matchAll := NewMatchAllQuery()
  57. searchResult1, err := client.Search().Index(testAliasName).Query(matchAll).Do(context.TODO())
  58. if err != nil {
  59. t.Fatal(err)
  60. }
  61. if searchResult1.Hits == nil {
  62. t.Errorf("expected SearchResult.Hits != nil; got nil")
  63. }
  64. if searchResult1.Hits.TotalHits != 3 {
  65. t.Errorf("expected SearchResult.Hits.TotalHits = %d; got %d", 3, searchResult1.Hits.TotalHits)
  66. }
  67. // Remove first index should remove two tweets, so should only yield 1
  68. aliasRemove1, err := client.Alias().
  69. Remove(testIndexName, testAliasName).
  70. //Pretty(true).
  71. Do(context.TODO())
  72. if err != nil {
  73. t.Fatal(err)
  74. }
  75. if !aliasRemove1.Acknowledged {
  76. t.Errorf("expected AliasResult.Acknowledged %v; got %v", true, aliasRemove1.Acknowledged)
  77. }
  78. searchResult2, err := client.Search().Index(testAliasName).Query(matchAll).Do(context.TODO())
  79. if err != nil {
  80. t.Fatal(err)
  81. }
  82. if searchResult2.Hits == nil {
  83. t.Errorf("expected SearchResult.Hits != nil; got nil")
  84. }
  85. if searchResult2.Hits.TotalHits != 1 {
  86. t.Errorf("expected SearchResult.Hits.TotalHits = %d; got %d", 1, searchResult2.Hits.TotalHits)
  87. }
  88. }
  89. func TestAliasAddAction(t *testing.T) {
  90. var tests = []struct {
  91. Action *AliasAddAction
  92. Expected string
  93. Invalid bool
  94. }{
  95. {
  96. Action: NewAliasAddAction("").Index(""),
  97. Invalid: true,
  98. },
  99. {
  100. Action: NewAliasAddAction("alias1").Index(""),
  101. Invalid: true,
  102. },
  103. {
  104. Action: NewAliasAddAction("").Index("index1"),
  105. Invalid: true,
  106. },
  107. {
  108. Action: NewAliasAddAction("alias1").Index("index1"),
  109. Expected: `{"add":{"alias":"alias1","index":"index1"}}`,
  110. },
  111. {
  112. Action: NewAliasAddAction("alias1").Index("index1", "index2"),
  113. Expected: `{"add":{"alias":"alias1","indices":["index1","index2"]}}`,
  114. },
  115. {
  116. Action: NewAliasAddAction("alias1").Index("index1").Routing("routing1"),
  117. Expected: `{"add":{"alias":"alias1","index":"index1","routing":"routing1"}}`,
  118. },
  119. {
  120. Action: NewAliasAddAction("alias1").Index("index1").Routing("routing1").IndexRouting("indexRouting1"),
  121. Expected: `{"add":{"alias":"alias1","index":"index1","index_routing":"indexRouting1","routing":"routing1"}}`,
  122. },
  123. {
  124. Action: NewAliasAddAction("alias1").Index("index1").Routing("routing1").SearchRouting("searchRouting1"),
  125. Expected: `{"add":{"alias":"alias1","index":"index1","routing":"routing1","search_routing":"searchRouting1"}}`,
  126. },
  127. {
  128. Action: NewAliasAddAction("alias1").Index("index1").Routing("routing1").SearchRouting("searchRouting1", "searchRouting2"),
  129. Expected: `{"add":{"alias":"alias1","index":"index1","routing":"routing1","search_routing":"searchRouting1,searchRouting2"}}`,
  130. },
  131. {
  132. Action: NewAliasAddAction("alias1").Index("index1").Filter(NewTermQuery("user", "olivere")),
  133. Expected: `{"add":{"alias":"alias1","filter":{"term":{"user":"olivere"}},"index":"index1"}}`,
  134. },
  135. }
  136. for i, tt := range tests {
  137. src, err := tt.Action.Source()
  138. if err != nil {
  139. if !tt.Invalid {
  140. t.Errorf("#%d: expected to succeed", i)
  141. }
  142. } else {
  143. if tt.Invalid {
  144. t.Errorf("#%d: expected to fail", i)
  145. } else {
  146. dst, err := json.Marshal(src)
  147. if err != nil {
  148. t.Fatal(err)
  149. }
  150. if want, have := tt.Expected, string(dst); want != have {
  151. t.Errorf("#%d: expected %s, got %s", i, want, have)
  152. }
  153. }
  154. }
  155. }
  156. }
  157. func TestAliasRemoveAction(t *testing.T) {
  158. var tests = []struct {
  159. Action *AliasRemoveAction
  160. Expected string
  161. Invalid bool
  162. }{
  163. {
  164. Action: NewAliasRemoveAction(""),
  165. Invalid: true,
  166. },
  167. {
  168. Action: NewAliasRemoveAction("alias1"),
  169. Invalid: true,
  170. },
  171. {
  172. Action: NewAliasRemoveAction("").Index("index1"),
  173. Invalid: true,
  174. },
  175. {
  176. Action: NewAliasRemoveAction("alias1").Index("index1"),
  177. Expected: `{"remove":{"alias":"alias1","index":"index1"}}`,
  178. },
  179. {
  180. Action: NewAliasRemoveAction("alias1").Index("index1", "index2"),
  181. Expected: `{"remove":{"alias":"alias1","indices":["index1","index2"]}}`,
  182. },
  183. }
  184. for i, tt := range tests {
  185. src, err := tt.Action.Source()
  186. if err != nil {
  187. if !tt.Invalid {
  188. t.Errorf("#%d: expected to succeed", i)
  189. }
  190. } else {
  191. if tt.Invalid {
  192. t.Errorf("#%d: expected to fail", i)
  193. } else {
  194. dst, err := json.Marshal(src)
  195. if err != nil {
  196. t.Fatal(err)
  197. }
  198. if want, have := tt.Expected, string(dst); want != have {
  199. t.Errorf("#%d: expected %s, got %s", i, want, have)
  200. }
  201. }
  202. }
  203. }
  204. }