如何编写任务以重新索引Tire中的所有ES索引?

时间:2013-01-08 23:47:55

标签: ruby-on-rails elasticsearch tire

现在我有:

desc "Index Elastic Search"
namespace :tire do
  namespace :import do
    task all: :environment do
      aliases = Tire::Configuration.client.get(Tire::Configuration.url + '/_aliases').body
      indexes_names = MultiJson.load(aliases).keys

      indexes_names.each do |name|
        index = Tire::Index.new name
        index.delete
        index.import
        index.refresh
        puts "[INFO] #{name} re-indexed"
      end
    end
  end
end

但是我收到了错误

wrong number of arguments (0 for 1)
/Users/rege/.rvm/gems/ruby-1.9.3-p194@network/gems/tire-0.5.2/lib/tire/index.rb:185:in `import'
/Users/rege/Code/Network/lib/tasks/tire.rake:15:in `block (4 levels) in <top (required)>'
/Users/rege/Code/Network/lib/tasks/tire.rake:12:in `each'
/Users/rege/Code/Network/lib/tasks/tire.rake:12:in `block (3 levels) in <top (required)>'
Tasks: TOP => tire:import:all

1 个答案:

答案 0 :(得分:1)

您需要告诉每个索引要导入的内容。假设您使用默认的索引命名约定,那么您需要这样做:

index.import name.singularize.camelcase.constantize.all

<强>更新: 根据您的命名风格,您可以使用:

index.import name.gsub(/^myapp_(development|production)__/, '').singularize.camelcase.constantize.all