Skip to content

Commit 836df85

Browse files
committed
[Gem] Fixes Bulk Helper
Addresses an issue where when using `slice`, the wrong hash was being passed again to `ingest` therefore adding unneccessary hash nesting.
1 parent e2290fc commit 836df85

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

elasticsearch/lib/elasticsearch/helpers/bulk_helper.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,9 @@ def initialize(client, index, params = {})
4848
def ingest(docs, params = {}, body = {}, &block)
4949
ingest_docs = docs.map { |doc| { index: { _index: @index, data: doc} } }
5050
if (slice = params.delete(:slice))
51-
ingest_docs.each_slice(slice) { |items| ingest(items, params, &block) }
51+
ingest_docs.each_slice(slice) do |items|
52+
ingest(items.map { |item| item[:index][:data] }, params, &block)
53+
end
5254
else
5355
bulk_request(ingest_docs, params, &block)
5456
end

elasticsearch/spec/integration/helpers/bulk_helper_spec.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
context 'Elasticsearch client helpers' do
2222
context 'Bulk helper' do
2323
let(:index) { 'bulk_animals' }
24+
let(:index_slice) { 'bulk_animals_slice' }
2425
let(:params) { { refresh: 'wait_for' } }
2526
let(:bulk_helper) { Elasticsearch::Helpers::BulkHelper.new(client, index, params) }
2627
let(:docs) do
@@ -40,6 +41,7 @@
4041

4142
after do
4243
client.indices.delete(index: index, ignore: 404)
44+
client.indices.delete(index: index_slice, ignore: 404)
4345
end
4446

4547
it 'Ingests documents' do
@@ -76,10 +78,13 @@
7678

7779
it 'Ingests documents and yields response and docs' do
7880
slice = 2
81+
bulk_helper = Elasticsearch::Helpers::BulkHelper.new(client, index_slice, params)
7982
response = bulk_helper.ingest(docs, {slice: slice}) do |response, docs|
8083
expect(response).to be_an_instance_of Elasticsearch::API::Response
8184
expect(docs.count).to eq slice
8285
end
86+
response = client.search(index: index_slice, size: 200)
87+
expect(response['hits']['hits'].map { |a| a['_source'].transform_keys(&:to_sym) }).to eq docs
8388
end
8489

8590
context 'JSON File helper' do

0 commit comments

Comments
 (0)