Sunspot Index Only When Searchable Fields Change
We are using sunspot for search in our latest project. We also use devise and are开发者_如何学运维 indexing our user model as follows:
searchable do
text :fname
text :lname
text :email
text :description
text :twitter_username
end
With this setup, a user cannot even log in unless solr is running. This implies that on every save of the User model, there is some communication to our solr server (reindexing?), even if none of the searchable fields have changed. Is this correct?
We also have lots of other models which are being indexed by sunspot which have non-searchable fields that are updated frequently. It appears as though sunspot is reindexing them on all of these updates.
Is there way to configure sunspot to only interface with solr when a searchable field changes?
I'll assume you have the latest version of sunspot gem.
Sunspot reindexes each time the model has changed. To tweak this behaviour (see options for searchable):
searchable :ignore_attribute_changes_of => [ :average_rating, :permalink ] do text :title end
Take a look on the source code: https://github.com/outoftime/sunspot/blob/master/sunspot_rails/lib/sunspot/rails/searchable.rb
Your site should still work until the first request to solr:
either a full text search request
either an indexing request
You are probably making an update on the user model each time a user logs in, therefore triggering a reindex.
More tips:
in fact, it's not the indexing that takes a lot of time, it's the commit command; the commit command is issued, by default, on the end of each web request ; This is very costly in a production env. I advice you to change the policy to autocommit after X seconds;
if you want that your want to patch sunspot so that you site should work in case of solr server failure, I advise you to take a look on this gem, that mocks server interface for the test env: sunspot_matchers
精彩评论