Skip to content

Instantly share code, notes, and snippets.

@avinasha
Forked from mahemoff/elasticsearch.rake
Last active December 19, 2015 21:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save avinasha/6019602 to your computer and use it in GitHub Desktop.
Save avinasha/6019602 to your computer and use it in GitHub Desktop.
# 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 = [Ticket]
klasses.each do |klass|
puts "[Import] ::::::: Class: #{klass} :::::::::\n"
ENV['CLASS'] = klass.name
ENV['INDEX'] = new_index = klass.tire.index.name << '_' << Time.now.strftime('%Y%m%d%H%M%S')
puts "[Import] New index name #{new_index}"
Rake::Task["tire:import"].execute("CLASS='#{klass}'")
puts '[IMPORT] about to swap index'
if alias = Tire::Alias.find(klass.tire.index.name)
puts "[IMPORT] aliases found: #{alias.indices.to_ary.join(',')}. deleting..."
old_indices = alias.indices
old_indices.each do |index|
alias.indices.delete index
end
alias.indices.add new_index
alias.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
alias = Tire::Alias.new
alias.name(klass.tire.index.name)
alais.index(new_index)
alias.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