diff --git a/elasticsearch/lib/elasticsearch/helpers/bulk_helper.rb b/elasticsearch/lib/elasticsearch/helpers/bulk_helper.rb index e5982eccb7..62205326ed 100644 --- a/elasticsearch/lib/elasticsearch/helpers/bulk_helper.rb +++ b/elasticsearch/lib/elasticsearch/helpers/bulk_helper.rb @@ -76,12 +76,15 @@ def delete(ids, params = {}, body = {}) # @yieldparam ingest_docs [Array] The collection of documents sent in the bulk request. # def update(docs, params = {}, body = {}, &block) - ingest_docs = docs.map do |doc| - { update: { _index: @index, _id: doc.delete('id'), data: { doc: doc } } } - end - if (slice = params.delete(:slice)) - ingest_docs.each_slice(slice) { |items| update(items, params, &block) } + if (slice = params[:slice]) + request_params = params.dup + request_params.delete(:slice) + docs.each_slice(slice) { |items| update(items, request_params, &block) } else + ingest_docs = docs.map do |doc| + document = doc.dup + { update: { _index: @index, _id: document.delete('id'), data: { doc: document } } } + end bulk_request(ingest_docs, params, &block) end end diff --git a/elasticsearch/spec/integration/helpers/bulk_helper_spec.rb b/elasticsearch/spec/integration/helpers/bulk_helper_spec.rb index 1633a12ce2..61accc3a4b 100644 --- a/elasticsearch/spec/integration/helpers/bulk_helper_spec.rb +++ b/elasticsearch/spec/integration/helpers/bulk_helper_spec.rb @@ -65,6 +65,29 @@ response = bulk_helper.update(docs) expect(response.status).to eq(200) expect(response['items'].map { |i| i['update']['result'] }.uniq.first).to eq('updated') + expect(docs.map { |doc| doc['id'] }).not_to include(nil) + end + + it 'Updates documents in slices' do + docs = [ + { scientific_name: 'Otocyon megalotos', name: 'Bat-eared fox' }, + { scientific_name: 'Herpestes javanicus', name: 'Small Indian mongoose' } + ] + bulk_helper = Elasticsearch::Helpers::BulkHelper.new(CLIENT, index_slice, params) + bulk_helper.ingest(docs) + animals = CLIENT.search(index: index_slice, size: 200)['hits']['hits'] + docs = animals.map { |animal| animal['_source'].merge({ 'id' => animal['_id'] }) } + docs.map { |doc| doc['scientific_name'].upcase! } + + bulk_helper.update(docs, { slice: 1 }) do |response, update_docs| + expect(response.status).to eq(200) + expect(update_docs.count).to eq(1) + end + + expect(docs.map { |doc| doc['id'] }).not_to include(nil) + response = CLIENT.search(index: index_slice, size: 200) + expect(response['hits']['hits'].map { |animal| animal['_source']['scientific_name'] }.sort) + .to eq(docs.map { |doc| doc['scientific_name'] }.sort) end it 'Deletes documents' do