2013-01-08 63 views
0

現在我有:如何在Tire中編寫重建索引所有ES索引的任務?

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/[email protected]/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

你需要告訴每個索引要導入的內容。假設你使用默認的索引命名約定,那麼你需要做的是:

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

UPDATE: 考慮您的命名風格,您可以使用此:

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

我每個索引的名字前有我的應用程序和環境的名稱:''myapp_development__links''所以你的解決方案給我:''未初始化的常量MyAppDevelopmentLink'' – tomekfranek

+0

正如預期的那樣,請看我更新的答案。 –

+0

我也加了''test''嗯,但我想知道是否正確的方法來做到這一點? – tomekfranek