searchkick.rb
1.46 KB
1
2
3
4
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
42
43
44
45
46
47
48
49
require "searchkick/version"
require "searchkick/tasks"
require "tire"
module Searchkick
module ClassMethods
# https://gist.github.com/jarosan/3124884
def reindex
alias_name = klass.tire.index.name
new_index = alias_name + "_" + Time.now.strftime("%Y%m%d%H%M%S")
# Rake::Task["tire:import"].invoke
index = Tire::Index.new(new_index)
Tire::Tasks::Import.create_index(index, klass)
scope = klass.respond_to?(:tire_import) ? klass.tire_import : klass
scope.find_in_batches do |batch|
index.import batch
end
if a = Tire::Alias.find(alias_name)
puts "[IMPORT] Alias found: #{Tire::Alias.find(alias_name).indices.to_ary.join(",")}"
old_indices = Tire::Alias.find(alias_name).indices
old_indices.each do |index|
a.indices.delete index
end
a.indices.add new_index
a.save
old_indices.each do |index|
puts "[IMPORT] Deleting index: #{index}"
i = Tire::Index.new(index)
i.delete if i.exists?
end
else
puts "[IMPORT] No alias found. Deleting index, creating new one, and setting up alias"
i = Tire::Index.new(alias_name)
i.delete if i.exists?
Tire::Alias.create(name: alias_name, indices: [new_index])
end
puts "[IMPORT] Saved alias #{alias_name} pointing to #{new_index}"
end
end
end
Tire::Model::Search::ClassMethodsProxy.send :include, Searchkick::ClassMethods