Module: Searchkick::Reindex

Defined in:
lib/searchkick/reindex.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(klass) ⇒ Object



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

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

Instance Method Details

#clean_indicesObject

remove old indices that start w/ index_name



36
37
38
39
40
41
42
43
# File 'lib/searchkick/reindex.rb', line 36

def clean_indices
  all_indices = Searchkick.client.indices.get_aliases
  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|
    Searchkick::Index.new(index).delete
  end
  indices
end

#reindexObject



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

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

  clean_indices

  index.create searchkick_index_options

  # check if alias exists
  if Searchkick.client.indices.exists_alias(name: alias_name)
    searchkick_import(index) # import before swap

    # get existing indices to remove
    old_indices = Searchkick.client.indices.get_alias(name: alias_name).keys
    actions = old_indices.map{|name| {remove: {index: name, alias: alias_name}} } + [{add: {index: new_name, alias: alias_name}}]
    Searchkick.client.indices.update_aliases body: {actions: actions}
    clean_indices
  else
    searchkick_index.delete if searchkick_index.exists?
    Searchkick.client.indices.update_aliases body: {actions: [{add: {index: new_name, alias: alias_name}}]}
    searchkick_import(index) # import after swap
  end

  index.refresh

  true
end