indices_stats_test.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 TestIndexStatsBuildURL(t *testing.T) {
  10. client := setupTestClientAndCreateIndex(t)
  11. tests := []struct {
  12. Indices []string
  13. Metrics []string
  14. Expected string
  15. }{
  16. {
  17. []string{},
  18. []string{},
  19. "/_stats",
  20. },
  21. {
  22. []string{"index1"},
  23. []string{},
  24. "/index1/_stats",
  25. },
  26. {
  27. []string{},
  28. []string{"metric1"},
  29. "/_stats/metric1",
  30. },
  31. {
  32. []string{"index1"},
  33. []string{"metric1"},
  34. "/index1/_stats/metric1",
  35. },
  36. {
  37. []string{"index1", "index2"},
  38. []string{"metric1"},
  39. "/index1%2Cindex2/_stats/metric1",
  40. },
  41. {
  42. []string{"index1", "index2"},
  43. []string{"metric1", "metric2"},
  44. "/index1%2Cindex2/_stats/metric1%2Cmetric2",
  45. },
  46. }
  47. for i, test := range tests {
  48. path, _, err := client.IndexStats().Index(test.Indices...).Metric(test.Metrics...).buildURL()
  49. if err != nil {
  50. t.Fatalf("case #%d: %v", i+1, err)
  51. }
  52. if path != test.Expected {
  53. t.Errorf("case #%d: expected %q; got: %q", i+1, test.Expected, path)
  54. }
  55. }
  56. }
  57. func TestIndexStats(t *testing.T) {
  58. client := setupTestClientAndCreateIndexAndAddDocs(t)
  59. stats, err := client.IndexStats(testIndexName).Do(context.TODO())
  60. if err != nil {
  61. t.Fatalf("expected no error; got: %v", err)
  62. }
  63. if stats == nil {
  64. t.Fatalf("expected response; got: %v", stats)
  65. }
  66. stat, found := stats.Indices[testIndexName]
  67. if !found {
  68. t.Fatalf("expected stats about index %q; got: %v", testIndexName, found)
  69. }
  70. if stat.Total == nil {
  71. t.Fatalf("expected total to be != nil; got: %v", stat.Total)
  72. }
  73. if stat.Total.Docs == nil {
  74. t.Fatalf("expected total docs to be != nil; got: %v", stat.Total.Docs)
  75. }
  76. if stat.Total.Docs.Count == 0 {
  77. t.Fatalf("expected total docs count to be > 0; got: %d", stat.Total.Docs.Count)
  78. }
  79. }
  80. func TestIndexStatsWithShards(t *testing.T) {
  81. client := setupTestClientAndCreateIndexAndAddDocs(t)
  82. stats, err := client.IndexStats(testIndexName).Level("shards").Do(context.TODO())
  83. if err != nil {
  84. t.Fatalf("expected no error; got: %v", err)
  85. }
  86. if stats == nil {
  87. t.Fatalf("expected response; got: %v", stats)
  88. }
  89. stat, found := stats.Indices[testIndexName]
  90. if !found {
  91. t.Fatalf("expected stats about index %q; got: %v", testIndexName, found)
  92. }
  93. if stat.Total == nil {
  94. t.Fatalf("expected total to be != nil; got: %v", stat.Total)
  95. }
  96. if stat.Total.Docs == nil {
  97. t.Fatalf("expected total docs to be != nil; got: %v", stat.Total.Docs)
  98. }
  99. if stat.Total.Docs.Count == 0 {
  100. t.Fatalf("expected total docs count to be > 0; got: %d", stat.Total.Docs.Count)
  101. }
  102. if stat.Shards == nil {
  103. t.Fatalf("expected shard level information to be != nil; got: %v", stat.Shards)
  104. }
  105. shard, found := stat.Shards["0"]
  106. if !found || shard == nil {
  107. t.Fatalf("expected shard level information for shard 0; got: %v (found=%v)", shard, found)
  108. }
  109. if len(shard) != 1 {
  110. t.Fatalf("expected shard level information array to be == 1; got: %v", len(shard))
  111. }
  112. if shard[0].Docs == nil {
  113. t.Fatalf("expected docs to be != nil; got: %v", shard[0].Docs)
  114. }
  115. }