elastomer-client 3.2.3 → 6.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (114) hide show
  1. checksums.yaml +4 -4
  2. data/.devcontainer/devcontainer.json +46 -0
  3. data/.devcontainer/postCreateCommand.sh +4 -0
  4. data/.github/dependabot.yaml +11 -0
  5. data/.github/workflows/main.yml +45 -0
  6. data/.github/workflows/rubocop.yml +15 -0
  7. data/.gitignore +1 -1
  8. data/.rubocop.yml +13 -65
  9. data/.ruby-version +1 -0
  10. data/CHANGELOG.md +73 -0
  11. data/Gemfile +18 -1
  12. data/README.md +110 -51
  13. data/Rakefile +3 -1
  14. data/docker/compose.yaml +71 -0
  15. data/docker/elasticsearch8plus.yml +13 -0
  16. data/docs/README.md +4 -5
  17. data/docs/bulk_indexing.md +1 -1
  18. data/docs/client.md +20 -33
  19. data/docs/cluster.md +8 -8
  20. data/docs/docs.md +5 -5
  21. data/docs/index.md +4 -4
  22. data/docs/multi_search.md +1 -1
  23. data/docs/notifications.md +3 -3
  24. data/docs/scan_scroll.md +1 -1
  25. data/docs/snapshots.md +1 -1
  26. data/docs/templates.md +1 -1
  27. data/elastomer-client.gemspec +7 -16
  28. data/lib/{elastomer → elastomer_client}/client/bulk.rb +70 -47
  29. data/lib/{elastomer → elastomer_client}/client/cluster.rb +18 -16
  30. data/lib/{elastomer → elastomer_client}/client/delete_by_query.rb +6 -4
  31. data/lib/{elastomer → elastomer_client}/client/docs.rb +82 -72
  32. data/lib/{elastomer → elastomer_client}/client/errors.rb +7 -17
  33. data/lib/{elastomer → elastomer_client}/client/index.rb +55 -79
  34. data/lib/{elastomer → elastomer_client}/client/multi_percolate.rb +7 -5
  35. data/lib/{elastomer → elastomer_client}/client/multi_search.rb +5 -3
  36. data/lib/{elastomer → elastomer_client}/client/native_delete_by_query.rb +6 -6
  37. data/lib/{elastomer → elastomer_client}/client/nodes.rb +11 -10
  38. data/lib/{elastomer → elastomer_client}/client/percolator.rb +9 -10
  39. data/lib/elastomer_client/client/reindex.rb +29 -0
  40. data/lib/{elastomer → elastomer_client}/client/repository.rb +7 -5
  41. data/lib/{elastomer → elastomer_client}/client/rest_api_spec/api_spec.rb +7 -6
  42. data/lib/{elastomer → elastomer_client}/client/rest_api_spec/api_spec_v5_6.rb +1 -1
  43. data/lib/elastomer_client/client/rest_api_spec/api_spec_v8_13.rb +7567 -0
  44. data/lib/elastomer_client/client/rest_api_spec/api_spec_v8_7.rb +6553 -0
  45. data/lib/{elastomer → elastomer_client}/client/rest_api_spec/rest_api.rb +5 -3
  46. data/lib/{elastomer → elastomer_client}/client/rest_api_spec.rb +3 -2
  47. data/lib/{elastomer → elastomer_client}/client/scroller.rb +17 -16
  48. data/lib/{elastomer → elastomer_client}/client/snapshot.rb +10 -8
  49. data/lib/{elastomer → elastomer_client}/client/tasks.rb +9 -13
  50. data/lib/{elastomer → elastomer_client}/client/template.rb +10 -9
  51. data/lib/elastomer_client/client/update_by_query.rb +50 -0
  52. data/lib/{elastomer → elastomer_client}/client.rb +51 -62
  53. data/lib/{elastomer → elastomer_client}/core_ext/time.rb +2 -0
  54. data/lib/{elastomer → elastomer_client}/middleware/compress.rb +2 -2
  55. data/lib/{elastomer → elastomer_client}/middleware/encode_json.rb +4 -2
  56. data/lib/{elastomer → elastomer_client}/middleware/limit_size.rb +5 -3
  57. data/lib/{elastomer → elastomer_client}/middleware/opaque_id.rb +10 -7
  58. data/lib/{elastomer → elastomer_client}/middleware/parse_json.rb +5 -3
  59. data/lib/{elastomer → elastomer_client}/notifications.rb +17 -15
  60. data/lib/elastomer_client/version.rb +9 -0
  61. data/lib/elastomer_client/version_support.rb +24 -0
  62. data/script/bootstrap +4 -2
  63. data/script/console +3 -1
  64. data/script/generate-rest-api-spec +77 -22
  65. data/test/assertions.rb +32 -39
  66. data/test/client/bulk_test.rb +166 -141
  67. data/test/client/cluster_test.rb +35 -13
  68. data/test/client/docs_test.rb +387 -274
  69. data/test/client/errors_test.rb +38 -40
  70. data/test/client/index_test.rb +243 -202
  71. data/test/client/multi_percolate_test.rb +46 -41
  72. data/test/client/multi_search_test.rb +122 -67
  73. data/test/client/native_delete_by_query_test.rb +96 -88
  74. data/test/client/nodes_test.rb +21 -10
  75. data/test/client/percolator_test.rb +19 -14
  76. data/test/client/reindex_test.rb +76 -0
  77. data/test/client/repository_test.rb +31 -19
  78. data/test/client/rest_api_spec/api_spec_test.rb +13 -11
  79. data/test/client/rest_api_spec/rest_api_test.rb +9 -7
  80. data/test/client/scroller_test.rb +44 -70
  81. data/test/client/snapshot_test.rb +38 -21
  82. data/test/client/stubbed_client_test.rb +7 -4
  83. data/test/client/tasks_test.rb +12 -17
  84. data/test/client/template_test.rb +34 -13
  85. data/test/client/update_by_query_test.rb +137 -0
  86. data/test/client_test.rb +158 -92
  87. data/test/core_ext/time_test.rb +14 -12
  88. data/test/middleware/encode_json_test.rb +18 -7
  89. data/test/middleware/opaque_id_test.rb +18 -14
  90. data/test/middleware/parse_json_test.rb +17 -9
  91. data/test/mock_response.rb +30 -0
  92. data/test/notifications_test.rb +15 -8
  93. data/test/test_helper.rb +40 -97
  94. data/test/version_support_test.rb +13 -78
  95. metadata +60 -208
  96. data/.overcommit.yml +0 -5
  97. data/.travis.yml +0 -34
  98. data/docker/docker-compose.cibuild.yml +0 -8
  99. data/docker/docker-compose.es24.yml +0 -34
  100. data/docker/docker-compose.es56.yml +0 -37
  101. data/docs/warmers.md +0 -3
  102. data/lib/elastomer/client/app_delete_by_query.rb +0 -144
  103. data/lib/elastomer/client/rest_api_spec/api_spec_v2_3.rb +0 -2232
  104. data/lib/elastomer/client/rest_api_spec/api_spec_v2_4.rb +0 -2250
  105. data/lib/elastomer/client/warmer.rb +0 -98
  106. data/lib/elastomer/version.rb +0 -7
  107. data/lib/elastomer/version_support.rb +0 -182
  108. data/script/cibuild +0 -103
  109. data/script/cibuild-elastomer-client +0 -1
  110. data/script/cibuild-elastomer-client-es24 +0 -8
  111. data/script/cibuild-elastomer-client-es56 +0 -8
  112. data/test/client/app_delete_by_query_test.rb +0 -192
  113. data/test/client/es_5_x_warmer_test.rb +0 -13
  114. data/test/client/warmer_test.rb +0 -60
@@ -1,36 +1,39 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require_relative "../test_helper"
2
4
 
3
- describe Elastomer::Client::MultiPercolate do
5
+ describe ElastomerClient::Client::MultiPercolate do
4
6
 
5
7
  before do
8
+ if $client.version_support.es_version_8_plus?
9
+ skip "Multi percolate not supported in ES version #{$client.version}"
10
+ end
11
+
6
12
  @name = "elastomer-mpercolate-test"
7
13
  @index = $client.index(@name)
8
14
 
9
15
  unless @index.exists?
10
16
  base_mappings_settings = {
11
- :settings => { "index.number_of_shards" => 1, "index.number_of_replicas" => 0 },
12
- :mappings => {
13
- :doc1 => {
14
- :_source => { :enabled => true }, :_all => { :enabled => false },
15
- :properties => {
16
- :title => $client.version_support.text(analyzer: "standard"),
17
- :author => $client.version_support.keyword
17
+ settings: { "index.number_of_shards" => 1, "index.number_of_replicas" => 0 },
18
+ mappings: {
19
+ doc1: {
20
+ _source: { enabled: true }, _all: { enabled: false },
21
+ properties: {
22
+ title: { type: "text", analyzer: "standard" },
23
+ author: { type: "keyword" }
18
24
  }
19
25
  },
20
- :doc2 => {
21
- :_source => { :enabled => true }, :_all => { :enabled => false },
22
- :properties => {
23
- :title => $client.version_support.text(analyzer: "standard"),
24
- :author => $client.version_support.keyword
26
+ doc2: {
27
+ _source: { enabled: true }, _all: { enabled: false },
28
+ properties: {
29
+ title: { type: "text", analyzer: "standard" },
30
+ author: { type: "keyword" }
25
31
  }
26
32
  }
27
33
  }
28
34
  }
29
35
 
30
- # COMPATIBILITY
31
- if requires_percolator_mapping?
32
- base_mappings_settings[:mappings][:percolator] = { :properties => { :query => { :type => "percolator" } } }
33
- end
36
+ base_mappings_settings[:mappings][:percolator] = { properties: { query: { type: "percolator" } } }
34
37
 
35
38
  @index.create base_mappings_settings
36
39
  wait_for_index(@name)
@@ -58,6 +61,7 @@ describe Elastomer::Client::MultiPercolate do
58
61
  body = body.join "\n"
59
62
  h = $client.multi_percolate body
60
63
  response1, response2, response3 = h["responses"]
64
+
61
65
  assert_equal ["1", "2"], response1["matches"].map { |match| match["_id"] }.sort
62
66
  assert_equal ["1", "3"], response2["matches"].map { |match| match["_id"] }.sort
63
67
  assert_equal 2, response3["total"]
@@ -78,6 +82,7 @@ describe Elastomer::Client::MultiPercolate do
78
82
  body = body.join "\n"
79
83
  h = $client.mpercolate body
80
84
  response1, response2, response3 = h["responses"]
85
+
81
86
  assert_equal ["1", "2"], response1["matches"].map { |match| match["_id"] }.sort
82
87
  assert_equal ["1", "3"], response2["matches"].map { |match| match["_id"] }.sort
83
88
  assert_equal 2, response3["total"]
@@ -86,50 +91,50 @@ describe Elastomer::Client::MultiPercolate do
86
91
  it "supports a nice block syntax" do
87
92
  populate!
88
93
 
89
- h = $client.multi_percolate(:index => @name, :type => "doc2") do |m|
90
- m.percolate :author => "pea53"
91
- m.percolate :author => "grantr"
92
- m.count({ :author => "grantr" })
94
+ h = $client.multi_percolate(index: @name, type: "doc2") do |m|
95
+ m.percolate author: "pea53"
96
+ m.percolate author: "grantr"
97
+ m.count({ author: "grantr" })
93
98
  end
94
99
 
95
100
  response1, response2, response3 = h["responses"]
101
+
96
102
  assert_equal ["1", "2"], response1["matches"].map { |match| match["_id"] }.sort
97
103
  assert_equal ["1", "3"], response2["matches"].map { |match| match["_id"] }.sort
98
104
  assert_equal 2, response3["total"]
99
105
  end
100
106
 
101
- # rubocop:disable Metrics/MethodLength
102
107
  def populate!
103
108
  @docs.index \
104
- :_id => 1,
105
- :_type => "doc1",
106
- :title => "the author of gravatar",
107
- :author => "mojombo"
109
+ _id: 1,
110
+ _type: "doc1",
111
+ title: "the author of gravatar",
112
+ author: "mojombo"
108
113
 
109
114
  @docs.index \
110
- :_id => 2,
111
- :_type => "doc1",
112
- :title => "the author of resque",
113
- :author => "defunkt"
115
+ _id: 2,
116
+ _type: "doc1",
117
+ title: "the author of resque",
118
+ author: "defunkt"
114
119
 
115
120
  @docs.index \
116
- :_id => 1,
117
- :_type => "doc2",
118
- :title => "the author of logging",
119
- :author => "pea53"
121
+ _id: 1,
122
+ _type: "doc2",
123
+ title: "the author of logging",
124
+ author: "pea53"
120
125
 
121
126
  @docs.index \
122
- :_id => 2,
123
- :_type => "doc2",
124
- :title => "the author of rubber-band",
125
- :author => "grantr"
127
+ _id: 2,
128
+ _type: "doc2",
129
+ title: "the author of rubber-band",
130
+ author: "grantr"
126
131
 
127
132
  percolator1 = @index.percolator "1"
128
- percolator1.create :query => { :match_all => { } }
133
+ percolator1.create query: { match_all: { } }
129
134
  percolator2 = @index.percolator "2"
130
- percolator2.create :query => { :match => { :author => "pea53" } }
135
+ percolator2.create query: { match: { author: "pea53" } }
131
136
  percolator2 = @index.percolator "3"
132
- percolator2.create :query => { :match => { :author => "grantr" } }
137
+ percolator2.create query: { match: { author: "grantr" } }
133
138
 
134
139
  @index.refresh
135
140
  end
@@ -1,6 +1,8 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require_relative "../test_helper"
2
4
 
3
- describe Elastomer::Client::MultiSearch do
5
+ describe ElastomerClient::Client::MultiSearch do
4
6
 
5
7
  before do
6
8
  @name = "elastomer-msearch-test"
@@ -8,24 +10,14 @@ describe Elastomer::Client::MultiSearch do
8
10
 
9
11
  unless @index.exists?
10
12
  @index.create \
11
- :settings => { "index.number_of_shards" => 1, "index.number_of_replicas" => 0 },
12
- :mappings => {
13
- :doc1 => {
14
- :_source => { :enabled => true }, :_all => { :enabled => false },
15
- :properties => {
16
- :title => $client.version_support.text(analyzer: "standard"),
17
- :author => $client.version_support.keyword
18
- }
19
- },
20
- :doc2 => {
21
- :_source => { :enabled => true }, :_all => { :enabled => false },
22
- :properties => {
23
- :title => $client.version_support.text(analyzer: "standard"),
24
- :author => $client.version_support.keyword
25
- }
13
+ settings: { "index.number_of_shards" => 1, "index.number_of_replicas" => 0 },
14
+ mappings: mappings_wrapper("book", {
15
+ _source: { enabled: true },
16
+ properties: {
17
+ title: { type: "text", analyzer: "standard" },
18
+ author: { type: "keyword" }
26
19
  }
27
- }
28
-
20
+ }, !$client.version_support.es_version_8_plus?)
29
21
  wait_for_index(@name)
30
22
  end
31
23
 
@@ -40,28 +32,41 @@ describe Elastomer::Client::MultiSearch do
40
32
  populate!
41
33
 
42
34
  body = [
43
- '{"index" : "elastomer-msearch-test", "size" : 0}',
35
+ '{"index" : "elastomer-msearch-test"}',
44
36
  '{"query" : {"match_all" : {}}}',
45
- '{"index" : "elastomer-msearch-test", "type": "doc2"}',
46
- '{"query" : {"match": {"author" : "grantr"}}}',
37
+ '{"index" : "elastomer-msearch-test"}',
38
+ '{"query" : {"match": {"author" : "Author 2"}}}',
47
39
  nil
48
40
  ]
49
41
  body = body.join "\n"
50
42
  h = $client.multi_search body
51
43
  response1, response2 = h["responses"]
52
- assert_equal 4, response1["hits"]["total"]
53
- assert_equal 1, response2["hits"]["total"]
44
+
45
+ if $client.version_support.es_version_8_plus?
46
+ assert_equal 2, response1["hits"]["total"]["value"]
47
+ assert_equal 1, response2["hits"]["total"]["value"]
48
+ else
49
+ assert_equal 2, response1["hits"]["total"]
50
+ assert_equal 1, response2["hits"]["total"]
51
+ end
52
+
54
53
  assert_equal "2", response2["hits"]["hits"][0]["_id"]
55
54
 
56
55
  body = [
57
56
  "{}",
58
- '{"query" : {"match": {"author" : "grantr"}}}',
57
+ '{"query" : {"match": {"author" : "Author 2"}}}',
59
58
  nil
60
59
  ]
61
60
  body = body.join "\n"
62
- h = $client.multi_search body, :index => @name
61
+ h = $client.multi_search body, index: @name
63
62
  response1 = h["responses"].first
64
- assert_equal 1, response1["hits"]["total"]
63
+
64
+ if $client.version_support.es_version_8_plus?
65
+ assert_equal 1, response1["hits"]["total"]["value"]
66
+ else
67
+ assert_equal 1, response1["hits"]["total"]
68
+ end
69
+
65
70
  assert_equal "2", response1["hits"]["hits"][0]["_id"]
66
71
  end
67
72
 
@@ -69,28 +74,41 @@ describe Elastomer::Client::MultiSearch do
69
74
  populate!
70
75
 
71
76
  body = [
72
- '{"index" : "elastomer-msearch-test", "size" : 0}',
77
+ '{"index" : "elastomer-msearch-test"}',
73
78
  '{"query" : {"match_all" : {}}}',
74
- '{"index" : "elastomer-msearch-test", "type": "doc2"}',
75
- '{"query" : {"match": {"author" : "grantr"}}}',
79
+ '{"index" : "elastomer-msearch-test"}',
80
+ '{"query" : {"match": {"author" : "Author 2"}}}',
76
81
  nil
77
82
  ]
78
83
  body = body.join "\n"
79
84
  h = $client.msearch body
80
85
  response1, response2 = h["responses"]
81
- assert_equal 4, response1["hits"]["total"]
82
- assert_equal 1, response2["hits"]["total"]
86
+
87
+ if $client.version_support.es_version_8_plus?
88
+ assert_equal 2, response1["hits"]["total"]["value"]
89
+ assert_equal 1, response2["hits"]["total"]["value"]
90
+ else
91
+ assert_equal 2, response1["hits"]["total"]
92
+ assert_equal 1, response2["hits"]["total"]
93
+ end
94
+
83
95
  assert_equal "2", response2["hits"]["hits"][0]["_id"]
84
96
 
85
97
  body = [
86
98
  "{}",
87
- '{"query" : {"match": {"author" : "grantr"}}}',
99
+ '{"query" : {"match": {"author" : "Author 2"}}}',
88
100
  nil
89
101
  ]
90
102
  body = body.join "\n"
91
- h = $client.msearch body, :index => @name
103
+ h = $client.msearch body, index: @name
92
104
  response1 = h["responses"].first
93
- assert_equal 1, response1["hits"]["total"]
105
+
106
+ if $client.version_support.es_version_8_plus?
107
+ assert_equal 1, response1["hits"]["total"]["value"]
108
+ else
109
+ assert_equal 1, response1["hits"]["total"]
110
+ end
111
+
94
112
  assert_equal "2", response1["hits"]["hits"][0]["_id"]
95
113
  end
96
114
 
@@ -98,61 +116,98 @@ describe Elastomer::Client::MultiSearch do
98
116
  populate!
99
117
 
100
118
  h = $client.multi_search do |m|
101
- m.search({:query => { :match_all => {}}}, :index => @name, :size => 0)
102
- m.search({:query => { :match => { "title" => "author" }}}, :index => @name, :type => "doc2")
119
+ m.search({query: { match_all: {}}}, index: @name)
120
+ m.search({query: { match: { "title" => "author" }}}, index: @name)
103
121
  end
104
122
 
105
123
  response1, response2 = h["responses"]
106
124
 
107
- assert_equal 4, response1["hits"]["total"]
108
- assert_equal 2, response2["hits"]["total"]
125
+ if $client.version_support.es_version_8_plus?
126
+ assert_equal 2, response1["hits"]["total"]["value"]
127
+ assert_equal 2, response2["hits"]["total"]["value"]
128
+ else
129
+ assert_equal 2, response1["hits"]["total"]
130
+ assert_equal 2, response2["hits"]["total"]
131
+ end
109
132
 
110
133
  h = @index.multi_search do |m|
111
- m.search({:query => { :match_all => {}}}, :size => 0)
112
- m.search({:query => { :match => { "title" => "author" }}}, :type => "doc2")
134
+ m.search({query: { match_all: {}}})
135
+ m.search({query: { match: { "title" => "author" }}})
113
136
  end
114
137
 
115
138
  response1, response2 = h["responses"]
116
139
 
117
- assert_equal 4, response1["hits"]["total"]
118
- assert_equal 2, response2["hits"]["total"]
140
+ if $client.version_support.es_version_8_plus?
141
+ assert_equal 2, response1["hits"]["total"]["value"]
142
+ assert_equal 2, response2["hits"]["total"]["value"]
143
+ else
144
+ assert_equal 2, response1["hits"]["total"]
145
+ assert_equal 2, response2["hits"]["total"]
146
+ end
119
147
 
120
- h = @index.docs("doc1").multi_search do |m|
121
- m.search({:query => { :match_all => {}}}, :size => 0)
122
- m.search({:query => { :match => { "title" => "logging" }}}, :type => "doc2")
148
+ type = $client.version_support.es_version_8_plus? ? "" : "book"
149
+ h = @index.docs(type).multi_search do |m|
150
+ m.search({query: { match_all: {}}})
151
+ m.search({query: { match: { "title" => "2" }}})
123
152
  end
124
153
 
125
154
  response1, response2 = h["responses"]
126
155
 
127
- assert_equal 2, response1["hits"]["total"]
128
- assert_equal 1, response2["hits"]["total"]
156
+ if $client.version_support.es_version_8_plus?
157
+ assert_equal 2, response1["hits"]["total"]["value"]
158
+ assert_equal 1, response2["hits"]["total"]["value"]
159
+ else
160
+ assert_equal 2, response1["hits"]["total"]
161
+ assert_equal 1, response2["hits"]["total"]
162
+ end
129
163
  end
130
164
 
131
- # rubocop:disable Metrics/MethodLength
132
- def populate!
133
- @docs.index \
134
- :_id => 1,
135
- :_type => "doc1",
136
- :title => "the author of gravatar",
137
- :author => "mojombo"
165
+ it "performs suggestion queries using the search endpoint" do
166
+ populate!
138
167
 
139
- @docs.index \
140
- :_id => 2,
141
- :_type => "doc1",
142
- :title => "the author of resque",
143
- :author => "defunkt"
168
+ h = @index.multi_search do |m|
169
+ m.search({
170
+ query: {
171
+ match: {
172
+ title: "by author"
173
+ }
174
+ },
175
+ suggest: {
176
+ suggestion1: {
177
+ text: "by author",
178
+ term: {
179
+ field: "author"
180
+ }
181
+ }
182
+ }
183
+ })
184
+ end
144
185
 
186
+ response = h["responses"][0]
187
+
188
+ if $client.version_support.es_version_8_plus?
189
+ assert_equal 2, response["hits"]["total"]["value"]
190
+ else
191
+ assert_equal 2, response["hits"]["total"]
192
+ end
193
+
194
+ refute_nil response["suggest"], "expected suggester text to be returned"
195
+ end
196
+
197
+ def populate!
145
198
  @docs.index \
146
- :_id => 1,
147
- :_type => "doc2",
148
- :title => "the author of logging",
149
- :author => "pea53"
199
+ document_wrapper("book", {
200
+ _id: 1,
201
+ title: "Book 1 by author 1",
202
+ author: "Author 1"
203
+ })
150
204
 
151
205
  @docs.index \
152
- :_id => 2,
153
- :_type => "doc2",
154
- :title => "the author of rubber-band",
155
- :author => "grantr"
206
+ document_wrapper("book", {
207
+ _id: 2,
208
+ title: "Book 2 by author 2",
209
+ author: "Author 2"
210
+ })
156
211
 
157
212
  @index.refresh
158
213
  end
@@ -1,6 +1,8 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require_relative "../test_helper"
2
4
 
3
- describe Elastomer::Client::NativeDeleteByQuery do
5
+ describe ElastomerClient::Client::NativeDeleteByQuery do
4
6
  before do
5
7
  @index = $client.index "elastomer-delete-by-query-test"
6
8
  @index.delete if @index.exists?
@@ -11,113 +13,119 @@ describe Elastomer::Client::NativeDeleteByQuery do
11
13
  @index.delete if @index.exists?
12
14
  end
13
15
 
14
- if supports_native_delete_by_query?
15
- describe "when an index with documents exists" do
16
- before do
17
- @index.create(nil)
18
- wait_for_index(@index.name)
19
- end
16
+ describe "when an index with documents exists" do
17
+ before do
18
+ @index.create(nil)
19
+ wait_for_index(@index.name)
20
+ end
20
21
 
21
- it "deletes by query" do
22
- @docs.index({ :_id => 0, :name => "mittens" })
23
- @docs.index({ :_id => 1, :name => "luna" })
22
+ it "deletes by query" do
23
+ @docs.index({ _id: 0, name: "mittens" })
24
+ @docs.index({ _id: 1, name: "luna" })
24
25
 
25
- @index.refresh
26
+ @index.refresh
26
27
 
27
- query = {
28
- query: {
29
- match: {
30
- name: "mittens"
31
- }
28
+ query = {
29
+ query: {
30
+ match: {
31
+ name: "mittens"
32
32
  }
33
33
  }
34
+ }
34
35
 
35
- response = @index.native_delete_by_query(query)
36
- refute_nil response["took"]
37
- assert_equal(false, response["timed_out"])
38
- assert_equal(1, response["batches"])
39
- assert_equal(1, response["total"])
40
- assert_equal(1, response["deleted"])
41
- assert_empty(response["failures"])
42
-
43
- @index.refresh
44
- response = @docs.multi_get(:ids => [0, 1])
45
- refute_found response.fetch("docs")[0]
46
- assert_found response.fetch("docs")[1]
47
- end
36
+ response = @index.native_delete_by_query(query)
37
+
38
+ refute_nil response["took"]
39
+ refute(response["timed_out"])
40
+ assert_equal(1, response["batches"])
41
+ assert_equal(1, response["total"])
42
+ assert_equal(1, response["deleted"])
43
+ assert_empty(response["failures"])
48
44
 
49
- it "fails when internal version is 0" do
50
- @docs.index({_id: 0, name: "mittens"})
51
- # Creating a document with external version 0 also sets the internal version to 0
52
- # Otherwise you can't index a document with version 0.
53
- @docs.index({_id: 1, _version: 0, _version_type: "external", name: "mittens"})
54
- @index.refresh
55
-
56
- query = {
57
- query: {
58
- match: {
59
- name: "mittens"
60
- }
45
+ @index.refresh
46
+ response = @docs.multi_get(ids: [0, 1])
47
+
48
+ refute_found response.fetch("docs")[0]
49
+ assert_found response.fetch("docs")[1]
50
+ end
51
+
52
+ it "fails when internal version is 0" do
53
+ if $client.version_support.es_version_8_plus?
54
+ skip "Concurrency control with internal version is not supported in ES #{$client.version}"
55
+ end
56
+ @docs.index({_id: 0, name: "mittens"})
57
+ # Creating a document with external version 0 also sets the internal version to 0
58
+ # Otherwise you can't index a document with version 0.
59
+ @docs.index({_id: 1, _version: 0, _version_type: "external", name: "mittens"})
60
+ @index.refresh
61
+
62
+ query = {
63
+ query: {
64
+ match: {
65
+ name: "mittens"
61
66
  }
62
67
  }
68
+ }
63
69
 
64
- assert_raises(Elastomer::Client::RequestError) do
65
- @index.native_delete_by_query(query)
66
- end
70
+ assert_raises(ElastomerClient::Client::RequestError) do
71
+ @index.native_delete_by_query(query)
67
72
  end
73
+ end
68
74
 
69
- it "fails when an unknown parameter is provided" do
70
- assert_raises(Elastomer::Client::IllegalArgument) do
71
- @index.native_delete_by_query({}, foo: "bar")
72
- end
75
+ it "fails when an unknown parameter is provided" do
76
+ assert_raises(ElastomerClient::Client::IllegalArgument) do
77
+ @index.native_delete_by_query({}, foo: "bar")
73
78
  end
79
+ end
74
80
 
75
- it "deletes by query when routing is specified" do
76
- index = $client.index "elastomer-delete-by-query-routing-test"
77
- index.delete if index.exists?
78
- type = "docs"
79
- index.create({mappings: { type => { _routing: { required: true } } } })
80
- wait_for_index(@index.name)
81
- docs = index.docs(type)
82
-
83
- docs.index({ :_id => 0, :_routing => "cat", :name => "mittens" })
84
- docs.index({ :_id => 1, :_routing => "cat", :name => "luna" })
85
- docs.index({ :_id => 2, :_routing => "dog", :name => "mittens" })
86
-
87
- query = {
88
- query: {
89
- match: {
90
- name: "mittens"
91
- }
81
+ it "deletes by query when routing is specified" do
82
+ index = $client.index "elastomer-delete-by-query-routing-test"
83
+ index.delete if index.exists?
84
+ type = "docs"
85
+ # default number of shards in ES8 is 1, so set it to 2 shards so routing to different shards can be tested
86
+ settings = $client.version_support.es_version_8_plus? ? { number_of_shards: 2 } : {}
87
+ index.create({
88
+ settings:,
89
+ mappings: mappings_wrapper(type, {
90
+ properties: {
91
+ name: { type: "text", analyzer: "standard" },
92
+ },
93
+ _routing: { required: true }
94
+ })
95
+ })
96
+ wait_for_index(@index.name)
97
+ docs = index.docs(type)
98
+
99
+ docs.index({ _id: 0, _routing: "cat", name: "mittens" })
100
+ docs.index({ _id: 1, _routing: "cat", name: "luna" })
101
+ docs.index({ _id: 2, _routing: "dog", name: "mittens" })
102
+
103
+ query = {
104
+ query: {
105
+ match: {
106
+ name: "mittens"
92
107
  }
93
108
  }
109
+ }
94
110
 
95
- index.refresh
96
- response = index.native_delete_by_query(query, routing: "cat")
111
+ index.refresh
112
+ response = index.native_delete_by_query(query, routing: "cat")
97
113
 
98
- assert_equal(1, response["deleted"])
114
+ assert_equal(1, response["deleted"])
99
115
 
100
- response = docs.multi_get({
101
- :docs => [
102
- { :_id => 0, :_routing => "cat" },
103
- { :_id => 1, :_routing => "cat" },
104
- { :_id => 2, :_routing => "dog" },
105
- ]
106
- })
107
- refute_found response["docs"][0]
108
- assert_found response["docs"][1]
109
- assert_found response["docs"][2]
116
+ response = docs.multi_get({
117
+ docs: [
118
+ { _id: 0, routing: "cat" },
119
+ { _id: 1, routing: "cat" },
120
+ { _id: 2, routing: "dog" },
121
+ ]
122
+ })
110
123
 
111
- index.delete if index.exists?
112
- end
113
- end
114
- else
115
- describe "when native _delete_by_query is not supported" do
116
- it "raises an error" do
117
- assert_raises(Elastomer::Client::IncompatibleVersionException) do
118
- @index.native_delete_by_query({query: {match_all: {}}})
119
- end
120
- end
124
+ refute_found response["docs"][0]
125
+ assert_found response["docs"][1]
126
+ assert_found response["docs"][2]
127
+
128
+ index.delete if index.exists?
121
129
  end
122
130
  end
123
131
  end