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

#bulk_delete(records) ⇒ Object



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

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



39
40
41
# File 'lib/searchkick/bulk_indexer.rb', line 39

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

#bulk_update(records, method_name) ⇒ Object



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

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

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

def import_scope(scope, resume: false, method_name: nil, async: false, batch: false, batch_id: nil, full: false)
  # use scope for import
  scope = scope.search_import if scope.respond_to?(:search_import)

  if batch
    import_or_update scope.to_a, method_name, async
    Searchkick.with_redis { |r| r.srem(batches_key, batch_id) } if batch_id
  elsif full && async
    full_reindex_async(scope)
  elsif scope.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
      scope = scope.where("id > ?", total_docs)
    end

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

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