Skip to content

Instantly share code, notes, and snippets.

@nz
Created August 8, 2011 21:30
Show Gist options
  • Save nz/1132809 to your computer and use it in GitHub Desktop.
Save nz/1132809 to your computer and use it in GitHub Desktop.
Sunspot - Retry search on partialResults
# Sample code using Sunspot 1.2.1 to retry a search when partialResults=true
#
# I hope this code will be somewhat deprecated in Sunspot 1.2.2, since we have to
# use instance_eval to access the otherwise inaccessible @solr_response instance variable.
#
class Post
# ...
searchable do
# your Sunspot search setup
end
# ...
def self.search(params={})
retry_search = true
search_count = 0
while retry_search && search_count < 2
search_count = search_count + 1
search = Post.solr_search do
# your search code here
end
solr_response = search.instance_eval("@solr_result")
retry_search = solr_response["responseHeader"]["partialResults"] == true
search
end
end
end
@robzon
Copy link

robzon commented Aug 18, 2011

I'm trying this with websolr on Heroku and I don't get responseHeader in solr_response. I only get keys: numFound, start, maxScore and docs. Is there another way to check if results are partial?

@nz
Copy link
Author

nz commented Aug 18, 2011

If you're not getting partialResults in the header, then you're all set. That means Solr had all the time it needed to search your full index.

@robzon
Copy link

robzon commented Aug 18, 2011

I don't get the header at all. And I'm pretty sure I get partial results, cause the results are blank and with the exact same query ran for the second time I get proper results.

@nz
Copy link
Author

nz commented Aug 18, 2011

You should fire up a ticket at http://help.websolr.com/ for something like that (sounds like replication latency to me)

@robzon
Copy link

robzon commented Aug 18, 2011

Ok, thanks, will do!

@brupm
Copy link

brupm commented Aug 26, 2011

robzon, any progress? I also don't get the header at all.

@robzon
Copy link

robzon commented Aug 29, 2011

Nope, filed a bug report but no response yet. As our database grew we now get first query with blank results 100% of time. For now I simply retry searching twice if there are no results and that works well (from user perspective). Will let you know when we get the proper fix.

@nz
Copy link
Author

nz commented Aug 29, 2011

Just updated this gist to correct an error. You should check solr_result, not solr_response, as per Sunspot's abstract_search.rb.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment