diff --git a/README.md b/README.md index efc50d4..f4043ab 100644 --- a/README.md +++ b/README.md @@ -398,8 +398,15 @@ Product.search "milk", load: false 3. Deploy and reindex + You can reindex a particular class: + + ```ruby + rake searchkick:reindex:class[Product] # or Product.reindex in the console + ``` + + Or you can reindex all models at the same time: ```ruby - rake searchkick:reindex CLASS=Product # or Product.reindex in the console + rake searchkick:reindex:all ``` 4. Once it finishes, replace search calls w/ searchkick calls diff --git a/lib/searchkick/tasks.rb b/lib/searchkick/tasks.rb index 084c45b..82f1201 100644 --- a/lib/searchkick/tasks.rb +++ b/lib/searchkick/tasks.rb @@ -2,8 +2,23 @@ require "rake" namespace :searchkick do desc "re-index elasticsearch" - task :reindex => :environment do - klass = ENV["CLASS"].constantize - klass.reindex + namespace :reindex do + desc "reindex a Model by passing it as an argument" + task :class, [:klass] => [:environment] do |t, args| + begin + args[:klass].constantize.reindex + rescue + puts "#{args[:klass]} model not found" + end + end + + desc "reindex all models" + task :all => [:environment] do + Dir[Rails.root + "app/models/**/*.rb"].each { |path| require path } + models = ActiveRecord::Base.descendants.map(&:name) + models.each do |model| + model.constantize.reindex if model.respond_to?(:search) + end + end end end -- libgit2 0.21.0