elastomer-client 3.2.2 → 6.2.0

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 +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,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,64 +87,53 @@ 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
124
+
125
+ refute h["exists"], "was not successfully deleted"
139
126
 
140
- h = @index.docs("book").get :id => book_id2
141
- assert_equal "Tolkien", h["_source"]["author"]
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
- items = h["items"]
150
137
 
151
138
  assert_kind_of Integer, h["took"]
152
139
 
@@ -155,50 +142,57 @@ describe Elastomer::Client::Bulk do
155
142
 
156
143
  @index.refresh
157
144
 
158
- h = @index.docs("tweet").get :id => 1
159
- assert_equal "pea53", h["_source"]["author"]
145
+ h = @index.docs("book").get id: 1
160
146
 
161
- h = @index.docs("book").get :id => 1
162
- assert_equal "John Scalzi", h["_source"]["author"]
147
+ assert_equal "Author 1", h["_source"]["author"]
163
148
 
149
+ h = @index.docs("book").get id: 2
150
+
151
+ assert_equal "Author 2", h["_source"]["author"]
164
152
 
165
153
  h = @index.bulk do |b|
166
- b.index '{"author":"Tolkien", "title":"The Silmarillion"}', :_id => 2, :_type => "book"
167
- 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"})
168
156
  end
169
- items = h["items"]
170
157
 
171
158
  assert_bulk_index h["items"].first, "expected to index a book"
172
159
  assert_bulk_delete h["items"].last, "expected to delete a book"
173
160
 
174
161
  @index.refresh
175
162
 
176
- h = @index.docs("book").get :id => 1
177
- assert !h["exists"], "was not successfully deleted"
163
+ h = @index.docs("book").get id: 1
178
164
 
179
- h = @index.docs("book").get :id => 2
180
- 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"]
181
170
  end
182
171
 
183
172
  it "executes a bulk API call when a request size is reached" do
184
173
  ary = []
185
- 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|
186
178
  2.times { |num|
187
- document = {:_id => num, :_type => "tweet", :author => "pea53", :message => "tweet #{num} is a 100 character request"}
188
- ary << b.index(document)
179
+ document = { author: "Author 1", title: book_title}
180
+ ary << b.index(document, { _id: num, _type: "book" })
189
181
  }
190
182
  ary.compact!
183
+
191
184
  assert_equal 0, ary.length
192
185
 
193
186
  7.times { |num|
194
- document = {:_id => num+2, :_type => "tweet", :author => "pea53", :message => "tweet #{num+2} is a 100 character request"}
195
- ary << b.index(document)
187
+ document = { author: "Author 1", title: book_title }
188
+ ary << b.index(document, { _id: num+2, _type: "book" })
196
189
  }
197
190
  ary.compact!
191
+
198
192
  assert_equal 4, ary.length
199
193
 
200
- document = {:_id => 10, :_type => "tweet", :author => "pea53", :message => "tweet 10 is a 102 character request"}
201
- ary << b.index(document)
194
+ document = {author: "Author 1", title: book_title}
195
+ ary << b.index(document, {_id: 10, _type: "book"})
202
196
  end
203
197
  ary.compact!
204
198
 
@@ -206,30 +200,36 @@ describe Elastomer::Client::Bulk do
206
200
  ary.each { |a| a["items"].each { |b| assert_bulk_index(b) } }
207
201
 
208
202
  @index.refresh
209
- h = @index.docs.search :q => "*:*", :size => 0
203
+ h = @index.docs.search q: "*:*", size: 0
210
204
 
211
- 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
212
210
  end
213
211
 
214
212
  it "executes a bulk API call when an action count is reached" do
215
213
  ary = []
216
- ary << @index.bulk(:action_count => 3) do |b|
214
+ ary << @index.bulk(action_count: 3) do |b|
217
215
  2.times { |num|
218
- document = {:_id => num, :_type => "tweet", :author => "pea53", :message => "this is tweet number #{num}"}
219
- ary << b.index(document)
216
+ document = {author: "Author 1", title: "This is book number #{num}"}
217
+ ary << b.index(document, {_id: num, _type: "book"})
220
218
  }
221
219
  ary.compact!
220
+
222
221
  assert_equal 0, ary.length
223
222
 
224
223
  7.times { |num|
225
- document = {:_id => num+2, :_type => "tweet", :author => "pea53", :message => "this is tweet number #{num+2}"}
226
- 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"})
227
226
  }
228
227
  ary.compact!
228
+
229
229
  assert_equal 2, ary.length
230
230
 
231
- document = {:_id => 10, :_type => "tweet", :author => "pea53", :message => "this is tweet number 10"}
232
- ary << b.index(document)
231
+ document = {author: "Author 1", title: "This is book number 10"}
232
+ ary << b.index(document, {_id: 10, _type: "book"})
233
233
  end
234
234
  ary.compact!
235
235
 
@@ -237,26 +237,32 @@ describe Elastomer::Client::Bulk do
237
237
  ary.each { |a| a["items"].each { |b| assert_bulk_index(b) } }
238
238
 
239
239
  @index.refresh
240
- h = @index.docs.search :q => "*:*", :size => 0
240
+ h = @index.docs.search q: "*:*", size: 0
241
241
 
242
- 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
243
247
  end
244
248
 
245
- it "rejects documents that excceed the maximum request size" do
246
- 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))
247
251
  index = client.index(@name)
248
252
 
249
253
  ary = []
250
- 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|
251
256
  2.times { |num|
252
- document = {:_id => num, :_type => "tweet", :author => "pea53", :message => "tweet #{num} is a 100 character request"}
253
- ary << b.index(document)
257
+ document = {author: "Author 1", title: book_title}
258
+ ary << b.index(document, document_wrapper("book", { _id: num }))
254
259
  }
