Skip to content

Instantly share code, notes, and snippets.

@lassebunk
Last active July 24, 2020 04:10
Show Gist options
  • Select an option

  • Save lassebunk/107b4406aacfac85e42b to your computer and use it in GitHub Desktop.

Select an option

Save lassebunk/107b4406aacfac85e42b to your computer and use it in GitHub Desktop.

Revisions

  1. lassebunk revised this gist Jul 17, 2014. 1 changed file with 4 additions and 1 deletion.
    5 changes: 4 additions & 1 deletion sunspot_tasks.rake
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,7 @@
    # This task can be used to not drop your Solr index before reindexing it.
    # This task can be used reindex Solr without dropping your index first.
    # This is ideal for production environments where a live, working index is critical.
    #
    # Put this in lib/tasks/sunspot_tasks.rake

    namespace :sunspot do
    task reindex_gracefully: :environment do
  2. lassebunk created this gist Jul 17, 2014.
    24 changes: 24 additions & 0 deletions sunspot_tasks.rake
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,24 @@
    # This task can be used to not drop your Solr index before reindexing it.

    namespace :sunspot do
    task reindex_gracefully: :environment do
    Dir.glob(Rails.root.join('app/models/**/*.rb')).each { |path| require path }
    sunspot_models = Sunspot.searchable

    index_options = { :batch_commit => false }

    begin
    require 'progress_bar'
    total_documents = sunspot_models.map { | m | m.count }.sum
    index_options[:progress_bar] = ProgressBar.new(total_documents)
    rescue LoadError => e
    $stdout.puts "Skipping progress bar: for progress reporting, add gem 'progress_bar' to your Gemfile"
    rescue Exception => e
    $stderr.puts "Error using progress bar: #{e.message}"
    end

    sunspot_models.each do |model|
    model.solr_index index_options
    end
    end
    end