indices_refresh_test.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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 TestRefreshBuildURL(t *testing.T) {
  10. client := setupTestClient(t)
  11. tests := []struct {
  12. Indices []string
  13. Expected string
  14. }{
  15. {
  16. []string{},
  17. "/_refresh",
  18. },
  19. {
  20. []string{"index1"},
  21. "/index1/_refresh",
  22. },
  23. {
  24. []string{"index1", "index2"},
  25. "/index1%2Cindex2/_refresh",
  26. },
  27. }
  28. for i, test := range tests {
  29. path, _, err := client.Refresh().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 TestRefresh(t *testing.T) {
  40. client := setupTestClientAndCreateIndex(t)
  41. tweet1 := tweet{User: "olivere", Message: "Welcome to Golang and Elasticsearch."}
  42. tweet2 := tweet{User: "olivere", Message: "Another unrelated topic."}
  43. tweet3 := tweet{User: "sandrae", Message: "Cycling is fun."}
  44. // Add some documents
  45. _, err := client.Index().Index(testIndexName).Type("tweet").Id("1").BodyJson(&tweet1).Do(context.TODO())
  46. if err != nil {
  47. t.Fatal(err)
  48. }
  49. _, err = client.Index().Index(testIndexName).Type("tweet").Id("2").BodyJson(&tweet2).Do(context.TODO())
  50. if err != nil {
  51. t.Fatal(err)
  52. }
  53. _, err = client.Index().Index(testIndexName).Type("tweet").Id("3").BodyJson(&tweet3).Do(context.TODO())
  54. if err != nil {
  55. t.Fatal(err)
  56. }
  57. _, err = client.Flush().Index(testIndexName).Do(context.TODO())
  58. if err != nil {
  59. t.Fatal(err)
  60. }
  61. // Refresh indices
  62. res, err := client.Refresh(testIndexName, testIndexName2).Do(context.TODO())
  63. if err != nil {
  64. t.Fatal(err)
  65. }
  66. if res == nil {
  67. t.Fatal("expected result; got nil")
  68. }
  69. }