Reputation: 697
Need to do bulk upsert in an elasticsearch index, with elasticsearch-ruby. Any help will be appreciated.
Upvotes: 2
Views: 1286
Reputation: 96
Basically you are building an array of elasticsearch operations which can be sent in bulk with the 2nd block of code below. The main thing here is knowing the syntax required for each operation and this should help show you how delete/index/update works.
Note: data_hash is generated by querying your model and using the elasticsearch helper method '.as_indexed_json' on the returned model. This is then the data you are indexing or updating on an existing elasticsearch record. Delete obviously doesn't require this.
# operations is an array of model ids and the operation you want to perform on them
batch_for_bulk = []
operations.each do |id, operation|
data_hash = YourModel.find(id).as_indexed_json
if operation == 'delete'
batch_for_bulk.push({ delete: { _id: id}})
elsif operation == 'index'
batch_for_bulk.push({ index: { _id: id, data: data_hash}})
elsif operation == 'update'
batch_for_bulk.push({ update: { _id: id, data: {doc: data_hash}}})
end
end
And here's how to send the request with some protections
begin
YourModel.__elasticsearch__.client.bulk(
index: YourModel.index_name,
body: batch_for_bulk
) if batch_for_bulk.present?
rescue Faraday::TimeoutError
# handle your errors here
end
Hope this is helpful!
Upvotes: 2