class Elasticsearch::Model::Extensions::BatchUpdating::BatchUpdater
Public Class Methods
new(klass)
click to toggle source
# File lib/elasticsearch/model/extensions/batch_updating/batch_updater.rb, line 6 def initialize(klass) @klass = klass end
Public Instance Methods
klass()
click to toggle source
# File lib/elasticsearch/model/extensions/batch_updating/batch_updater.rb, line 10 def klass @klass end
reconnect!()
click to toggle source
# File lib/elasticsearch/model/extensions/batch_updating/batch_updater.rb, line 14 def reconnect! klass.connection.reconnect! # This is required to prevent requests from timing out when they are made in multiple processes(in other words, the process is forked). klass.__elasticsearch__.client.transport.__build_connections end
split_ids_into(chunk_num, min:nil, max:nil)
click to toggle source
# File lib/elasticsearch/model/extensions/batch_updating/batch_updater.rb, line 48 def split_ids_into(chunk_num, min:nil, max:nil) min ||= klass.minimum(:id) max ||= klass.maximum(:id) chunk_num.times.inject([]) do |r,i| chunk_size = ((max-min+1)/chunk_num.to_f).ceil first = chunk_size * i last = if i == chunk_num - 1 max else chunk_size * (i + 1) - 1 end r << (first..last) end end
update_index_in_batch(records, index: nil, type: nil, client: nil)
click to toggle source
@param [Array] records
# File lib/elasticsearch/model/extensions/batch_updating/batch_updater.rb, line 21 def update_index_in_batch(records, index: nil, type: nil, client: nil) client ||= klass.__elasticsearch__.client index ||= klass.index_name type ||= klass.document_type if records.size > 1 response = client.bulk \ index: index, type: type, body: records.map { |r| { index: { _id: r.id, data: r.as_indexed_json } } } one_or_more_errors_occurred = response["errors"] if one_or_more_errors_occurred if defined? ::Rails ::Rails.logger.warn "One or more error(s) occurred while updating the index #{records} for the type #{type}\n#{JSON.pretty_generate(response)}" else warn "One or more error(s) occurred while updating the index #{records} for the type #{type}\n#{JSON.pretty_generate(response)}" end end else records.each do |r| client.index index: index, type: type, id: r.id, body: r.as_indexed_json end end end