Commit 2e5aa58ef6993c95bf07643c1bf30dc7ae5ff008

Authored by Andrew Kane
2 parents cacccb43 05ab5feb

Merge branch 'reindexing' of https://github.com/mehulkar/searchkick into mehulkar-reindexing

.gitignore
... ... @@ -16,3 +16,4 @@ test/tmp
16 16 test/version_tmp
17 17 tmp
18 18 *.log
  19 +.DS_Store
... ...
README.md
... ... @@ -398,8 +398,15 @@ Product.search "milk", load: false
398 398  
399 399 3. Deploy and reindex
400 400  
  401 + You can reindex a particular class:
  402 +
  403 + ```ruby
  404 + rake searchkick:reindex:class[Product] # or Product.reindex in the console
  405 + ```
  406 +
  407 + Or you can reindex all models at the same time:
401 408 ```ruby
402   - rake searchkick:reindex CLASS=Product # or Product.reindex in the console
  409 + rake searchkick:reindex:all
403 410 ```
404 411  
405 412 4. Once it finishes, replace search calls w/ searchkick calls
... ...
lib/searchkick/tasks.rb
1 1 require "rake"
2 2  
3 3 namespace :searchkick do
4   - desc "re-index elasticsearch"
  4 + desc "re-index elasticsearch backwards compatibility task"
5 5 task :reindex => :environment do
6 6 klass = ENV["CLASS"].constantize
7 7 klass.reindex
8 8 end
  9 +
  10 + desc "re-index elasticsearch backwards compatibility task"
  11 + namespace :reindex do
  12 + desc "reindex a Model by passing it as an argument"
  13 + task :class, [:klass] => [:environment] do |t, args|
  14 + begin
  15 + args[:klass].constantize.reindex
  16 + rescue
  17 + puts "#{args[:klass]} model not found"
  18 + end
  19 + end
  20 +
  21 + desc "reindex all models"
  22 + task :all => [:environment] do
  23 + Dir[Rails.root + "app/models/**/*.rb"].each { |path| require path }
  24 + models = ActiveRecord::Base.descendants.map(&:name)
  25 + models.each do |model|
  26 + model.constantize.reindex if model.respond_to?(:search)
  27 + end
  28 + end
  29 + end
9 30 end
... ...