elastomer-client 3.2.2 → 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 +76 -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 +6 -4
  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 +165 -143
  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,6 +1,8 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require_relative "../test_helper"
2
4
 
3
- describe Elastomer::Client::Docs do
5
+ describe ElastomerClient::Client::Docs do
4
6
 
5
7
  before do
6
8
  @name = "elastomer-docs-test"
@@ -8,27 +10,18 @@ describe Elastomer::Client::Docs 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", term_vector: "with_positions_offsets"),
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", term_vector: "with_positions_offsets"),
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", term_vector: "with_positions_offsets" },
18
+ author: { type: "keyword" }
26
19
  }
27
- }
20
+ }, true)
28
21
 
29
22
  # COMPATIBILITY
30
- if requires_percolator_mapping?
31
- @index.update_mapping("percolator", { :properties => { :query => { :type => "percolator"}}})
23
+ if !$client.version_support.es_version_8_plus?
24
+ @index.update_mapping("percolator", { properties: { query: { type: "percolator"}}})
32
25
  end
33
26
 
34
27
  wait_for_index(@name)
@@ -42,56 +35,54 @@ describe Elastomer::Client::Docs do
42
35
  end
43
36
 
44
37
  it "raises error when writing same document twice" do
45
- document = {
46
- :_id => "documentid",
47
- :_type => "doc2",
48
- :_op_type => "create",
49
- :title => "the author of logging",
50
- :author => "pea53"
51
- }
38
+ document = ({
39
+ _id: "documentid",
40
+ _type: "book",
41
+ _op_type: "create",
42
+ title: "Book by Author1",
43
+ author: "Author1"
44
+ })
52
45
  h = @docs.index document.dup
53
46
 
54
47
  assert_created h
55
48
 
56
- assert_raises(Elastomer::Client::DocumentAlreadyExistsError) do
49
+ assert_raises(ElastomerClient::Client::DocumentAlreadyExistsError) do
57
50
  @docs.index document.dup
58
51
  end
59
52
  end
60
53
 
61
54
  it "autogenerates IDs for documents" do
62
- h = @docs.index \
63
- :_type => "doc2",
64
- :title => "the author of logging",
65
- :author => "pea53"
66
-
67
- assert_created h
68
- assert_match %r/^\S{20,22}$/, h["_id"]
69
-
70
- h = @docs.index \
71
- :_id => nil,
72
- :_type => "doc3",
73
- :title => "the author of rubber-band",
74
- :author => "grantr"
55
+ h = @docs.index(
56
+ {
57
+ _id: nil,
58
+ title: "Book1 by author 1",
59
+ author: "Author1",
60
+ _type: "book"
61
+ })
75
62
 
76
63
  assert_created h
77
64
  assert_match %r/^\S{20,22}$/, h["_id"]
78
65
 
79
- h = @docs.index \
80
- :_id => "",
81
- :_type => "doc4",
82
- :title => "the author of toml",
83
- :author => "mojombo"
66
+ h = @docs.index(
67
+ {
68
+ _id: nil,
69
+ title: "Book2 by author 2",
70
+ author: "Author2",
71
+ _type: "book"
72
+ })
84
73
 
85
74
  assert_created h
86
75
  assert_match %r/^\S{20,22}$/, h["_id"]
87
76
  end
88
77
 
89
78
  it "uses the provided document ID" do
90
- h = @docs.index \
91
- :_id => "42",
92
- :_type => "doc2",
93
- :title => "the author of logging",
94
- :author => "pea53"
79
+ h = @docs.index (
80
+ {
81
+ _id: 42,
82
+ _type: "book",
83
+ title: "Book1 by author 1",
84
+ author: "Author1"
85
+ })
95
86
 
96
87
  assert_created h
97
88
  assert_equal "42", h["_id"]
@@ -99,56 +90,66 @@ describe Elastomer::Client::Docs do
99
90
 
100
91
  it "accepts JSON encoded document strings" do
101
92
  h = @docs.index \
102
- '{"author":"pea53", "title":"the author of logging"}',
103
- :id => "42",
104
- :type => "doc2"
93
+ '{"author":"Author1", "title":"Book1 by author 1"}',
94
+ id: 42,
95
+ type: "book"
105
96
 
106
97
  assert_created h
107
98
  assert_equal "42", h["_id"]
108
-
109
- h = @docs.index \
110
- '{"author":"grantr", "title":"the author of rubber-band"}',
111
- :type => "doc2"
112
-
113
- assert_created h
114
- assert_match %r/^\S{20,22}$/, h["_id"]
115
99
  end
116
100
 
117
101
  describe "indexing directive fields" do
102
+ before do
103
+ # Since we set dynamic: strict, adding the above doc to the index throws an error, so update the index to allow dynamic mapping
104
+ if !$client.version_support.es_version_8_plus?
105
+ @index.update_mapping "book", { book: { dynamic: "true" } }
106
+ end
107
+ end
108
+
109
+ after do
110
+ # Since we set dynamic: strict, adding the above doc to the index throws an error, so update the index to allow dynamic mapping
111
+ if !$client.version_support.es_version_8_plus?
112
+ @index.update_mapping "book", { book: { dynamic: "strict" } }
113
+ end
114
+ end
115
+
118
116
  it "indexes fields that are not recognized as indexing directives" do
