Class: Searchkick::RecordIndexer

Inherits:
Object
  • Object
show all
Defined in:
lib/searchkick/record_indexer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(index) ⇒ RecordIndexer

Returns a new instance of RecordIndexer.



5
6
7
# File 'lib/searchkick/record_indexer.rb', line 5

def initialize(index)
  @index = index
end

Instance Attribute Details

#indexObject (readonly)

Returns the value of attribute index.



3
4
5
# File 'lib/searchkick/record_indexer.rb', line 3

def index
  @index
end

Instance Method Details

#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

#reindex_items(klass, items, method_name:, single: false) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/searchkick/record_indexer.rb', line 63

def reindex_items(klass, items, method_name:, single: false)
  routing = items.to_h { |r| [r[:id], r[:routing]] }
  record_ids = routing.keys

  relation = Searchkick.load_records(klass, record_ids)
  # call search_import even for single records for nested associations
  relation = relation.search_import if relation.respond_to?(:search_import)
  records = relation.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|
      construct_record(klass, id, routing[id])
    end

  import_inline(records, delete_records, method_name: method_name, single: single)
end