Sunspot Index Only When Searchable Fields Change
Asked Answered
P

1

8

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?

Prosperity answered 18/5, 2011 at 6:1 Comment(1)
If you want to ensure login when Sunspot is down, take a look at github.com/bdurand/sunspot_index_queueJanitajanith
E
12

I'll assume you have the latest version of sunspot gem.

  1. 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

  2. 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

Empson answered 18/5, 2011 at 16:56 Comment(1)
Will resort to ignore_attributes of. Kind of annoying to have to specify this when we explicitly declare searchable fields. Do you forsee any complications if I were to create a patch which adds additional logic to the searchable.rb to query the sunspot setup to determine if the changed fields will affect the index?Prosperity

© 2022 - 2024 — McMap. All rights reserved.