Skip to content

Instantly share code, notes, and snippets.

@nz
Created August 16, 2011 17:37
Show Gist options
  • Select an option

  • Save nz/1149651 to your computer and use it in GitHub Desktop.

Select an option

Save nz/1149651 to your computer and use it in GitHub Desktop.
Custom reindexing with Sunspot
# Custom reindexing with Sunspot
# Use your own finder, rescue and log exceptions, etc.
# Throw this in a rake task, or class method to run from the console
Article.find_in_batches(:batch_size => 100) do |articles|
articles.each do |article|
tries = 0
begin
article.solr_index
rescue RSolr::RequestError => e
puts "ERROR - #{e.message} on #{article.inspect}"
if (tries += 1) < 3
sleep 2 ** tries
retry
end
end
end
end
# Sample rake task to run the above
# 1. Customize the array of classes to reindex
# 2. Save in lib/tasks/solr.rake
# 3. Execute 'rake solr:reindex'
namespace :solr do
task :reindex => :environment do
classes_to_reindex = [ Foo, Bar ]
classes_to_reindex.each do |klass|
klass.find_in_batches(:batch_size => 100) do |records|
puts "Indexing a new batch..."
attempt = 0
begin
Sunspot.index(records)
rescue Exception => e
puts "Exception - #{e.message} - retrying..."
retry if (attempt += 1) < 2
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment