indices_put_alias_test.go 6.0 KB

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