Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions elasticsearch/lib/elasticsearch/helpers/bulk_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,15 @@ def delete(ids, params = {}, body = {})
# @yieldparam ingest_docs [Array<Hash>] 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
Expand Down
23 changes: 23 additions & 0 deletions elasticsearch/spec/integration/helpers/bulk_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading