Created
August 16, 2011 17:37
-
-
Save nz/1149651 to your computer and use it in GitHub Desktop.
Custom reindexing with Sunspot
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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