Sha256: 75b18e938ad2309b127f6cfc92e68fb2fbf6698321f51e04ae079c85ac47dc9d
Contents?: true
Size: 1.11 KB
Versions: 18
Compression:
Stored size: 1.11 KB
Contents
module Searchkick class ProcessBatchJob < ActiveJob::Base queue_as { Searchkick.queue_name } def perform(class_name:, record_ids:, index_name: nil) # separate routing from id routing = Hash[record_ids.map { |r| r.split(/(?<!\|)\|(?!\|)/, 2).map { |v| v.gsub("||", "|") } }] record_ids = routing.keys klass = class_name.constantize scope = Searchkick.load_records(klass, record_ids) scope = scope.search_import if scope.respond_to?(:search_import) records = scope.select(&:should_index?) # determine which records to delete delete_ids = record_ids - records.map { |r| r.id.to_s } delete_records = delete_ids.map do |id| m = klass.new m.id = id if routing[id] m.define_singleton_method(:search_routing) do routing[id] end end m end # bulk reindex index = klass.searchkick_index(name: index_name) Searchkick.callbacks(:bulk) do index.bulk_index(records) if records.any? index.bulk_delete(delete_records) if delete_records.any? end end end end
Version data entries
18 entries across 18 versions & 1 rubygems