Search code examples
ruby-on-railssolrsunspotsunspot-railssunspot-solr

Sunspot Rails - Prevent Indexing on Save


By default, the sunspot solr gem issues an index command to the solr server as part of the save callback. This behavior is acceptable in most of my app, but there are some parts of it (especially those in rake tasks for bulk processing) where I want to save instances of my model without any interaction whatsoever with the solr server. How do I achieve this?


Solution

  • According to the docs I think you should add auto_index: false to your searchable block, i.e.:

    class Foo < ActiveRecord::Base
      searchable auto_index: false do
        # your search fields here
      end
    end
    

    Then you can use the following methods to manually reindex records:

    # On a class itself
    Person.reindex
    Sunspot.commit # or commit(true) for a soft commit (Solr4)
    
    # On mixed objects
    Sunspot.index [post1, item2]
    Sunspot.index person3
    Sunspot.commit # or commit(true) for a soft commit (Solr4)
    
    # With autocommit
    Sunspot.index! [post1, item2, person3]