indices_segments_test.go 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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 TestIndicesSegments(t *testing.T) {
  10. client := setupTestClientAndCreateIndex(t)
  11. tests := []struct {
  12. Indices []string
  13. Expected string
  14. }{
  15. {
  16. []string{},
  17. "/_segments",
  18. },
  19. {
  20. []string{"index1"},
  21. "/index1/_segments",
  22. },
  23. {
  24. []string{"index1", "index2"},
  25. "/index1%2Cindex2/_segments",
  26. },
  27. }
  28. for i, test := range tests {
  29. path, _, err := client.IndexSegments().Index(test.Indices...).buildURL()
  30. if err != nil {
  31. t.Errorf("case #%d: %v", i+1, err)
  32. }
  33. if path != test.Expected {
  34. t.Errorf("case #%d: expected %q; got: %q", i+1, test.Expected, path)
  35. }
  36. }
  37. }
  38. func TestIndexSegments(t *testing.T) {
  39. client := setupTestClientAndCreateIndexAndAddDocs(t)
  40. //client := setupTestClientAndCreateIndexAndAddDocs(t, SetTraceLog(log.New(os.Stdout, "", 0)))
  41. segments, err := client.IndexSegments(testIndexName).Pretty(true).Human(true).Do(context.TODO())
  42. if err != nil {
  43. t.Fatalf("expected no error; got: %v", err)
  44. }
  45. if segments == nil {
  46. t.Fatalf("expected response; got: %v", segments)
  47. }
  48. indices, found := segments.Indices[testIndexName]
  49. if !found {
  50. t.Fatalf("expected index information about index %v; got: %v", testIndexName, found)
  51. }
  52. shards, found := indices.Shards["0"]
  53. if !found {
  54. t.Fatalf("expected shard information about index %v", testIndexName)
  55. }
  56. if shards == nil {
  57. t.Fatalf("expected shard information to be != nil for index %v", testIndexName)
  58. }
  59. shard := shards[0]
  60. if shard == nil {
  61. t.Fatalf("expected shard information to be != nil for shard 0 in index %v", testIndexName)
  62. }
  63. if shard.Routing == nil {
  64. t.Fatalf("expected shard routing information to be != nil for index %v", testIndexName)
  65. }
  66. segmentDetail, found := shard.Segments["_0"]
  67. if !found {
  68. t.Fatalf("expected segment detail to be != nil for index %v", testIndexName)
  69. }
  70. if segmentDetail == nil {
  71. t.Fatalf("expected segment detail to be != nil for index %v", testIndexName)
  72. }
  73. if segmentDetail.NumDocs == 0 {
  74. t.Fatal("expected segment to contain >= 1 docs")
  75. }
  76. }