255
260
  ary.compact!
261
+
256
262
  assert_equal 0, ary.length
257
263
 
258
- document = {:_id => 342, :_type => "tweet", :author => "pea53", :message => "a"*290}
259
- 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 })) }
260
266
  end
261
267
  ary.compact!
262
268
 
@@ -264,88 +270,103 @@ describe Elastomer::Client::Bulk do
264
270
  ary.each { |a| a["items"].each { |b| assert_bulk_index(b) } }
265
271
 
266
272
  index.refresh
267
- h = index.docs.search :q => "*:*", :size => 0
273
+ h = index.docs.search q: "*:*", size: 0
268
274
 
269
- 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
270
280
  end
271
281
 
272
- it "uses :id from parameters" do
282
+ it "uses :id from parameters and supports symbol and string parameters" do
273
283
  response = @index.bulk do |b|
274
- document = { :_type => "tweet", :author => "pea53", :message => "just a test tweet" }
275
- params = { :id => "foo" }
284
+ document1 = { author: "Author 1", title: "Book 1" }
285
+ b.index document1, { id: "foo", type: "book" }
276
286
 
277
- b.index document, params
287
+ document2 = { author: "Author 2", title: "Book 2" }
288
+ b.index document2, { "id" => "bar", "type" => "book" }
278
289
  end
279
290
 
280
291
  assert_kind_of Integer, response["took"]
281
292
 
282
293
  items = response["items"]
294
+
283
295
  assert_bulk_index(items[0])
284
296
 
285
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"]
286
302
  end
287
303
 
288
- it "supports symbol and string parameters" do
304
+ it "empty symbol and string parameters don't set id" do
289
305
  response = @index.bulk do |b|
290
- doc1 = { :author => "pea53", :message => "a tweet about foo" }
291
- b.index doc1, { :id => "foo", :type => "tweet" }
306
+ document1 = { author: "Author 1", title: "Book 1" }
307
+ b.index document1, { id: "", type: "book" }
292
308
 
293
- doc2 = { :author => "pea53", :message => "a tweet about bar" }
294
- b.index doc2, { "id" => "bar", "type" => "tweet" }
309
+ document2 = { author: "Author 2", title: "Book 2" }
310
+ b.index document2, { "id" => "", "type" => "book" }
295
311
  end
296
312
 
297
313
  assert_kind_of Integer, response["took"]
298
314
 
299
315
  items = response["items"]
316
+
300
317
  assert_bulk_index(items[0])
301
- assert_bulk_index(items[1])
302
318
 
303
- assert_equal "a tweet about foo", @index.docs("tweet").get(:id => "foo")["_source"]["message"]
304
- 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"]
305
325
  end
306
326
 
307
- it 'doesn\'t override parameters from the document' do
308
- document = { :_id => 1, :_type => "tweet", :author => "pea53", :message => "just a test tweet" }
309
- params = { :id => 2 }
327
+ it "supports the routing parameter on index actions" do
328
+ document = { title: "Book 1" }
310
329
 
311
330
  response = @index.bulk do |b|
312
- b.index document, params
331
+ b.index document, { routing: "custom", _id: 1, _type: "book" }
313
332
  end
314
333
 
315
- assert_kind_of Integer, response["took"]
316
-
317
334
  items = response["items"]
318
- assert_bulk_index(items[0])
319
335
 
320
- refute_found @index.docs("tweet").get(:id => 1)
321
- 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"]
322
339
  end
323
340
 
324
- it 'doesn\'t upgrade non-prefixed keys to parameters' do
325
- document = { :id => 1, :type => "book", :version => 5, :author => "pea53", :message => "just a test tweet" }
326
- 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
327
350
 
328
351
  response = @index.bulk do |b|
329
352
  b.index document, params
330
353
  end
331
354
 
332
- assert_kind_of Integer, response["took"]
333
-
334
355
  items = response["items"]
335
- assert_bulk_index(items[0])
336
356
 
337
- assert_equal "2", items[0]["index"]["_id"]
338
- assert_equal "tweet", items[0]["index"]["_type"]
339
- 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"]
340
360
  end
341
361
 
342
362
  it "streams bulk responses" do
343
363
  ops = [
344
- [:index, { :message => "tweet 1" }, { :_id => 1, :_type => "book", :_index => @index.name }],
345
- [:index, { :message => "tweet 2" }, { :_id => 2, :_type => "book", :_index => @index.name }],
346
- [: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 })],
347
367
  ]
348
- responses = $client.bulk_stream_responses(ops, { :action_count => 2 }).to_a
368
+ responses = $client.bulk_stream_responses(ops, { action_count: 2 }).to_a
369
+
349
370
  assert_equal(2, responses.length)
350
371
  assert_bulk_index(responses[0]["items"][0])
351
372
  assert_bulk_index(responses[0]["items"][1])
@@ -354,12 +375,13 @@ describe Elastomer::Client::Bulk do
354
375
 
355
376
  it "streams bulk items" do
356
377
  ops = [
357
- [:index, { :message => "tweet 1" }, { :_id => 1, :_type => "book", :_index => @index.name }],
358
- [:index, { :message => "tweet 2" }, { :_id => 2, :_type => "book", :_index => @index.name }],
359
- [: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 })],
360
381
  ]
361
382
  items = []
362
- $client.bulk_stream_items(ops, { :action_count => 2 }) { |item| items << item }
383
+ $client.bulk_stream_items(ops, { action_count: 2 }) { |item| items << item }
384
+
363
385
  assert_equal(3, items.length)
364
386
  assert_bulk_index(items[0])
365
387
  assert_bulk_index(items[1])