Created
July 21, 2013 08:21
-
-
Save sgringwe/6047913 to your computer and use it in GitHub Desktop.
Reindex elasaticsaerch (fixed from tire wiki page)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Run with: rake environment elasticsearch:reindex | |
# Begins by creating the index using tire:import:model command. This will create the "official" index name, e.g. "things" each time. | |
# Then we rename it to, e.g. "things_20121001052916" and alias "things" to it. | |
namespace :elasticsearch do | |
desc "re-index elasticsearch" | |
task :reindex => :environment do | |
klasses = [ | |
User, | |
Event, | |
Post | |
] | |
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} with alias #{klass.tire.index.name}" | |
Rake::Task["tire:import:model"].execute("CLASS='#{klass.name}'") | |
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 | |
puts "Using the name #{klass.tire.index.name} as the alias for #{new_index}" | |
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment