alias_test.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. // Copyright 2012-2015 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. "testing"
  7. )
  8. const (
  9. testAliasName = "elastic-test-alias"
  10. )
  11. func TestAliasLifecycle(t *testing.T) {
  12. var err error
  13. client := setupTestClientAndCreateIndex(t)
  14. // Some tweets
  15. tweet1 := tweet{User: "olivere", Message: "Welcome to Golang and Elasticsearch."}
  16. tweet2 := tweet{User: "sandrae", Message: "Cycling is fun."}
  17. tweet3 := tweet{User: "olivere", Message: "Another unrelated topic."}
  18. // Add tweets to first index
  19. _, err = client.Index().Index(testIndexName).Type("tweet").Id("1").BodyJson(&tweet1).Do()
  20. if err != nil {
  21. t.Fatal(err)
  22. }
  23. _, err = client.Index().Index(testIndexName).Type("tweet").Id("2").BodyJson(&tweet2).Do()
  24. if err != nil {
  25. t.Fatal(err)
  26. }
  27. // Add tweets to second index
  28. _, err = client.Index().Index(testIndexName2).Type("tweet").Id("3").BodyJson(&tweet3).Do()
  29. if err != nil {
  30. t.Fatal(err)
  31. }
  32. // Flush
  33. _, err = client.Flush().Index(testIndexName).Do()
  34. if err != nil {
  35. t.Fatal(err)
  36. }
  37. _, err = client.Flush().Index(testIndexName2).Do()
  38. if err != nil {
  39. t.Fatal(err)
  40. }
  41. /*
  42. // Alias should not yet exist
  43. aliasesResult1, err := client.Aliases().Do()
  44. if err != nil {
  45. t.Fatal(err)
  46. }
  47. if len(aliasesResult1.Indices) != 0 {
  48. t.Errorf("expected len(AliasesResult.Indices) = %d; got %d", 0, len(aliasesResult1.Indices))
  49. }
  50. */
  51. // Add both indices to a new alias
  52. aliasCreate, err := client.Alias().
  53. Add(testIndexName, testAliasName).
  54. Add(testIndexName2, testAliasName).
  55. //Pretty(true).
  56. Do()
  57. if err != nil {
  58. t.Fatal(err)
  59. }
  60. if !aliasCreate.Acknowledged {
  61. t.Errorf("expected AliasResult.Acknowledged %v; got %v", true, aliasCreate.Acknowledged)
  62. }
  63. // Search should return all 3 tweets
  64. matchAll := NewMatchAllQuery()
  65. searchResult1, err := client.Search().Index(testAliasName).Query(&matchAll).Do()
  66. if err != nil {
  67. t.Fatal(err)
  68. }
  69. if searchResult1.Hits == nil {
  70. t.Errorf("expected SearchResult.Hits != nil; got nil")
  71. }
  72. if searchResult1.Hits.TotalHits != 3 {
  73. t.Errorf("expected SearchResult.Hits.TotalHits = %d; got %d", 3, searchResult1.Hits.TotalHits)
  74. }
  75. /*
  76. // Alias should return both indices
  77. aliasesResult2, err := client.Aliases().Do()
  78. if err != nil {
  79. t.Fatal(err)
  80. }
  81. if len(aliasesResult2.Indices) != 2 {
  82. t.Errorf("expected len(AliasesResult.Indices) = %d; got %d", 2, len(aliasesResult2.Indices))
  83. }
  84. */
  85. // Remove first index should remove two tweets, so should only yield 1
  86. aliasRemove1, err := client.Alias().
  87. Remove(testIndexName, testAliasName).
  88. //Pretty(true).
  89. Do()
  90. if err != nil {
  91. t.Fatal(err)
  92. }
  93. if !aliasRemove1.Acknowledged {
  94. t.Errorf("expected AliasResult.Acknowledged %v; got %v", true, aliasRemove1.Acknowledged)
  95. }
  96. searchResult2, err := client.Search().Index(testAliasName).Query(&matchAll).Do()
  97. if err != nil {
  98. t.Fatal(err)
  99. }
  100. if searchResult2.Hits == nil {
  101. t.Errorf("expected SearchResult.Hits != nil; got nil")
  102. }
  103. if searchResult2.Hits.TotalHits != 1 {
  104. t.Errorf("expected SearchResult.Hits.TotalHits = %d; got %d", 1, searchResult2.Hits.TotalHits)
  105. }
  106. }