Class: Searchkick::BulkIndexer

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(index) ⇒ BulkIndexer

Returns a new instance of BulkIndexer.



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

def index
  @index
end

Instance Method Details

#batches_leftObject



54
55
56
# File 'lib/searchkick/bulk_indexer.rb', line 54

def batches_left
  Searchkick.with_redis { |r| r.scard(batches_key) }
end

#bulk_delete(records) ⇒ Object



46
47
48
# File 'lib/searchkick/bulk_indexer.rb', line 46

def bulk_delete(records)
  Searchkick.indexer.queue(records.reject { |r| r.id.blank? }.map { |r| RecordData.new(index, r).delete_data })
end

#bulk_index(records) ⇒ Object



42
43
44
# File 'lib/searchkick/bulk_indexer.rb', line 42

def bulk_index(records)
  Searchkick.indexer.queue(records.map { |r| RecordData.new(index, r).index_data })
end

#bulk_update(records, method_name) ⇒ Object



50
51
52
# File 'lib/searchkick/bulk_indexer.rb', line 50

def bulk_update(records, method_name)
  Searchkick.indexer.queue(records.map { |r| RecordData.new(index, r).update_data(method_name) })
end

#import_scope(relation, resume: false, method_name: nil, async: false, batch: false, batch_id: nil, full: 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
# File 'lib/searchkick/bulk_indexer.rb', line 9

def import_scope(relation, resume: false, method_name: nil, async: false, batch: false, batch_id: nil, full: false, scope: nil)
  if scope
    relation = relation.send(scope)
  elsif relation.respond_to?(:search_import)
    relation = relation.search_import
  end

  if batch
    import_or_update relation.to_a, method_name, async
    Searchkick.with_redis { |r| r.srem(batches_key, batch_id) } if batch_id
  elsif full && async
    full_reindex_async(relation)
  elsif relation.respond_to?(:find_in_batches)
    if resume
      # use total docs instead of max id since there's not a great way
      # to get the max _id without scripting since it's a string

      # TODO use primary key and prefix with table name
      relation = relation.where("id > ?", total_docs)
    end

    relation = relation.select("id").except(:includes, :preload) if async

    relation.find_in_batches batch_size: batch_size do |items|
      import_or_update items, method_name, async
    end
  else
    each_batch(relation) do |items|
      import_or_update items, method_name, async
    end
  end
end