-
-
Save nz/1132809 to your computer and use it in GitHub Desktop.
# 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 |
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.
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.
You should fire up a ticket at http://help.websolr.com/ for something like that (sounds like replication latency to me)
Ok, thanks, will do!
robzon, any progress? I also don't get the header at all.
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.
Just updated this gist to correct an error. You should check solr_result
, not solr_response
, as per Sunspot's abstract_search.rb.
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?