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(record) ⇒ RecordIndexer

Returns a new instance of RecordIndexer.



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

def initialize(record)
  @record = record
  @index = record.class.searchkick_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

#recordObject (readonly)

Returns the value of attribute record.



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

def record
  @record
end

Instance Method Details

#reindex(method_name = nil, refresh: false, mode: nil) ⇒ Object



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
# File 'lib/searchkick/record_indexer.rb', line 10

def reindex(method_name = nil, refresh: false, mode: nil)
  unless [:inline, true, nil, :async, :queue].include?(mode)
    raise ArgumentError, "Invalid value for mode"
  end

  mode ||= Searchkick.callbacks_value || index.options[:callbacks] || true

  case mode
  when :queue
    if method_name
      raise Searchkick::Error, "Partial reindex not supported with queue option"
    end

    index.reindex_queue.push(record.id.to_s)
  when :async
    unless defined?(ActiveJob)
      raise Searchkick::Error, "Active Job not found"
    end

    Searchkick::ReindexV2Job.perform_later(
      record.class.name,
      record.id.to_s,
      method_name ? method_name.to_s : nil
    )
  else # bulk, inline/true/nil
    reindex_record(method_name)

    index.refresh if refresh
  end
end