This repository has been archived by the owner on Jun 30, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 533
Rake Task for Re indexing With Aliases
joe1chen edited this page Mar 27, 2013
·
1 revision
elasticsearch.rake
# Run with: rake environment elasticsearch:reindex
# Begins by creating the index using tire:import command. This will create the "official" index name, e.g. "person" each time.
# Then we rename it to, e.g. "person20121001" and alias "person" to it.
namespace :elasticsearch do
desc "re-index elasticsearch"
task :reindex => :environment do
klasses = [Place, Person, Caption]
klasses.each do |klass|
puts "::::::: #{klass} :::::::::\n"
ENV['CLASS'] = klass.name
ENV['INDEX'] = new_index = klass.tire.index.name << '_' << Time.now.strftime('%Y%m%d%H%M%S')
puts "New index #{new_index}"
Rake::Task["tire:import"].execute("CLASS='#{klass}'")
puts '[IMPORT] about to swap index'
if a = Tire::Alias.find(klass.tire.index.name)
puts "[IMPORT] aliases found: #{Tire::Alias.find(klass.tire.index.name).indices.to_ary.join(',')}. deleting."
old_indices = Tire::Alias.find(klass.tire.index.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 aliases found. deleting index. creating new one and setting up alias."
klass.tire.index.delete
a = Tire::Alias.new
a.name(klass.tire.index.name)
a.index(new_index)
a.save
puts "Saved alias #{klass.tire.index.name} pointing to #{new_index}"
end
puts "[IMPORT] done. Index: '#{new_index}' created."
end
end
end