require 'elasticsearch/rails/tasks/import'

namespace :elasticsearch do
  desc "Zero downtime reindexing
  $ rake environment elasticsearch:reindex CLASS='User' INDEX='employees_201402240920' ALIAS='employees FORCE=y'"
  task reindex: :environment do
    # TODO: check for arguments CLASS INDEX ALIAS FORCE
    aliaz = ENV["ALIAS"]
    index = ENV["INDEX"]

    client = Elasticsearch::Client.new
    has_alias = client.indices.exists_alias name: aliaz
    old_indices = has_alias ? client.indices.get_alias(name: aliaz).map {|k,v| k } : []

    # Creat new index
    Rake::Task["elasticsearch:import:model"].invoke

    client.indices.delete_alias(index: "*", name: aliaz) if has_alias
    client.indices.put_alias(index: index, name: aliaz)
    client.indices.delete(index: old_indices) if old_indices.present?
  end
end

require 'elasticsearch/rails/tasks/import'
Rake::TaskManager.record_task_metadata = true

namespace :elasticsearch do
  desc "Zero downtime re-indexing
  $ rake environment elasticsearch:reindex CLASS='Model_name' ALIAS='alias_name'"
  task reindex: :environment do |task|
    if ENV['CLASS'].blank? || ENV['ALIAS'].blank?
      puts "USAGE:"
      puts task.full_comment
      exit(1)
    end

    ENV["FORCE"] = 'y' # must be set to force mapping
    ENV['INDEX'] = "#{ENV['ALIAS']}_#{Time.new.strftime('%Y%m%d%H%M%S')}"

    client = Elasticsearch::Client.new
    has_alias = client.indices.exists_alias name: ENV["ALIAS"]
    old_indices = has_alias ? client.indices.get_alias(name: ENV["ALIAS"]).map {|k,v| k } : []

    # Creat new index, command line arguments are used
    Rake::Task["elasticsearch:import:model"].invoke

    client.indices.delete_alias(index: "*", name: ENV["ALIAS"]) if has_alias
    client.indices.put_alias(index: ENV['INDEX'], name: ENV["ALIAS"])
    client.indices.delete(index: old_indices) if old_indices.present?
  end
end

Check for arguments in Rake task and assign index name dynamically
Check for arguments in Rake task and assign index name dynamically
