Skip to content

Commit

Permalink
[Gem] Fixes Bulk Helper
Browse files Browse the repository at this point in the history
Addresses an issue where when using `slice`, the wrong hash was being
passed again to `ingest` therefore adding unneccessary hash nesting.
  • Loading branch information
picandocodigo committed Apr 18, 2024
1 parent 2a43b6a commit 3b1977a
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
4 changes: 3 additions & 1 deletion elasticsearch/lib/elasticsearch/helpers/bulk_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ def initialize(client, index, params = {})
def ingest(docs, params = {}, body = {}, &block)
ingest_docs = docs.map { |doc| { index: { _index: @index, data: doc} } }
if (slice = params.delete(:slice))
ingest_docs.each_slice(slice) { |items| ingest(items, params, &block) }
ingest_docs.each_slice(slice) do |items|
ingest(items.map { |item| item[:index][:data] }, params, &block)
end
else
bulk_request(ingest_docs, params, &block)
end
Expand Down
5 changes: 5 additions & 0 deletions elasticsearch/spec/integration/helpers/bulk_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
context 'Elasticsearch client helpers' do
context 'Bulk helper' do
let(:index) { 'bulk_animals' }
let(:index_slice) { 'bulk_animals_slice' }
let(:params) { { refresh: 'wait_for' } }
let(:bulk_helper) { Elasticsearch::Helpers::BulkHelper.new(client, index, params) }
let(:docs) do
Expand All @@ -40,6 +41,7 @@

after do
client.indices.delete(index: index, ignore: 404)
client.indices.delete(index: index_slice, ignore: 404)
end

it 'Ingests documents' do
Expand Down Expand Up @@ -76,10 +78,13 @@

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

context 'JSON File helper' do
Expand Down

0 comments on commit 3b1977a

Please sign in to comment.