elastomer-client 3.2.3 → 6.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
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 +34 -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 +100 -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,9 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require_relative "../test_helper"
4
+ require "json"
2
5
 
3
- describe Elastomer::Client::Bulk do
6
+ describe ElastomerClient::Client::Bulk do
4
7
 
5
8
  before do
6
9
  @name = "elastomer-bulk-test"
@@ -8,23 +11,14 @@ describe Elastomer::Client::Bulk do
8
11
 
9
12
  unless @index.exists?
10
13
  @index.create \
11
- :settings => { "index.number_of_shards" => 1, "index.number_of_replicas" => 0 },
12
- :mappings => {
13
- :tweet => {
14
- :_source => { :enabled => true }, :_all => { :enabled => false },
15
- :properties => {
16
- :message => $client.version_support.text(analyzer: "standard"),
17
- :author => $client.version_support.keyword
18
- }
19
- },
20
- :book => {
21
- :_source => { :enabled => true }, :_all => { :enabled => false },
22
- :properties => {
23
- :title => $client.version_support.text(analyzer: "standard"),
24
- :author => $client.version_support.keyword
25
- }
14
+ settings: { "index.number_of_shards" => 1, "index.number_of_replicas" => 0 },
15
+ mappings: mappings_wrapper("book", {
16
+ _source: { enabled: true },
17
+ properties: {
18
+ title: { type: "text", analyzer: "standard" },
19
+ author: { type: "keyword" }
26
20
  }
27
- }
21
+ })
28
22
 
29
23
  wait_for_index(@name)
30
24
  end
@@ -36,10 +30,10 @@ describe Elastomer::Client::Bulk do
36
30
 
37
31
  it "performs bulk index actions" do
38
32
  body = [
39
- '{"index" : {"_id":"1", "_type":"tweet", "_index":"elastomer-bulk-test"}}',
40
- '{"author":"pea53", "message":"just a test tweet"}',
41
- '{"index" : {"_id":"1", "_type":"book", "_index":"elastomer-bulk-test"}}',
42
- '{"author":"John Scalzi", "title":"Old Mans War"}',
33
+ {index: document_wrapper("book", {_id: "1", _index: "elastomer-bulk-test"})}.to_json,
34
+ '{"author":"Author 1", "title":"Book 1"}',
35
+ {index: document_wrapper("book", {_id: "2", _index: "elastomer-bulk-test"})}.to_json,
36
+ '{"author":"Author 2", "title":"Book 2"}',
43
37
  nil
44
38
  ]
45
39
  body = body.join "\n"
@@ -50,38 +44,42 @@ describe Elastomer::Client::Bulk do
50
44
 
51
45
  @index.refresh
52
46
 
53
- h = @index.docs("tweet").get :id => 1
54
- assert_equal "pea53", h["_source"]["author"]
47
+ h = @index.docs("book").get id: 1
48
+
49
+ assert_equal "Author 1", h["_source"]["author"]
55
50
 
56
- h = @index.docs("book").get :id => 1
57
- assert_equal "John Scalzi", h["_source"]["author"]
51
+ h = @index.docs("book").get id: 2
52
+
53
+ assert_equal "Author 2", h["_source"]["author"]
58
54
 
59
55
 
60
56
  body = [
61
- '{"index" : {"_id":"2", "_type":"book"}}',
62
- '{"author":"Tolkien", "title":"The Silmarillion"}',
63
- '{"delete" : {"_id":"1", "_type":"book"}}',
57
+ {index: document_wrapper("book", {_id: "3"})}.to_json,
58
+ '{"author":"Author 3", "title":"Book 3"}',
59
+ {delete: document_wrapper("book", {_id: "1"})}.to_json,
64
60
  nil
65
61
  ]
66
62
  body = body.join "\n"
67
- h = $client.bulk body, :index => @name
63
+ h = $client.bulk body, index: @name
68
64
 
69
65
  assert_bulk_index h["items"].first, "expected to index a book"
70
66
  assert_bulk_delete h["items"].last, "expected to delete a book"
71
67
 
72
68
  @index.refresh
73
69
 
74
- h = @index.docs("book").get :id => 1
75
- assert !h["exists"], "was not successfully deleted"
70
+ h = @index.docs("book").get id: 1
71
+
72
+ refute h["exists"], "was not successfully deleted"
76
73
 
77
- h = @index.docs("book").get :id => 2
78
- assert_equal "Tolkien", h["_source"]["author"]
74
+ h = @index.docs("book").get id: 3
75
+
76
+ assert_equal "Author 3", h["_source"]["author"]
79
77
  end
80
78
 
81
79
  it "supports a nice block syntax" do
82
80
  h = @index.bulk do |b|
83
- b.index :_id => 1, :_type => "tweet", :author => "pea53", :message => "just a test tweet"
84
- b.index :_id => nil, :_type => "book", :author => "John Scalzi", :title => "Old Mans War"
81
+ b.index({ author: "Author 1", title: "Book 1" }, { _id: 1, _type: "book" })
82
+ b.index({ author: "Author 2", title: "Book 2" }, { _id: nil, _type: "book" })
85
83
  end
86
84
  items = h["items"]
87
85
 
@@ -89,62 +87,52 @@ describe Elastomer::Client::Bulk do
89
87
 
90
88
  assert_equal 2, h["items"].length
91
89
 
92
- if bulk_index_returns_create_for_new_documents?
93
- assert_bulk_index h["items"].first
94
- assert_bulk_create h["items"].last
95
- book_id = items.last["create"]["_id"]
96
- else
97
- assert_bulk_index h["items"].first
98
- assert_bulk_index h["items"].last
99
- book_id = items.last["index"]["_id"]
100
- end
90
+ assert_bulk_index h["items"].first
91
+ assert_bulk_index h["items"].last
92
+ book_id = items.last["index"]["_id"]
101
93
 
102
94
  assert_match %r/^\S{20,22}$/, book_id
103
95
 
104
96
  @index.refresh
105
97
 
106
- h = @index.docs("tweet").get :id => 1
107
- assert_equal "pea53", h["_source"]["author"]
98
+ h = @index.docs("book").get id: 1
99
+
100
+ assert_equal "Author 1", h["_source"]["author"]
108
101
 
109
- h = @index.docs("book").get :id => book_id
110
- assert_equal "John Scalzi", h["_source"]["author"]
102
+ h = @index.docs("book").get id: book_id
111
103
 
104
+ assert_equal "Author 2", h["_source"]["author"]
112
105
 
113
106
  h = @index.bulk do |b|
114
- b.index :_id => "", :_type => "book", :author => "Tolkien", :title => "The Silmarillion"
115
- b.delete :_id => book_id, :_type => "book"
107
+ b.index({ author: "Author 3", title: "Book 3" }, _id: "", _type: "book")
108
+ b.delete ({_id: book_id, _type: "book"})
116
109
  end
117
110
  items = h["items"]
118
111
 
119
112
  assert_equal 2, h["items"].length
120
113
 
121
- if bulk_index_returns_create_for_new_documents?
122
- assert_bulk_create h["items"].first, "expected to create a book"
123
- assert_bulk_delete h["items"].last, "expected to delete a book"
124
-
125
- book_id2 = items.first["create"]["_id"]
126
- else
127
- assert_bulk_index h["items"].first, "expected to create a book"
128
- assert_bulk_delete h["items"].last, "expected to delete a book"
114
+ assert_bulk_index h["items"].first, "expected to create a book"
115
+ assert_bulk_delete h["items"].last, "expected to delete a book"
129
116
 
130
- book_id2 = items.first["index"]["_id"]
131
- end
117
+ book_id2 = items.first["index"]["_id"]
132
118
 
133
119
  assert_match %r/^\S{20,22}$/, book_id2
134
120
 
135
121
  @index.refresh
136
122
 
137
- h = @index.docs("book").get :id => book_id
138
- assert !h["exists"], "was not successfully deleted"
123
+ h = @index.docs("book").get id: book_id
139
124
 
140
- h = @index.docs("book").get :id => book_id2
141
- assert_equal "Tolkien", h["_source"]["author"]
125
+ refute h["exists"], "was not successfully deleted"
126
+
127
+ h = @index.docs("book").get id: book_id2
128
+
129
+ assert_equal "Author 3", h["_source"]["author"]
142
130
  end
143
131
 
144
132
  it "allows documents to be JSON strings" do
145
133
  h = @index.bulk do |b|
146
- b.index '{"author":"pea53", "message":"just a test tweet"}', :_id => 1, :_type => "tweet"
147
- b.create '{"author":"John Scalzi", "title":"Old Mans War"}', :_id => 1, :_type => "book"
134
+ b.index '{"author":"Author 1", "title":"Book 1"}', {_id: 1, _type: "book"}
135
+ b.create '{"author":"Author 2", "title":"Book 2"}', {_id: 2, _type: "book"}
148
136
  end
149
137
 
150
138
  assert_kind_of Integer, h["took"]
@@ -154,15 +142,17 @@ describe Elastomer::Client::Bulk do
154
142
 
155
143
  @index.refresh
156
144
 
157
- h = @index.docs("tweet").get :id => 1
158
- assert_equal "pea53", h["_source"]["author"]
145
+ h = @index.docs("book").get id: 1
146
+
147
+ assert_equal "Author 1", h["_source"]["author"]
148
+
149
+ h = @index.docs("book").get id: 2
159
150
 
160
- h = @index.docs("book").get :id => 1
161
- assert_equal "John Scalzi", h["_source"]["author"]
151
+ assert_equal "Author 2", h["_source"]["author"]
162
152
 
163
153
  h = @index.bulk do |b|
164
- b.index '{"author":"Tolkien", "title":"The Silmarillion"}', :_id => 2, :_type => "book"
165
- b.delete :_id => 1, :_type => "book"
154
+ b.index '{"author":"Author 3", "title":"Book 3"}', {_id: 3, _type: "book"}
155
+ b.delete ({_id: 1, _type: "book"})
166
156
  end
167
157
 
168
158
  assert_bulk_index h["items"].first, "expected to index a book"
@@ -170,32 +160,39 @@ describe Elastomer::Client::Bulk do
170
160
 
171
161
  @index.refresh
172
162
 
173
- h = @index.docs("book").get :id => 1
174
- assert !h["exists"], "was not successfully deleted"
163
+ h = @index.docs("book").get id: 1
175
164
 
176
- h = @index.docs("book").get :id => 2
177
- assert_equal "Tolkien", h["_source"]["author"]
165
+ refute h["exists"], "was not successfully deleted"
166
+
167
+ h = @index.docs("book").get id: 3
168
+
169
+ assert_equal "Author 3", h["_source"]["author"]
178
170
  end
179
171
 
180
172
  it "executes a bulk API call when a request size is reached" do
181
173
  ary = []
182
- ary << @index.bulk(:request_size => 300) do |b|
174
+ # since ES8 does not include the mapping type in the document, it has less characters per request
175
+ # add characters to the document to get 100 characters per request
176
+ book_title = $client.version_support.es_version_8_plus? ? "A"*52 : "A"*34
177
+ ary << @index.bulk(request_size: 300) do |b|
183
178
  2.times { |num|
184
- document = {:_id => num, :_type => "tweet", :author => "pea53", :message => "tweet #{num} is a 100 character request"}
185
- ary << b.index(document)
179
+ document = { author: "Author 1", title: book_title}
180
+ ary << b.index(document, { _id: num, _type: "book" })
186
181
  }
187
182
  ary.compact!
183
+
188
184
  assert_equal 0, ary.length
189
185
 
190
186
  7.times { |num|
191
- document = {:_id => num+2, :_type => "tweet", :author => "pea53", :message => "tweet #{num+2} is a 100 character request"}
192
- ary << b.index(document)
187
+ document = { author: "Author 1", title: book_title }
188
+ ary << b.index(document, { _id: num+2, _type: "book" })
193
189
  }
194
190
  ary.compact!
191
+
195
192
  assert_equal 4, ary.length
196
193
 
197
- document = {:_id => 10, :_type => "tweet", :author => "pea53", :message => "tweet 10 is a 102 character request"}
198
- ary << b.index(document)
194
+ document = {author: "Author 1", title: book_title}
195
+ ary << b.index(document, {_id: 10, _type: "book"})
199
196
  end
200
197
  ary.compact!
201
198
 
@@ -203,30 +200,36 @@ describe Elastomer::Client::Bulk do
203
200
  ary.each { |a| a["items"].each { |b| assert_bulk_index(b) } }
204
201
 
205
202
  @index.refresh
206
- h = @index.docs.search :q => "*:*", :size => 0
203
+ h = @index.docs.search q: "*:*", size: 0
207
204
 
208
- assert_equal 10, h["hits"]["total"]
205
+ if $client.version_support.es_version_8_plus?
206
+ assert_equal 10, h["hits"]["total"]["value"]
207
+ else
208
+ assert_equal 10, h["hits"]["total"]
209
+ end
209
210
  end
210
211
 
211
212
  it "executes a bulk API call when an action count is reached" do
212
213
  ary = []
213
- ary << @index.bulk(:action_count => 3) do |b|
214
+ ary << @index.bulk(action_count: 3) do |b|
214
215
  2.times { |num|
215
- document = {:_id => num, :_type => "tweet", :author => "pea53", :message => "this is tweet number #{num}"}
216
- ary << b.index(document)
216
+ document = {author: "Author 1", title: "This is book number #{num}"}
217
+ ary << b.index(document, {_id: num, _type: "book"})
217
218
  }
218
219
  ary.compact!
220
+
219
221
  assert_equal 0, ary.length
220
222
 
221
223
  7.times { |num|
222
- document = {:_id => num+2, :_type => "tweet", :author => "pea53", :message => "this is tweet number #{num+2}"}
223
- ary << b.index(document)
224
+ document = {author: "Author 1", title: "This is book number #{num+2}"}
225
+ ary << b.index(document, {_id: num+2, _type: "book"})
224
226
  }
225
227
  ary.compact!
228
+
226
229
  assert_equal 2, ary.length
227
230
 
228
- document = {:_id => 10, :_type => "tweet", :author => "pea53", :message => "this is tweet number 10"}
229
- ary << b.index(document)
231
+ document = {author: "Author 1", title: "This is book number 10"}
232
+ ary << b.index(document, {_id: 10, _type: "book"})
230
233
  end
231
234
  ary.compact!
232
235
 
@@ -234,26 +237,32 @@ describe Elastomer::Client::Bulk do
234
237
  ary.each { |a| a["items"].each { |b| assert_bulk_index(b) } }
235
238
 
236
239
  @index.refresh
237
- h = @index.docs.search :q => "*:*", :size => 0
240
+ h = @index.docs.search q: "*:*", size: 0
238
241
 
239
- assert_equal 10, h["hits"]["total"]
242
+ if $client.version_support.es_version_8_plus?
243
+ assert_equal 10, h["hits"]["total"]["value"]
244
+ else
245
+ assert_equal 10, h["hits"]["total"]
246
+ end
240
247
  end
241
248
 
242
- it "rejects documents that excceed the maximum request size" do
243
- client = Elastomer::Client.new($client_params.merge(:max_request_size => 300))
249
+ it "rejects documents that exceed the maximum request size" do
250
+ client = ElastomerClient::Client.new(**$client_params.merge(max_request_size: 300))
244
251
  index = client.index(@name)
245
252
 
246
253
  ary = []
247
- ary << index.bulk(:request_size => 300) do |b|
254
+ book_title = $client.version_support.es_version_8_plus? ? "A"*52 : "A"*34
255
+ ary << index.bulk(request_size: 300) do |b|
248
256
  2.times { |num|
249
- document = {:_id => num, :_type => "tweet", :author => "pea53", :message => "tweet #{num} is a 100 character request"}
250
- ary << b.index(document)
257
+ document = {author: "Author 1", title: book_title}
258
+ ary << b.index(document, document_wrapper("book", { _id: num }))
251
259
  }
252
260
  ary.compact!
261
+
253
262
  assert_equal 0, ary.length
254
263
 
255
- document = {:_id => 342, :_type => "tweet", :author => "pea53", :message => "a"*290}
256
- assert_raises(Elastomer::Client::RequestSizeError) { b.index(document) }
264
+ document = { author: "Author 1", message: "A"*290 }
265
+ assert_raises(ElastomerClient::Client::RequestSizeError) { b.index(document, document_wrapper("book", { _id: 342 })) }
257
266
  end
258
267
  ary.compact!
259
268
 
@@ -261,88 +270,103 @@ describe Elastomer::Client::Bulk do
261
270
  ary.each { |a| a["items"].each { |b| assert_bulk_index(b) } }
262
271
 
263
272
  index.refresh
264
- h = index.docs.search :q => "*:*", :size => 0
273
+ h = index.docs.search q: "*:*", size: 0
265
274
 
266
- assert_equal 2, h["hits"]["total"]
275
+ if $client.version_support.es_version_8_plus?
276
+ assert_equal 2, h["hits"]["total"]["value"]
277
+ else
278
+ assert_equal 2, h["hits"]["total"]
279
+ end
267
280
  end
268
281
 
269
- it "uses :id from parameters" do
282
+ it "uses :id from parameters and supports symbol and string parameters" do
270
283
  response = @index.bulk do |b|
271
- document = { :_type => "tweet", :author => "pea53", :message => "just a test tweet" }
272
- params = { :id => "foo" }
284
+ document1 = { author: "Author 1", title: "Book 1" }
285
+ b.index document1, { id: "foo", type: "book" }
273
286
 
274
- b.index document, params
287
+ document2 = { author: "Author 2", title: "Book 2" }
288
+ b.index document2, { "id" => "bar", "type" => "book" }
275
289
  end
276
290
 
277
291
  assert_kind_of Integer, response["took"]
278
292
 
279
293
  items = response["items"]
294
+
280
295
  assert_bulk_index(items[0])
281
296
 
282
297
  assert_equal "foo", items[0]["index"]["_id"]
298
+ assert_equal "Book 1", @index.docs("book").get(id: "foo")["_source"]["title"]
299
+
300
+ assert_equal "bar", items[1]["index"]["_id"]
301
+ assert_equal "Book 2", @index.docs("book").get(id: "bar")["_source"]["title"]
283
302
  end
284
303
 
285
- it "supports symbol and string parameters" do
304
+ it "empty symbol and string parameters don't set id" do
286
305
  response = @index.bulk do |b|
287
- doc1 = { :author => "pea53", :message => "a tweet about foo" }
288
- b.index doc1, { :id => "foo", :type => "tweet" }
306
+ document1 = { author: "Author 1", title: "Book 1" }
307
+ b.index document1, { id: "", type: "book" }
289
308
 
290
- doc2 = { :author => "pea53", :message => "a tweet about bar" }
291
- b.index doc2, { "id" => "bar", "type" => "tweet" }
309
+ document2 = { author: "Author 2", title: "Book 2" }
310
+ b.index document2, { "id" => "", "type" => "book" }
292
311
  end
293
312
 
294
313
  assert_kind_of Integer, response["took"]
295
314
 
296
315
  items = response["items"]
316
+
297
317
  assert_bulk_index(items[0])
298
- assert_bulk_index(items[1])
299
318
 
300
- assert_equal "a tweet about foo", @index.docs("tweet").get(:id => "foo")["_source"]["message"]
301
- assert_equal "a tweet about bar", @index.docs("tweet").get(:id => "bar")["_source"]["message"]
319
+ # ES will generate ids for these documents
320
+ id1 = items[0]["index"]["_id"]
321
+ id2 = items[1]["index"]["_id"]
322
+
323
+ assert_equal "Book 1", @index.docs("book").get(id: id1)["_source"]["title"]
324
+ assert_equal "Book 2", @index.docs("book").get(id: id2)["_source"]["title"]
302
325
  end
303
326
 
304
- it 'doesn\'t override parameters from the document' do
305
- document = { :_id => 1, :_type => "tweet", :author => "pea53", :message => "just a test tweet" }
306
- params = { :id => 2 }
327
+ it "supports the routing parameter on index actions" do
328
+ document = { title: "Book 1" }
307
329
 
308
330
  response = @index.bulk do |b|
309
- b.index document, params
331
+ b.index document, { routing: "custom", _id: 1, _type: "book" }
310
332
  end
311
333
 
312
- assert_kind_of Integer, response["took"]
313
-
314
334
  items = response["items"]
315
- assert_bulk_index(items[0])
316
335
 
317
- refute_found @index.docs("tweet").get(:id => 1)
318
- assert_equal "just a test tweet", @index.docs("tweet").get(:id => 2)["_source"]["message"]
336
+ assert_kind_of Integer, response["took"]
337
+ assert_bulk_index(items[0])
338
+ assert_equal "custom", @index.docs("book").get(id: 1)["_routing"]
319
339
  end
320
340
 
321
- it 'doesn\'t upgrade non-prefixed keys to parameters' do
322
- document = { :id => 1, :type => "book", :version => 5, :author => "pea53", :message => "just a test tweet" }
323
- params = { :id => 2, :type => "tweet" }
341
+ it "supports the routing parameter within params in ES5 and ES8" do
342
+ document = { title: "Book 1" }
343
+
344
+ params = { _id: 1, _type: "book" }
345
+ if $client.version_support.es_version_8_plus?
346
+ params[:routing] = "custom"
347
+ else
348
+ params[:_routing] = "custom"
349
+ end
324
350
 
325
351
  response = @index.bulk do |b|
326
352
  b.index document, params
327
353
  end
328
354
 
329
- assert_kind_of Integer, response["took"]
330
-
331
355
  items = response["items"]
332
- assert_bulk_index(items[0])
333
356
 
334
- assert_equal "2", items[0]["index"]["_id"]
335
- assert_equal "tweet", items[0]["index"]["_type"]
336
- assert_equal 1, items[0]["index"]["_version"]
357
+ assert_kind_of Integer, response["took"]
358
+ assert_bulk_index(items[0])
359
+ assert_equal "custom", @index.docs("book").get(id: 1)["_routing"]
337
360
  end
338
361
 
339
362
  it "streams bulk responses" do
340
363
  ops = [
341
- [:index, { :message => "tweet 1" }, { :_id => 1, :_type => "book", :_index => @index.name }],
342
- [:index, { :message => "tweet 2" }, { :_id => 2, :_type => "book", :_index => @index.name }],
343
- [:index, { :message => "tweet 3" }, { :_id => 3, :_type => "book", :_index => @index.name }]
364
+ [:index, { title: "Book 1" }, document_wrapper("book", { _id: 1, _index: @index.name })],
365
+ [:index, { title: "Book 2" }, document_wrapper("book", { _id: 2, _index: @index.name })],
366
+ [:index, { title: "Book 3" }, document_wrapper("book", { _id: 3, _index: @index.name })],
344
367
  ]
345
- responses = $client.bulk_stream_responses(ops, { :action_count => 2 }).to_a
368
+ responses = $client.bulk_stream_responses(ops, { action_count: 2 }).to_a
369
+
346
370
  assert_equal(2, responses.length)
347
371
  assert_bulk_index(responses[0]["items"][0])
348
372
  assert_bulk_index(responses[0]["items"][1])
@@ -351,12 +375,13 @@ describe Elastomer::Client::Bulk do
351
375
 
352
376
  it "streams bulk items" do
353
377
  ops = [
354
- [:index, { :message => "tweet 1" }, { :_id => 1, :_type => "book", :_index => @index.name }],
355
- [:index, { :message => "tweet 2" }, { :_id => 2, :_type => "book", :_index => @index.name }],
356
- [:index, { :message => "tweet 3" }, { :_id => 3, :_type => "book", :_index => @index.name }]
378
+ [:index, { title: "Book 1" }, document_wrapper("book", { _id: 1, _index: @index.name })],
379
+ [:index, { title: "Book 2" }, document_wrapper("book", { _id: 2, _index: @index.name })],
380
+ [:index, { title: "Book 3" }, document_wrapper("book", { _id: 3, _index: @index.name })],
357
381
  ]
358
382
  items = []
359
- $client.bulk_stream_items(ops, { :action_count => 2 }) { |item| items << item }
383
+ $client.bulk_stream_items(ops, { action_count: 2 }) { |item| items << item }
384
+
360
385
  assert_equal(3, items.length)
361
386
  assert_bulk_index(items[0])
362
387
  assert_bulk_index(items[1])