Class: Searchkick::RelationIndexer

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(index) ⇒ RelationIndexer

Returns a new instance of RelationIndexer.



5
6
7
# File 'lib/searchkick/relation_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/relation_indexer.rb', line 3

def index
  @index
end

Instance Method Details

#batch_completed(batch_id) ⇒ Object



53
54
55
# File 'lib/searchkick/relation_indexer.rb', line 53

def batch_completed(batch_id)
  Searchkick.with_redis { |r| r.call("SREM", batches_key, [batch_id]) }
end

#batches_leftObject



49
50
51
# File 'lib/searchkick/relation_indexer.rb', line 49

def batches_left
  Searchkick.with_redis { |r| r.call("SCARD", batches_key) }
end

#reindex(relation, mode:, method_name: nil, full: false, resume: false, scope: nil) ⇒ 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
# File 'lib/searchkick/relation_indexer.rb', line 9

def reindex(relation, mode:, method_name: nil, full: false, resume: false, scope: nil)
  # apply scopes
  if scope
    relation = relation.send(scope)
  elsif relation.respond_to?(:search_import)
    relation = relation.search_import
  end

  # remove unneeded loading for async and queue
  if mode == :async || mode == :queue
    if relation.respond_to?(:primary_key)
      relation = relation.except(:includes, :preload)
      unless mode == :queue && relation.klass.method_defined?(:search_routing)
        relation = relation.except(:select).select(relation.primary_key)
      end
    elsif relation.respond_to?(:only)
      unless mode == :queue && relation.klass.method_defined?(:search_routing)
        relation = relation.only(:_id)
      end
    end
  end

  if mode == :async && full
    return full_reindex_async(relation)
  end

  relation = resume_relation(relation) if resume

  reindex_options = {
    mode: mode,
    method_name: method_name,
    full: full
  }
  record_indexer = RecordIndexer.new(index)

  in_batches(relation) do |items|
    record_indexer.reindex(items, **reindex_options)
  end
end