Sunspot Rails - Prevent Indexing on Save

1.6k views Asked by At

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?

2

There are 2 answers

0
zrl3dx On BEST ANSWER

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]
1
Maurizio In denmark On

To disable it add auto_commit_after_request: false to your sunspot.yml.