119
- doc = {
117
+ doc = ({
120
118
  _id: "12",
121
- _type: "doc2",
122
- title: "The Adventures of Huckleberry Finn",
123
- author: "Mark Twain",
119
+ _type: "book",
120
+ title: "Book1",
121
+ author: "Author1",
124
122
  _unknown_1: "unknown attribute 1",
125
- "_unknown_2" => "unknown attribute 2"
126
- }
123
+ "_unknown_2": "unknown attribute 2"
124
+ })
127
125
 
128
126
  h = @docs.index(doc)
127
+
129
128
  assert_created h
130
129
  assert_equal "12", h["_id"]
131
130
 
132
- indexed_doc = @docs.get(type: "doc2", id: "12")
131
+ indexed_doc = $client.version_support.es_version_8_plus? ? @docs.get(id: "12") : @docs.get(type: "book", id: "12")
133
132
  expected = {
134
- "title" => "The Adventures of Huckleberry Finn",
135
- "author" => "Mark Twain",
133
+ "title" => "Book1",
134
+ "author" => "Author1",
136
135
  "_unknown_1" => "unknown attribute 1",
137
136
  "_unknown_2" => "unknown attribute 2"
138
137
  }
138
+
139
139
  assert_equal expected, indexed_doc["_source"]
140
140
  end
141
141
 
142
142
  it "extracts indexing directives from the document" do
143
143
  doc = {
144
144
  _id: "12",
145
- "_type" => "doc2",
145
+ _type: "book",
146
146
  _routing: "author",
147
- title: "The Adventures of Huckleberry Finn",
148
- author: "Mark Twain"
147
+ title: "Book1",
148
+ author: "Author1"
149
149
  }
150
150
 
151
151
  h = @docs.index(doc)
152
+
152
153
  assert_created h
153
154
  assert_equal "12", h["_id"]
154
155
 
@@ -157,78 +158,87 @@ describe Elastomer::Client::Docs do
157
158
  refute doc.key?("_type")
158
159
  refute doc.key?(:_routing)
159
160
 
160
- indexed_doc = @docs.get(type: "doc2", id: "12")
161
+ indexed_doc = $client.version_support.es_version_8_plus? ? @docs.get(id: "12") : @docs.get(type: "book", id: "12")
161
162
  expected = {
162
- "title" => "The Adventures of Huckleberry Finn",
163
- "author" => "Mark Twain",
163
+ "title" => "Book1",
164
+ "author" => "Author1"
164
165
  }
166
+
165
167
  assert_equal expected, indexed_doc["_source"]
166
168
  end
167
169
 
168
- # COMPATIBILITY: Fail fast on known indexing directives that aren't for this version of ES
169
170
  it "raises an exception when a known indexing directive from an unsupported version is used" do
170
171
  # Symbol keys
171
- doc = {
172
+ doc = ({
172
173
  _id: "12",
173
- _type: "doc2",
174
- title: "The Adventures of Huckleberry Finn"
175
- }.merge(incompatible_indexing_directive)
174
+ _type: "book",
175
+ title: "Book1",
176
+ _consistency: "all"
177
+ })
176
178
 
177
- assert_raises(Elastomer::Client::IllegalArgument) do
179
+ assert_raises(ElastomerClient::Client::IllegalArgument) do
178
180
  @docs.index(doc)
179
181
  end
180
182
 
181
183
  # String keys
182
- doc = {
184
+ doc = ({
183
185
  "_id" => "12",
184
- "_type" => "doc2",
185
- "title" => "The Adventures of Huckleberry Finn"
186
- }.merge(incompatible_indexing_directive.stringify_keys)
186
+ "_type" => "book",
187
+ "title" => "Book1",
188
+ "_consistency" => "all"
189
+ })
187
190
 
188
- assert_raises(Elastomer::Client::IllegalArgument) do
191
+ assert_raises(ElastomerClient::Client::IllegalArgument) do
189
192
  @docs.index(doc)
190
193
  end
191
194
  end
192
195
  end
193
196
 
194
197
  it "gets documents from the search index" do
195
- h = @docs.get :id => "1", :type => "doc1"
198
+ h = $client.version_support.es_version_8_plus? ? @docs.get(id: "1") : @docs.get(id: "1", type: "book")
199
+
196
200
  refute_found h
197
201
 
198
202
  populate!
199
203
 
200
- h = @docs.get :id => "1", :type => "doc1"
204
+ h = $client.version_support.es_version_8_plus? ? @docs.get(id: "1") : @docs.get(id: "1", type: "book")
205
+
201
206
  assert_found h
202
- assert_equal "mojombo", h["_source"]["author"]
207
+ assert_equal "Author1", h["_source"]["author"]
203
208
  end
204
209
 
205
210
  it "checks if documents exist in the search index" do
206
- refute @docs.exists?(:id => "1", :type => "doc1")
211
+ refute $client.version_support.es_version_8_plus? ? @docs.exists?(id: "1") : @docs.exists?(id: "1", type: "book")
207
212
  populate!
208
- assert @docs.exists?(:id => "1", :type => "doc1")
213
+
214
+ assert $client.version_support.es_version_8_plus? ? @docs.exists?(id: "1") : @docs.exists?(id: "1", type: "book")
209
215
  end
210
216
 
211
217
  it "checks if documents exist in the search index with .exist?" do
212
- refute @docs.exist?(:id => "1", :type => "doc1")
218
+ refute $client.version_support.es_version_8_plus? ? @docs.exist?(id: "1") : @docs.exist?(id: "1", type: "book")
213
219
  populate!
214
- assert @docs.exist?(:id => "1", :type => "doc1")
220
+
221
+ assert $client.version_support.es_version_8_plus? ? @docs.exist?(id: "1") : @docs.exist?(id: "1", type: "book")
215
222
  end
216
223
 
217
224
  it "gets multiple documents from the search index" do
218
225
  populate!
219
226
 
220
- h = @docs.multi_get :docs => [
221
- { :_id => 1, :_type => "doc1" },
222
- { :_id => 1, :_type => "doc2" }
227
+ h = @docs.multi_get docs: [
228
+ document_wrapper("book", { _id: 1 }),
229
+ document_wrapper("book", { _id: 2 })
223
230
  ]
224
231
  authors = h["docs"].map { |d| d["_source"]["author"] }
225
- assert_equal %w[mojombo pea53], authors
226
232
 
227
- h = @docs.multi_get({:ids => [2, 1]}, :type => "doc1")
233
+ assert_equal %w[Author1 Author2], authors
234
+
235
+ h = $client.version_support.es_version_8_plus? ? @docs.multi_get({ids: [2, 1]}) : @docs.multi_get({ids: [2, 1]}, type: "book")
228
236
  authors = h["docs"].map { |d| d["_source"]["author"] }
229
- assert_equal %w[defunkt mojombo], authors
230
237
 
231
- h = @index.docs("doc1").multi_get :ids => [1, 2, 3, 4]
238
+ assert_equal %w[Author2 Author1], authors
239
+
240
+ h = @index.docs("book").multi_get ids: [1, 2, 3, 4]
241
+
232
242
  assert_found h["docs"][0]
233
243
  assert_found h["docs"][1]
234
244
  refute_found h["docs"][2]
@@ -238,18 +248,21 @@ describe Elastomer::Client::Docs do
238
248
  it "gets multiple documents from the search index with .mget" do
239
249
  populate!
240
250
 
241
- h = @docs.mget :docs => [
242
- { :_id => 1, :_type => "doc1" },
243
- { :_id => 1, :_type => "doc2" }
251
+ h = @docs.mget docs: [
252
+ document_wrapper("book", { _id: 1 }),
253
+ document_wrapper("book", { _id: 2 })
244
254
  ]
245
255
  authors = h["docs"].map { |d| d["_source"]["author"] }
246
- assert_equal %w[mojombo pea53], authors
247
256
 
248
- h = @docs.mget({:ids => [2, 1]}, :type => "doc1")
257
+ assert_equal %w[Author1 Author2], authors
258
+
259
+ h = @docs.mget({ids: [2, 1]})
249
260
  authors = h["docs"].map { |d| d["_source"]["author"] }
250
- assert_equal %w[defunkt mojombo], authors
251
261
 
252
- h = @index.docs("doc1").mget :ids => [1, 2, 3, 4]
262
+ assert_equal %w[Author2 Author1], authors
263
+
264
+ h = @index.docs("book").mget ids: [1, 2, 3, 4]
265
+
253
266
  assert_found h["docs"][0]
254
267
  assert_found h["docs"][1]
255
268
  refute_found h["docs"][2]
@@ -258,184 +271,246 @@ describe Elastomer::Client::Docs do
258
271
 
259
272
  it "deletes documents from the search index" do
260
273
  populate!
261
- @docs = @index.docs("doc2")
274
+ @docs = @index.docs("book")
262
275
 
263
- h = @docs.multi_get :ids => [1, 2]
276
+ h = @docs.multi_get ids: [1, 2]
264
277
  authors = h["docs"].map { |d| d["_source"]["author"] }
265
- assert_equal %w[pea53 grantr], authors
266
278
 
267
- h = @docs.delete :id => 1
268
- assert h["found"], "expected document to be found"
269
- h = @docs.multi_get :ids => [1, 2]
279
+ assert_equal %w[Author1 Author2], authors
280
+
281
+ h = @docs.delete id: 1
282
+
283
+ if $client.version_support.es_version_8_plus?
284
+ assert_equal "deleted", h["result"], "expected document to be found"
285
+ else
286
+ assert h["found"], "expected document to be found"
287
+ end
288
+
289
+ h = @docs.multi_get ids: [1, 2]
290
+
270
291
  refute_found h["docs"][0]
271
292
  assert_found h["docs"][1]
272
293
 
273
- assert_raises(ArgumentError) { @docs.delete :id => nil }
274
- assert_raises(ArgumentError) { @docs.delete :id => "" }
275
- assert_raises(ArgumentError) { @docs.delete :id => "\t" }
294
+ assert_raises(ArgumentError) { @docs.delete id: nil }
295
+ assert_raises(ArgumentError) { @docs.delete id: "" }
296
+ assert_raises(ArgumentError) { @docs.delete id: "\t" }
276
297
  end
277
298
 
278
299
  it "does not care if you delete a document that is not there" do
279
- @docs = @index.docs("doc2")
280
- h = @docs.delete :id => 42
300
+ @docs = @index.docs("book")
301
+ h = @docs.delete id: 42
281
302
 
282
- refute h["found"], "expected document to not be found"
303
+ if $client.version_support.es_version_8_plus?
304
+ refute_equal "deleted", h["result"], "expected document to not be found"
305
+ else
306
+ refute h["found"], "expected document to not be found"
307
+ end
283
308
  end
284
309
 
285
310
  it "deletes documents by query" do
286
311
  populate!
287
- @docs = @index.docs("doc2")
312
+ @docs = @index.docs("book")
288
313
 
289
- h = @docs.multi_get :ids => [1, 2]
314
+ h = @docs.multi_get ids: [1, 2]
290
315
  authors = h["docs"].map { |d| d["_source"]["author"] }
291
- assert_equal %w[pea53 grantr], authors
292
316
 
293
- h = @docs.delete_by_query(:q => "author:grantr")
317
+ assert_equal %w[Author1 Author2], authors
294
318
 
295
- if supports_native_delete_by_query?
296
- assert_equal(1, h["deleted"])
297
- else
298
- assert_equal(h["_indices"], {
299
- "_all" => {
300
- "found" => 1,
301
- "deleted" => 1,
302
- "missing" => 0,
303
- "failed" => 0,
304
- },
305
- @name => {
306
- "found" => 1,
307
- "deleted" => 1,
308
- "missing" => 0,
309
- "failed" => 0,
310
- },
311
- })
312
- end
319
+ h = @docs.delete_by_query(q: "author:Author2")
320
+
321
+ assert_equal(1, h["deleted"])
313
322
 
314
323
  @index.refresh
315
- h = @docs.multi_get :ids => [1, 2]
324
+ h = @docs.multi_get ids: [1, 2]
325
+
316
326
  assert_found h["docs"][0]
317
327
  refute_found h["docs"][1]
318
328
 
319
329
  h = @docs.delete_by_query(
320
- :query => {
321
- :bool => {
322
- :filter => {:term => {:author => "pea53"}}
330
+ query: {
331
+ bool: {
332
+ filter: {term: {author: "Author1"}}
323
333
  }
324
334
  }
325
335
  )
326
336
  @index.refresh
327
- h = @docs.multi_get :ids => [1, 2]
337
+ h = @docs.multi_get ids: [1, 2]
338
+
328
339
  refute_found h["docs"][0]
329
340
  refute_found h["docs"][1]
330
341
  end
331
342
 
343
+ it "updates documents by query" do
344
+ populate!
345
+
346
+ r = @docs.update_by_query(query: {
347
+ bool: {
348
+ filter: {term: {author: "Author1"}}
349
+ }
350
+ }, script: {
351
+ source: "ctx._source.author = 'Author1 Updated'"
352
+ })
353
+
354
+ assert_equal 1, r["updated"]
355
+
356
+ r = @docs.update_by_query({
357
+ query: {
358
+ bool: {
359
+ filter: {term: {author: "Author2"}}
360
+ }
361
+ },
362
+ script: {
363
+ source: "ctx._source.author = 'Author2 Updated'"
364
+ }
365
+ }, conflicts: "proceed")
366
+
367
+ assert_equal 1, r["updated"]
368
+
369
+ @index.refresh
370
+
371
+ h = @docs.multi_get ids: [1, 2]
372
+
373
+ assert_equal "Author1 Updated", h["docs"][0]["_source"]["author"]
374
+ assert_equal "Author2 Updated", h["docs"][1]["_source"]["author"]
375
+
376
+ end
377
+
332
378
  it "searches for documents" do
333
- h = @docs.search :q => "*:*"
334
- assert_equal 0, h["hits"]["total"]
379
+ h = @docs.search q: "*:*"
380
+
381
+ if $client.version_support.es_version_8_plus?
382
+ assert_equal 0, h["hits"]["total"]["value"]
383
+ else
384
+ assert_equal 0, h["hits"]["total"]
385
+ end
335
386
 
336
387
  populate!
337
388
 
338
- h = @docs.search :q => "*:*"
339
- assert_equal 4, h["hits"]["total"]
389
+ h = @docs.search q: "*:*"
340
390
 
341
- h = @docs.search :q => "*:*", :type => "doc1"
342
- assert_equal 2, h["hits"]["total"]
391
+ if $client.version_support.es_version_8_plus?
392
+ assert_equal 2, h["hits"]["total"]["value"]
393
+ else
394
+ assert_equal 2, h["hits"]["total"]
395
+ end
396
+
397
+ if !$client.version_support.es_version_8_plus?
398
+ h = @docs.search q: "*:*", type: "book"
399
+
400
+ assert_equal 2, h["hits"]["total"]
401
+ end
343
402
 
344
403
  h = @docs.search({
345
- :query => {:match_all => {}},
346
- :post_filter => {:term => {:author => "defunkt"}}
347
- }, :type => %w[doc1 doc2] )
348
- assert_equal 1, h["hits"]["total"]
404
+ query: {match_all: {}},
405
+ post_filter: {term: {author: "Author1"}}
406
+ })
407
+
408
+ if $client.version_support.es_version_8_plus?
409
+ assert_equal 1, h["hits"]["total"]["value"]
410
+ else
411
+ assert_equal 1, h["hits"]["total"]
412
+ end
349
413
 
350
414
  hit = h["hits"]["hits"].first
351
- assert_equal "the author of resque", hit["_source"]["title"]
415
+
416
+ assert_equal "Book1 by author 1", hit["_source"]["title"]
352
417
  end
353
418
 
354
419
  it "supports the shards search API" do
355
- h = @docs.search_shards(:type => "docs1")
420
+ h = @docs.search_shards(params={})
356
421
 
357
422
  assert h.key?("nodes"), "response contains \"nodes\" information"
358
423
  assert h.key?("shards"), "response contains \"shards\" information"
359
- assert h["shards"].is_a?(Array), "\"shards\" is an array"
424
+ assert_kind_of Array, h["shards"], "\"shards\" is an array"
360
425
  end
361
426
 
362
427
  it "generates QueryParsingError exceptions on bad input when searching" do
363
- query = {:query => {:query_string => {:query => "OR should fail"}}}
364
- assert_raises(Elastomer::Client::QueryParsingError) { @docs.search(query, :type => %w[doc1 doc2]) }
428
+ query = {query: {query_string: {query: "OR should fail"}}}
429
+ assert_raises(ElastomerClient::Client::QueryParsingError) { @docs.search(query) }
365
430
 
366
- query = {:query => {:foo_is_not_valid => {}}}
367
- assert_raises(Elastomer::Client::QueryParsingError) { @docs.search(query, :type => %w[doc1 doc2]) }
431
+ query = {query: {foo_is_not_valid: {}}}
432
+ assert_raises(ElastomerClient::Client::QueryParsingError) { @docs.search(query) }
368
433
  end
369
434
 
370
435
  it "counts documents" do
371
- h = @docs.count :q => "*:*"
436
+ h = @docs.count q: "*:*"
437
+
372
438
  assert_equal 0, h["count"]
373
439
 
374
440
  populate!
375
441
 
376
- h = @docs.count :q => "*:*"
377
- assert_equal 4, h["count"]
442
+ h = @docs.count q: "*:*"
378
443
 
379
- h = @docs.count :q => "*:*", :type => "doc1"
380
444
  assert_equal 2, h["count"]
381
445
 
382
- h = @docs.count :q => "*:*", :type => "doc1,doc2"
383
- assert_equal 4, h["count"]
446
+ if !$client.version_support.es_version_8_plus?
447
+ h = @docs.count(q: "*:*", type: "book")
448
+
449
+ assert_equal 2, h["count"]
450
+ end
384
451
 
385
452
  h = @docs.count({
386
- :query => {
387
- :bool => {
388
- :filter => {:term => {:author => "defunkt"}}
453
+ query: {
454
+ bool: {
455
+ filter: {term: {author: "Author1"}}
389
456
  }
390
457
  }
391
- }, :type => %w[doc1 doc2] )
458
+ })
459
+
392
460
  assert_equal 1, h["count"]
393
461
  end
394
462
 
395
463
  it "explains scoring" do
396
464
  populate!
397
465
 
398
- h = @docs.explain({
399
- :query => {
400
- :match => {
401
- "author" => "defunkt"
466
+ h = $client.version_support.es_version_8_plus? ?
467
+ @docs.explain({
468
+ query: {
469
+ match: {
470
+ "author" => "Author1"
471
+ }
402
472
  }
403
- }
404
- }, :type => "doc1", :id => 2)
405
- assert_equal true, h["matched"]
473
+ }, id: 1)
474
+ : @docs.explain({
475
+ query: {
476
+ match: {
477
+ "author" => "Author1"
478
+ }
479
+ }
480
+ }, id: 1, type: "book")
406
481
 
407
- h = @docs.explain(:type => "doc2", :id => 2, :q => "pea53")
408
- assert_equal false, h["matched"]
482
+ assert h["matched"]
483
+
484
+ h = $client.version_support.es_version_8_plus? ? @docs.explain(id: 2, q: "Author1") : @docs.explain(type: "book", id: 2, q: "Author1")
485
+
486
+ refute h["matched"]
409
487
  end
410
488
 
411
489
  it "validates queries" do
412
490
  populate!
413
491
 
414
- h = @docs.validate :q => "*:*"
415
- assert_equal true, h["valid"]
492
+ h = @docs.validate q: "*:*"
493
+
494
+ assert h["valid"]
416
495
 
417
496
  h = @docs.validate({
418
- :query => {
419
- :filtered => {
420
- :query => {:match_all => {}},
421
- :filter => {:term => {:author => "defunkt"}}
497
+ query: {
498
+ filtered: {
499
+ query: {match_all: {}},
500
+ filter: {term: {author: "Author2"}}
422
501
  }
423
502
  }
424
- }, :type => %w[doc1 doc2])
503
+ })
425
504
 
426
- if filtered_query_removed?
427
- refute h["valid"]
428
- else
429
- assert h["valid"]
430
- end
505
+ refute h["valid"]
431
506
 
432
507
  h = @docs.validate({
433
- :query => {
434
- :bool => {
435
- :filter => {:term => {:author => "defunkt"}}
508
+ query: {
509
+ bool: {
510
+ filter: {term: {author: "Author2"}}
436
511
  }
437
512
  }
438
- }, :type => %w[doc1 doc2])
513
+ })
439
514
 
440
515
  assert h["valid"]
441
516
  end
@@ -443,97 +518,99 @@ describe Elastomer::Client::Docs do
443
518
  it "updates documents" do
444
519
  populate!
445
520
 
446
- h = @docs.get :id => "1", :type => "doc1"
521
+ h = $client.version_support.es_version_8_plus? ? @docs.get(id: "1") : @docs.get(id: "1", type: "book")
522
+
447
523
  assert_found h
448
- assert_equal "mojombo", h["_source"]["author"]
524
+ assert_equal "Author1", h["_source"]["author"]
525
+
526
+ @docs.update(document_wrapper("book", {
527
+ _id: "1",
528
+ doc: {author: "Author1.1"}
529
+ }))
530
+ h = $client.version_support.es_version_8_plus? ? @docs.get(id: "1") : @docs.get(id: "1", type: "book")
449
531
 
450
- @docs.update({
451
- :_id => "1",
452
- :_type => "doc1",
453
- :doc => {:author => "TwP"}
454
- })
455
- h = @docs.get :id => "1", :type => "doc1"
456
532
  assert_found h
457
- assert_equal "TwP", h["_source"]["author"]
533
+ assert_equal "Author1.1", h["_source"]["author"]
458
534
 
459
535
  if $client.version >= "0.90"
460
- @docs.update({
461
- :_id => "42",
462
- :_type => "doc1",
463
- :doc => {
464
- :author => "TwP",
465
- :title => "the ineffable beauty of search"
536
+ @docs.update(document_wrapper("book", {
537
+ _id: "42",
538
+ doc: {
539
+ author: "Author42",
540
+ title: "Book42"
466
541
  },
467
- :doc_as_upsert => true
468
- })
542
+ doc_as_upsert: true
543
+ }))
544
+
545
+ h = $client.version_support.es_version_8_plus? ? @docs.get(id: "42") : @docs.get(id: "42", type: "book")
469
546
 
470
- h = @docs.get :id => "42", :type => "doc1"
471
547
  assert_found h
472
- assert_equal "TwP", h["_source"]["author"]
473
- assert_equal "the ineffable beauty of search", h["_source"]["title"]
548
+ assert_equal "Author42", h["_source"]["author"]
549
+ assert_equal "Book42", h["_source"]["title"]
474
550
  end
475
551
  end
476
552
 
477
553
  it "supports bulk operations with the same parameters as docs" do
478
554
  response = @docs.bulk do |b|
479
- populate!(b)
555
+ populate_with_params!(b)
480
556
  end
481
557
 
482
558
  assert_kind_of Integer, response["took"]
483
559
 
484
- response = @docs.get(:id => 1, :type => "doc1")
560
+ response = $client.version_support.es_version_8_plus? ? @docs.get(id: 1) : @docs.get(id: 1, type: "book")
561
+
485
562
  assert_found response
486
- assert_equal "mojombo", response["_source"]["author"]
563
+ assert_equal "Author1", response["_source"]["author"]
487
564
  end
488
565
 
489
566
  it "provides access to term vector statistics" do
490
567
  populate!
491
568
 
492
- response = @docs.termvector :type => "doc2", :id => 1, :fields => "title"
569
+ response = $client.version_support.es_version_8_plus? ? @docs.termvector(id: 1, fields: "title") : @docs.termvector(type: "book", id: 1, fields: "title")
493
570
 
494
571
  assert response["term_vectors"]["title"]
495
572
  assert response["term_vectors"]["title"]["field_statistics"]
496
573
  assert response["term_vectors"]["title"]["terms"]
497
- assert_equal %w[author logging of the], response["term_vectors"]["title"]["terms"].keys
574
+ assert_equal %w[1 author book1 by], response["term_vectors"]["title"]["terms"].keys
498
575
  end
499
576
 
500
577
  it "provides access to term vector statistics with .termvectors" do
501
578
  populate!
502
579
 
503
- response = @docs.termvectors :type => "doc2", :id => 1, :fields => "title"
580
+ response = $client.version_support.es_version_8_plus? ? @docs.termvectors(id: 1, fields: "title") : @docs.termvectors(type: "book", id: 1, fields: "title")
504
581
 
505
582
  assert response["term_vectors"]["title"]
506
583
  assert response["term_vectors"]["title"]["field_statistics"]
507
584
  assert response["term_vectors"]["title"]["terms"]
508
- assert_equal %w[author logging of the], response["term_vectors"]["title"]["terms"].keys
585
+ assert_equal %w[1 author book1 by], response["term_vectors"]["title"]["terms"].keys
509
586
  end
510
587
 
511
588
  it "provides access to term vector statistics with .term_vector" do
512
589
  populate!
513
590
 
514
- response = @docs.term_vector :type => "doc2", :id => 1, :fields => "title"
591
+ response = $client.version_support.es_version_8_plus? ? @docs.term_vector(id: 1, fields: "title") : @docs.term_vector(type: "book", id: 1, fields: "title")
515
592
 
516
593
  assert response["term_vectors"]["title"]
517
594
  assert response["term_vectors"]["title"]["field_statistics"]
518
595
  assert response["term_vectors"]["title"]["terms"]
519
- assert_equal %w[author logging of the], response["term_vectors"]["title"]["terms"].keys
596
+ assert_equal %w[1 author book1 by], response["term_vectors"]["title"]["terms"].keys
520
597
  end
521
598
 
522
599
  it "provides access to term vector statistics with .term_vectors" do
523
600
  populate!
524
601
 
525
- response = @docs.term_vectors :type => "doc2", :id => 1, :fields => "title"
602
+ response = $client.version_support.es_version_8_plus? ? @docs.term_vectors(id: 1, fields: "title") : @docs.term_vectors(type: "book", id: 1, fields: "title")
526
603
 
527
604
  assert response["term_vectors"]["title"]
528
605
  assert response["term_vectors"]["title"]["field_statistics"]
529
606
  assert response["term_vectors"]["title"]["terms"]
530
- assert_equal %w[author logging of the], response["term_vectors"]["title"]["terms"].keys
607
+ assert_equal %w[1 author book1 by], response["term_vectors"]["title"]["terms"].keys
531
608
  end
532
609
 
533
610
  it "provides access to multi term vector statistics" do
534
611
  populate!
535
612
 
536
- response = @docs.multi_termvectors({:ids => [1, 2]}, :type => "doc2", :fields => "title", :term_statistics => true)
613
+ response = $client.version_support.es_version_8_plus? ? @docs.multi_termvectors({ids: [1, 2]}, fields: "title", term_statistics: true) : @docs.multi_termvectors({ids: [1, 2]}, type: "book", fields: "title", term_statistics: true)
537
614
  docs = response["docs"]
538
615
 
539
616
  assert docs
@@ -543,7 +620,7 @@ describe Elastomer::Client::Docs do
543
620
  it "provides access to multi term vector statistics with .multi_term_vectors" do
544
621
  populate!
545
622
 
546
- response = @docs.multi_term_vectors({:ids => [1, 2]}, :type => "doc2", :fields => "title", :term_statistics => true)
623
+ response = $client.version_support.es_version_8_plus? ? @docs.multi_term_vectors({ids: [1, 2]}, fields: "title", term_statistics: true) : @docs.multi_term_vectors({ids: [1, 2]}, type: "book", fields: "title", term_statistics: true)
547
624
  docs = response["docs"]
548
625
 
549
626
  assert docs
@@ -551,79 +628,112 @@ describe Elastomer::Client::Docs do
551
628
  end
552
629
 
553
630
  it "percolates a given document" do
631
+ if $client.version_support.es_version_8_plus?
632
+ skip "Percolate not supported in ES version #{$client.version}"
633
+ end
634
+
554
635
  populate!
555
636
 
556
637
  percolator1 = @index.percolator "1"
557
- response = percolator1.create :query => { :match => { :author => "pea53" } }
638
+ response = percolator1.create query: { match: { author: "Author1" } }
639
+
558
640
  assert response["created"], "Couldn't create the percolator query"
559
641
  percolator2 = @index.percolator "2"
560
- response = percolator2.create :query => { :match => { :author => "defunkt" } }
642
+ response = percolator2.create query: { match: { author: "Author2" } }
643
+
561
644
  assert response["created"], "Couldn't create the percolator query"
562
645
  @index.refresh
563
646
 
564
- response = @index.docs("doc1").percolate(:doc => { :author => "pea53" })
647
+ response = @index.docs("book").percolate(doc: { author: "Author1" })
648
+
565
649
  assert_equal 1, response["matches"].length
566
650
  assert_equal "1", response["matches"][0]["_id"]
567
651
  end
568
652
 
569
653
  it "percolates an existing document" do
654
+ if $client.version_support.es_version_8_plus?
655
+ skip "Percolate not supported in ES version #{$client.version}"
656
+ end
657
+
570
658
  populate!
571
659
 
572
660
  percolator1 = @index.percolator "1"
573
- response = percolator1.create :query => { :match => { :author => "pea53" } }
661
+ response = percolator1.create query: { match: { author: "Author1" } }
662
+
574
663
  assert response["created"], "Couldn't create the percolator query"
575
664
  percolator2 = @index.percolator "2"
576
- response = percolator2.create :query => { :match => { :author => "defunkt" } }
665
+ response = percolator2.create query: { match: { author: "Author2" } }
666
+
577
667
  assert response["created"], "Couldn't create the percolator query"
578
668
  @index.refresh
579
669
 
580
- response = @index.docs("doc2").percolate(nil, :id => "1")
670
+ response = @index.docs("book").percolate(nil, id: "1")
671
+
581
672
  assert_equal 1, response["matches"].length
582
673
  assert_equal "1", response["matches"][0]["_id"]
583
674
  end
584
675
 
585
676
  it "counts the matches for percolating a given document" do
677
+ if $client.version_support.es_version_8_plus?
678
+ skip "Percolate not supported in ES version #{$client.version}"
679
+ end
680
+
586
681
  populate!
587
682
 
588
683
  percolator1 = @index.percolator "1"
589
- response = percolator1.create :query => { :match => { :author => "pea53" } }
684
+ response = percolator1.create query: { match: { author: "Author1" } }
685
+
590
686
  assert response["created"], "Couldn't create the percolator query"
591
687
  percolator2 = @index.percolator "2"
592
- response = percolator2.create :query => { :match => { :author => "defunkt" } }
688
+ response = percolator2.create query: { match: { author: "Author2" } }
689
+
593
690
  assert response["created"], "Couldn't create the percolator query"
594
691
  @index.refresh
595
692
 
596
- count = @index.docs("doc1").percolate_count :doc => { :author => "pea53" }
693
+ count = @index.docs("book").percolate_count doc: { author: "Author1" }
694
+
597
695
  assert_equal 1, count
598
696
  end
599
697
 
600
698
  it "counts the matches for percolating an existing document" do
699
+ if $client.version_support.es_version_8_plus?
700
+ skip "Percolate not supported in ES version #{$client.version}"
701
+ end
702
+
601
703
  populate!
602
704
 
603
705
  percolator1 = @index.percolator "1"
604
- response = percolator1.create :query => { :match => { :author => "pea53" } }
706
+ response = percolator1.create query: { match: { author: "Author1" } }
707
+
605
708
  assert response["created"], "Couldn't create the percolator query"
606
709
  percolator2 = @index.percolator "2"
607
- response = percolator2.create :query => { :match => { :author => "defunkt" } }
710
+ response = percolator2.create query: { match: { author: "Author2" } }
711
+
608
712
  assert response["created"], "Couldn't create the percolator query"
609
713
  @index.refresh
610
714
 
611
- count = @index.docs("doc2").percolate_count(nil, :id => "1")
715
+ count = @index.docs("book").percolate_count(nil, id: "1")
716
+
612
717
  assert_equal 1, count
613
718
  end
614
719
 
615
720
  it "performs multi percolate queries" do
616
- @index.percolator("1").create :query => { :match_all => { } }
617
- @index.percolator("2").create :query => { :match => { :author => "pea53" } }
721
+ if $client.version_support.es_version_8_plus?
722
+ skip "Multi percolate not supported in ES version #{$client.version}"
723
+ end
724
+
725
+ @index.percolator("1").create query: { match_all: { } }
726
+ @index.percolator("2").create query: { match: { author: "Author1" } }
618
727
  @index.refresh
619
728
 
620
- h = @index.docs("doc2").multi_percolate do |m|
621
- m.percolate :author => "pea53"
622
- m.percolate :author => "grantr"
623
- m.count({}, { :author => "grantr" })
729
+ h = @index.docs("book").multi_percolate do |m|
730
+ m.percolate author: "Author1"
731
+ m.percolate author: "Author2"
732
+ m.count({}, { author: "Author2" })
624
733
  end
625
734
 
626
735
  response1, response2, response3 = h["responses"]
736
+
627
737
  assert_equal ["1", "2"], response1["matches"].map { |match| match["_id"] }.sort
628
738
  assert_equal ["1"], response2["matches"].map { |match| match["_id"] }.sort
629
739
  assert_equal 1, response3["total"]
@@ -631,33 +741,36 @@ describe Elastomer::Client::Docs do
631
741
 
632
742
  # Create/index multiple documents.
633
743
  #
634
- # docs - An instance of Elastomer::Client::Docs or Elastomer::Client::Bulk. If
744
+ # docs - An instance of ElastomerClient::Client::Docs or ElastomerClient::Client::Bulk. If
635
745
  # nil uses the @docs instance variable.
636
- # rubocop:disable Metrics/MethodLength
637
746
  def populate!(docs = @docs)
638
- docs.index \
639
- :_id => 1,
640
- :_type => "doc1",
641
- :title => "the author of gravatar",
642
- :author => "mojombo"
643
-
644
- docs.index \
645
- :_id => 2,
646
- :_type => "doc1",
647
- :title => "the author of resque",
648
- :author => "defunkt"
649
-
650
- docs.index \
651
- :_id => 1,
652
- :_type => "doc2",
653
- :title => "the author of logging",
654
- :author => "pea53"
655
-
656
- docs.index \
657
- :_id => 2,
658
- :_type => "doc2",
659
- :title => "the author of rubber-band",
660
- :author => "grantr"
747
+ docs.index ({
748
+ _id: 1,
749
+ _type: "book",
750
+ title: "Book1 by author 1",
751
+ author: "Author1"
752
+ })
753
+
754
+ docs.index ({
755
+ _id: 2,
756
+ _type: "book",
757
+ title: "Book2 by author 2",
758
+ author: "Author2"
759
+ })
760
+
761
+ @index.refresh
762
+ end
763
+
764
+ def populate_with_params!(docs = @docs)
765
+ docs.index({
766
+ title: "Book1 by author 1",
767
+ author: "Author1"
768
+ }, { _id: 1, _type: "book" })
769
+
770
+ docs.index({
771
+ title: "Book2 by author 2",
772
+ author: "Author2"
773
+ }, { _id: 2, _type: "book" })
661
774
 
662
775
  @index.refresh
663
776
  end