update_by_query_test.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. "encoding/json"
  8. "testing"
  9. )
  10. func TestUpdateByQueryBuildURL(t *testing.T) {
  11. client := setupTestClient(t)
  12. tests := []struct {
  13. Indices []string
  14. Types []string
  15. Expected string
  16. ExpectErr bool
  17. }{
  18. {
  19. []string{},
  20. []string{},
  21. "",
  22. true,
  23. },
  24. {
  25. []string{"index1"},
  26. []string{},
  27. "/index1/_update_by_query",
  28. false,
  29. },
  30. {
  31. []string{"index1", "index2"},
  32. []string{},
  33. "/index1%2Cindex2/_update_by_query",
  34. false,
  35. },
  36. {
  37. []string{},
  38. []string{"type1"},
  39. "",
  40. true,
  41. },
  42. {
  43. []string{"index1"},
  44. []string{"type1"},
  45. "/index1/type1/_update_by_query",
  46. false,
  47. },
  48. {
  49. []string{"index1", "index2"},
  50. []string{"type1", "type2"},
  51. "/index1%2Cindex2/type1%2Ctype2/_update_by_query",
  52. false,
  53. },
  54. }
  55. for i, test := range tests {
  56. builder := client.UpdateByQuery().Index(test.Indices...).Type(test.Types...)
  57. err := builder.Validate()
  58. if err != nil {
  59. if !test.ExpectErr {
  60. t.Errorf("case #%d: %v", i+1, err)
  61. continue
  62. }
  63. } else {
  64. // err == nil
  65. if test.ExpectErr {
  66. t.Errorf("case #%d: expected error", i+1)
  67. continue
  68. }
  69. path, _, _ := builder.buildURL()
  70. if path != test.Expected {
  71. t.Errorf("case #%d: expected %q; got: %q", i+1, test.Expected, path)
  72. }
  73. }
  74. }
  75. }
  76. func TestUpdateByQueryBodyWithQuery(t *testing.T) {
  77. client := setupTestClient(t)
  78. out, err := client.UpdateByQuery().Query(NewTermQuery("user", "olivere")).getBody()
  79. if err != nil {
  80. t.Fatal(err)
  81. }
  82. b, err := json.Marshal(out)
  83. if err != nil {
  84. t.Fatal(err)
  85. }
  86. got := string(b)
  87. want := `{"query":{"term":{"user":"olivere"}}}`
  88. if got != want {
  89. t.Fatalf("\ngot %s\nwant %s", got, want)
  90. }
  91. }
  92. func TestUpdateByQueryBodyWithQueryAndScript(t *testing.T) {
  93. client := setupTestClient(t)
  94. out, err := client.UpdateByQuery().
  95. Query(NewTermQuery("user", "olivere")).
  96. Script(NewScriptInline("ctx._source.likes++")).
  97. getBody()
  98. if err != nil {
  99. t.Fatal(err)
  100. }
  101. b, err := json.Marshal(out)
  102. if err != nil {
  103. t.Fatal(err)
  104. }
  105. got := string(b)
  106. want := `{"query":{"term":{"user":"olivere"}},"script":{"inline":"ctx._source.likes++"}}`
  107. if got != want {
  108. t.Fatalf("\ngot %s\nwant %s", got, want)
  109. }
  110. }
  111. func TestUpdateByQuery(t *testing.T) {
  112. client := setupTestClientAndCreateIndexAndAddDocs(t) //, SetTraceLog(log.New(os.Stdout, "", 0)))
  113. esversion, err := client.ElasticsearchVersion(DefaultURL)
  114. if err != nil {
  115. t.Fatal(err)
  116. }
  117. if esversion < "2.3.0" {
  118. t.Skipf("Elasticsearch %v does not support update-by-query yet", esversion)
  119. }
  120. sourceCount, err := client.Count(testIndexName).Do(context.TODO())
  121. if err != nil {
  122. t.Fatal(err)
  123. }
  124. if sourceCount <= 0 {
  125. t.Fatalf("expected more than %d documents; got: %d", 0, sourceCount)
  126. }
  127. res, err := client.UpdateByQuery(testIndexName).ProceedOnVersionConflict().Do(context.TODO())
  128. if err != nil {
  129. t.Fatal(err)
  130. }
  131. if res == nil {
  132. t.Fatal("response is nil")
  133. }
  134. if res.Updated != sourceCount {
  135. t.Fatalf("expected %d; got: %d", sourceCount, res.Updated)
  136. }
  137. }