Noz
Noz

Reputation: 6346

Rails 3 Sunspot Solr Conditional Search Only Applied To New Records

I'm trying to perform a query and find 20 records recently created across multiple models using the sunspot_solr gem. Currently it achieves that no problem, but I wanted to add some extra validations for only one of the models that were being searched.

Currently in my User model it lists all new users that were created, but I only want it to list users who have confirmed their email address. I'm using Devise for user authentication and using active record to find all users who have not been confirmed I would do something along the lines of:

User.where("confirmed_at is null")

Or the reverse to test for the opposite. I tried to integrate this with my solr search as so...

Model

#Unless the user being searched hasn't been confirmed...
searchable :unless=> proc { |user| user.confirmed_at == nil } do
    time :created_at
    time :confirmed_at
  end 

Controller

@updates = Sunspot.search(Upload,Help,User) do    
      without(:confirmed_at, nil)
      order_by(:created_at, :desc)
      paginate page: 1, per_page: 20
end

However this added condition only eliminated user records created after the condition was added to the model. I'm not completely sure how indexing works, but it seems to be caching old information where the newely added conditions weren't being applied. I can confirm this by creating a new user record and I notice immediately it doesn't get indexed until I validate the email address.

What I've tried:

  1. Stopping and restarting server
  2. Stopping server and attempted to stop and restart solr but this produced a PID could not be found error

Upvotes: 0

Views: 927

Answers (1)

Noz
Noz

Reputation: 6346

After reading up a little more on indexing I confirmed my suspicions and found out that I had to re-index my search. To do this I looked at Sunspot readme and found this line:

bundle exec rake sunspot:solr:reindex

I have a bad habit of trying to make use of technologies before I even understand what they do...

Upvotes: 1

Related Questions