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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# 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

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

    # escape pipe with double pipe
    value = queue_escape(record.id.to_s)
    value = "#{value}|#{queue_escape(routing)}" if routing
    index.reindex_queue.push(value)
  when :async
    unless defined?(ActiveJob)
      raise Searchkick::Error, "Active Job not found"
    end

    # 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
    )
  else # bulk, inline/true/nil
    reindex_record(method_name)

    index.refresh if refresh
  end
end