Class: Searchkick::ProcessBatchJob

Inherits:
ActiveJob::Base
  • Object
show all
Defined in:
lib/searchkick/process_batch_job.rb

Instance Method Summary collapse

Instance Method Details

#perform(class_name:, record_ids:) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/searchkick/process_batch_job.rb', line 5

def perform(class_name:, record_ids:)
  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 { |id| m = klass.new; m.id = id; m }

  # bulk reindex
  index = klass.searchkick_index
  Searchkick.callbacks(:bulk) do
    index.bulk_index(records) if records.any?
    index.bulk_delete(delete_records) if delete_records.any?
  end
end