Created
July 16, 2012 20:37
-
-
Save jarosan/3124884 to your computer and use it in GitHub Desktop.
Elasticsearch reindex task
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 | |
namespace :elasticsearch do | |
desc "re-index elasticsearch" | |
task :reindex => :environment do | |
klass = Place | |
ENV['CLASS'] = klass.name | |
ENV['INDEX'] = new_index = klass.tire.index.name << '_' << Time.now.strftime('%Y%m%d%H%M%S') | |
Rake::Task["tire:import"].invoke | |
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 | |
end | |
puts "[IMPORT] done. Index: '#{new_index}' created." | |
end | |
end |
There is a bug in line 9 which mutates the current index name in place. It can be fixed by using interpolation:
ENV['INDEX'] = new_index = "#{klass.tire.index.name}_#{Time.now.strftime('%Y%m%d%H%M%S')}"
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@phiggins I think it's passed through by
import_model
viaparams[:index]
notindex
.