需要使用elasticsearch-ruby在elasticsearch索引中进行批量升级。任何帮助都将不胜感激。
发布于 2019-10-14 13:21:17
基本上,您正在构建一个elasticsearch操作数组,可以使用下面的第二个代码块批量发送这些操作。这里的主要内容是了解每个操作所需的语法,这应该有助于向您展示delete/index/update如何工作。
注意: data_hash是通过查询您的模型并在返回的模型上使用elasticsearch helper方法'.as_indexed_json‘生成的。这就是您在现有elasticsearch记录上索引或更新的数据。Delete显然不需要这样做。
# 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下面是如何发送带有一些保护措施的请求
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希望这能对你有所帮助!
https://stackoverflow.com/questions/44371888
复制相似问题