Method: Searchkick::RecordIndexer#reindex

Defined in:
lib/searchkick/record_indexer.rb

#reindex(records, mode:, method_name:, full: false, single: false) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/searchkick/record_indexer.rb', line 9

def reindex(records, mode:, method_name:, full: false, single: false)
  # prevents exists? check if records is a relation
  records = records.to_a
  return if records.empty?

  case mode
  when :async
    unless defined?(ActiveJob)
      raise Error, "Active Job not found"
    end

    # we could likely combine ReindexV2Job, BulkReindexJob, and ProcessBatchJob
    # but keep them separate for now
    if single
      record = records.first

      # always pass routing in case record is deleted
      # before the async job runs
      if record.respond_to?(:search_routing)
        routing = record.search_routing
      end

      Searchkick::ReindexV2Job.perform_later(
        record.class.name,
        record.id.to_s,
        method_name ? method_name.to_s : nil,
        routing: routing,
        index_name: index.name
      )
    else
      Searchkick::BulkReindexJob.perform_later(
        class_name: records.first.class.searchkick_options[:class_name],
        record_ids: records.map { |r| r.id.to_s },
        index_name: index.name,
        method_name: method_name ? method_name.to_s : nil
      )
    end
  when :queue
    if method_name
      raise Error, "Partial reindex not supported with queue option"
    end

    index.reindex_queue.push_records(records)
  when true, :inline
    index_records, other_records = records.partition { |r| index_record?(r) }
    import_inline(index_records, !full ? other_records : [], method_name: method_name, single: single)
  else
    raise ArgumentError, "Invalid value for mode"
  end

  # return true like model and relation reindex for now
  true
end