count_test.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 TestCountURL(t *testing.T) {
  10. client := setupTestClientAndCreateIndex(t)
  11. tests := []struct {
  12. Indices []string
  13. Types []string
  14. Expected string
  15. }{
  16. {
  17. []string{},
  18. []string{},
  19. "/_all/_count",
  20. },
  21. {
  22. []string{},
  23. []string{"tweet"},
  24. "/_all/tweet/_count",
  25. },
  26. {
  27. []string{"twitter-*"},
  28. []string{"tweet", "follower"},
  29. "/twitter-%2A/tweet%2Cfollower/_count",
  30. },
  31. {
  32. []string{"twitter-2014", "twitter-2015"},
  33. []string{"tweet", "follower"},
  34. "/twitter-2014%2Ctwitter-2015/tweet%2Cfollower/_count",
  35. },
  36. }
  37. for _, test := range tests {
  38. path, _, err := client.Count().Index(test.Indices...).Type(test.Types...).buildURL()
  39. if err != nil {
  40. t.Fatal(err)
  41. }
  42. if path != test.Expected {
  43. t.Errorf("expected %q; got: %q", test.Expected, path)
  44. }
  45. }
  46. }
  47. func TestCount(t *testing.T) {
  48. client := setupTestClientAndCreateIndex(t)
  49. tweet1 := tweet{User: "olivere", Message: "Welcome to Golang and Elasticsearch."}
  50. tweet2 := tweet{User: "olivere", Message: "Another unrelated topic."}
  51. tweet3 := tweet{User: "sandrae", Message: "Cycling is fun."}
  52. // Add all documents
  53. _, err := client.Index().Index(testIndexName).Type("tweet").Id("1").BodyJson(&tweet1).Do(context.TODO())
  54. if err != nil {
  55. t.Fatal(err)
  56. }
  57. _, err = client.Index().Index(testIndexName).Type("tweet").Id("2").BodyJson(&tweet2).Do(context.TODO())
  58. if err != nil {
  59. t.Fatal(err)
  60. }
  61. _, err = client.Index().Index(testIndexName).Type("tweet").Id("3").BodyJson(&tweet3).Do(context.TODO())
  62. if err != nil {
  63. t.Fatal(err)
  64. }
  65. _, err = client.Flush().Index(testIndexName).Do(context.TODO())
  66. if err != nil {
  67. t.Fatal(err)
  68. }
  69. // Count documents
  70. count, err := client.Count(testIndexName).Do(context.TODO())
  71. if err != nil {
  72. t.Fatal(err)
  73. }
  74. if count != 3 {
  75. t.Errorf("expected Count = %d; got %d", 3, count)
  76. }
  77. // Count documents
  78. count, err = client.Count(testIndexName).Type("tweet").Do(context.TODO())
  79. if err != nil {
  80. t.Fatal(err)
  81. }
  82. if count != 3 {
  83. t.Errorf("expected Count = %d; got %d", 3, count)
  84. }
  85. // Count documents
  86. count, err = client.Count(testIndexName).Type("gezwitscher").Do(context.TODO())
  87. if err != nil {
  88. t.Fatal(err)
  89. }
  90. if count != 0 {
  91. t.Errorf("expected Count = %d; got %d", 0, count)
  92. }
  93. // Count with query
  94. query := NewTermQuery("user", "olivere")
  95. count, err = client.Count(testIndexName).Query(query).Do(context.TODO())
  96. if err != nil {
  97. t.Fatal(err)
  98. }
  99. if count != 2 {
  100. t.Errorf("expected Count = %d; got %d", 2, count)
  101. }
  102. // Count with query and type
  103. query = NewTermQuery("user", "olivere")
  104. count, err = client.Count(testIndexName).Type("tweet").Query(query).Do(context.TODO())
  105. if err != nil {
  106. t.Fatal(err)
  107. }
  108. if count != 2 {
  109. t.Errorf("expected Count = %d; got %d", 2, count)
  110. }
  111. }