Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting number of hits without pagination in sunspot 1.2 on ruby on rails 3

I've successfully installed Sunspot for my Ruby On Rails 3 project, but I can't seem to find a way to get the total hits for a search query.

this is my search request

@search = Sunspot.search(Job) do
    fulltext params[:job]
    paginate(:page => params[:offset], :per_page => 25)
end

It works well except I need to get total number of real hits, not the total results returned (in this case 25 because of :per_page => 25)

In other words, I want to be able to display: Showing 1 to 25 out of 883 jobs found

Any help would be appreciated!

thanks

like image 265
Farbour Avatar asked Dec 10 '22 12:12

Farbour


2 Answers

The method total works here.

query_results = Sunspot.search(Recipe) do
  keywords(params[:qs])
  paginate(:page=>params[:page], :per_page=>30)
end

@search_results = query_result.results
@search_total = @search_results.total

Or, in your view, total_entries works on the results object.

    %div
      Your search for 
      = params[:qs]
      returned
      = pluralize(@search_results.total_entries, 'result')
like image 133
Jesse Wolgamott Avatar answered Dec 29 '22 01:12

Jesse Wolgamott


Never used Sunspot, but have you tried this in your view:

Showing #{@search.hits.page} to #{@search.hits.per_page} out of #{@search.total} jobs found
like image 37
hade Avatar answered Dec 29 '22 00:12

hade