Module: Searchkick::Reindex

Defined in:
lib/searchkick/reindex.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(klass) ⇒ Object



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

def self.extended(klass)
  (@descendents ||= []) << klass
end

Instance Method Details

#clean_indicesObject

remove old indices that start w/ index_name



44
45
46
47
48
49
50
51
# File 'lib/searchkick/reindex.rb', line 44

def clean_indices
  all_indices = JSON.parse(Tire::Configuration.client.get("#{Tire::Configuration.url}/_aliases").body)
  indices = all_indices.select{|k, v| v["aliases"].empty? && k =~ /\A#{Regexp.escape(searchkick_index.name)}_\d{14,17}\z/ }.keys
  indices.each do |index|
    Tire::Index.new(index).delete
  end
  indices
end

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

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

  clean_indices

  success = index.create searchkick_index_options
  raise index.response.to_s if !success

  if a = Tire::Alias.find(alias_name)
    searchkick_import(index) # import before swap

    a.indices.each do |i|
      a.indices.delete i
    end

    a.indices.add new_index
    response = a.save

    if response.success?
      clean_indices
    else
      raise response.to_s
    end
  else
    searchkick_index.delete if searchkick_index.exists?
    response = Tire::Alias.create(name: alias_name, indices: [new_index])
    raise response.to_s if !response.success?

    searchkick_import(index) # import after swap
  end

  index.refresh

  true
end