I have a model which deploys a delayed job that updates some of its attributes. The model is declared "searchable"...
searchable do
text :content, :stored => true
end
... which I thought would re-index after a save. On testing, this doesn't seem to be the case. If I run: rake sunspot:reindex
, then everything works as expected. What could be causing this issue?
I had the exact same problem as you - when I was testing my search functionality sunspot would never issue a commit to solr. If I manually call Sunspot.commit everything works. I fiddled around with auto_commit_after_request, but this is true by default so it shouldn't make a different.
So after some more investigation I found that Sunspot won't issue a commit automatically unless the change is made in the context of a web request. If you're doing a change from a test or a background job you have to call Sunspot.commit manually.
As mentioned by Jason, you can call
Sunspot.commit_if_dirty
to issue a commit from your client.From the server configuration side, another approach would be to set the
autoCommit
property in yoursolrconfig.xml
to automatically issue commits when there have been changes made to your index. AmaxTime
of 60000 ms (one minute) should suffice for most sites.Using
autoCommit
is probably the wiser choice in production applications, where a high volume of commits can easily impact your Solr server's performance. In fact, it's a good practice with Sunspot to disable itsauto_commit_after_request option
when your site starts getting a decent amount of updates.Lastly,
autoCommit
has the advantage of being able to set it and forget it.At Websolr, our default is to ignore client-issued commits in favor of
autoCommit
.The index will only reflect changes after
Sunspot.commit
is called. This happens automatically when you runrake sunspot:reindex
.Sunspot's Rails plugin also has a
auto_commit_after_request
config option which will callSunspot.commit_if_dirty
after every request but this will not be triggered by your background processes.Your best bet is to call
Sunspot.commit_if_dirty
after as the last thing in your delayed job.