Module: Searchkick::Reindex

Defined in:
lib/searchkick/reindex.rb

Instance Method Summary collapse

Instance Method Details

#reindexObject



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

def reindex
  alias_name = tire.index.name
  new_index = alias_name + "_" + Time.now.strftime("%Y%m%d%H%M%S")
  index = Tire::Index.new(new_index)

  index.create searchkick_index_options

  # use scope for import
  scope = respond_to?(:search_import) ? search_import : self
  scope.find_in_batches do |batch|
    index.import batch
  end

  if a = Tire::Alias.find(alias_name)
    old_indices = a.indices.dup
    old_indices.each do |index|
      a.indices.delete index
    end

    a.indices.add new_index
    response = a.save

    if response.success?
      old_indices.each do |index|
        i = Tire::Index.new(index)
        i.delete
      end
    end
  else
    tire.index.delete
    response = Tire::Alias.create(name: alias_name, indices: [new_index])
  end

  response.success? || (raise response.to_s)
end