delete_by_query_test.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 TestDeleteByQueryBuildURL(t *testing.T) {
  10. client := setupTestClient(t)
  11. tests := []struct {
  12. Indices []string
  13. Types []string
  14. Expected string
  15. ExpectErr bool
  16. }{
  17. {
  18. []string{},
  19. []string{},
  20. "",
  21. true,
  22. },
  23. {
  24. []string{"index1"},
  25. []string{},
  26. "/index1/_delete_by_query",
  27. false,
  28. },
  29. {
  30. []string{"index1", "index2"},
  31. []string{},
  32. "/index1%2Cindex2/_delete_by_query",
  33. false,
  34. },
  35. {
  36. []string{},
  37. []string{"type1"},
  38. "",
  39. true,
  40. },
  41. {
  42. []string{"index1"},
  43. []string{"type1"},
  44. "/index1/type1/_delete_by_query",
  45. false,
  46. },
  47. {
  48. []string{"index1", "index2"},
  49. []string{"type1", "type2"},
  50. "/index1%2Cindex2/type1%2Ctype2/_delete_by_query",
  51. false,
  52. },
  53. }
  54. for i, test := range tests {
  55. builder := client.DeleteByQuery().Index(test.Indices...).Type(test.Types...)
  56. err := builder.Validate()
  57. if err != nil {
  58. if !test.ExpectErr {
  59. t.Errorf("case #%d: %v", i+1, err)
  60. continue
  61. }
  62. } else {
  63. // err == nil
  64. if test.ExpectErr {
  65. t.Errorf("case #%d: expected error", i+1)
  66. continue
  67. }
  68. path, _, _ := builder.buildURL()
  69. if path != test.Expected {
  70. t.Errorf("case #%d: expected %q; got: %q", i+1, test.Expected, path)
  71. }
  72. }
  73. }
  74. }
  75. func TestDeleteByQuery(t *testing.T) {
  76. // client := setupTestClientAndCreateIndex(t, SetTraceLog(log.New(os.Stdout, "", 0)))
  77. client := setupTestClientAndCreateIndex(t)
  78. tweet1 := tweet{User: "olivere", Message: "Welcome to Golang and Elasticsearch."}
  79. tweet2 := tweet{User: "olivere", Message: "Another unrelated topic."}
  80. tweet3 := tweet{User: "sandrae", Message: "Cycling is fun."}
  81. // Add all documents
  82. _, err := client.Index().Index(testIndexName).Type("tweet").Id("1").BodyJson(&tweet1).Do(context.TODO())
  83. if err != nil {
  84. t.Fatal(err)
  85. }
  86. _, err = client.Index().Index(testIndexName).Type("tweet").Id("2").BodyJson(&tweet2).Do(context.TODO())
  87. if err != nil {
  88. t.Fatal(err)
  89. }
  90. _, err = client.Index().Index(testIndexName).Type("tweet").Id("3").BodyJson(&tweet3).Do(context.TODO())
  91. if err != nil {
  92. t.Fatal(err)
  93. }
  94. _, err = client.Flush().Index(testIndexName).Do(context.TODO())
  95. if err != nil {
  96. t.Fatal(err)
  97. }
  98. // Count documents
  99. count, err := client.Count(testIndexName).Do(context.TODO())
  100. if err != nil {
  101. t.Fatal(err)
  102. }
  103. if count != 3 {
  104. t.Fatalf("expected count = %d; got: %d", 3, count)
  105. }
  106. // Delete all documents by sandrae
  107. q := NewTermQuery("user", "sandrae")
  108. res, err := client.DeleteByQuery().
  109. Index(testIndexName).
  110. Type("tweet").
  111. Query(q).
  112. Pretty(true).
  113. Do(context.TODO())
  114. if err != nil {
  115. t.Fatal(err)
  116. }
  117. if res == nil {
  118. t.Fatalf("expected response != nil; got: %v", res)
  119. }
  120. // Flush and check count
  121. _, err = client.Flush().Index(testIndexName).Do(context.TODO())
  122. if err != nil {
  123. t.Fatal(err)
  124. }
  125. count, err = client.Count(testIndexName).Do(context.TODO())
  126. if err != nil {
  127. t.Fatal(err)
  128. }
  129. if count != 2 {
  130. t.Fatalf("expected Count = %d; got: %d", 2, count)
  131. }
  132. }