Sunspot Solr full-text search and will_paginate

The following query works like a charm:

@styles = Style.search { fulltext params[:q] } 

The problem I am facing is pagination. Here is the same pagination request:

 @styles = Style.search { fulltext params[:q]; paginate :page => params[:page], :per_page => params[:page_limit] } 

I have 11 Style entries.

If I have :page => 1 and :per_page => 10 , when I search for the 11th entry, I get an empty array returned for @styles.results

If I set :page=>2 and do the same search, I get the 11th style entry.

 [11] pry(#<StylesController>)> params[:page]=2 => 2 [12] pry(#<StylesController>)> x=Style.search {fulltext params[:q]; paginate :page => params[:page], :per_page => params[:page_limit] } => <Sunspot::Search:{:fq=>["type:Style"], :q=>"hel", :fl=>"* score", :qf=>"name_textp full_name_textp", :defType=>"dismax", :start=>10, :rows=>10}> [13] pry(#<StylesController>)> x.results => [#<Style id: 15...>] 

I thought it was about breaking up the search results, not the actual records in general

What is going on here and how to fix it?

EDIT

Well, let me try to explain it in a different way. Let's say I have these six entries:

 1 => 'a' 2 => 'b' 3 => 'c' 4 => 'd' 5 => 'e' 6 => 'f' 

Say I'm trying to find 'f'

 Letter.search { fulltext 'f'; paginate :page => 1, :per_page => 5 } 

My result will be an empty array []

Now let's say i try

 Letter.search { fulltext 'f'; paginate :page => 1, :per_page => 6 } 

Now my result [6 => 'f']

+4
source share
4 answers

my thoughts:

Get the results from solr, then search your model using the identifiers that solr returned and paginated, something like this:

 @search = Sunspot.search(Snippet) do fulltext params[:search] end @styles = Style.where(id: @search.results.map(&:id)).page(params[:page]).per(5) 

what I understood from the docs: I have not tried it

By default, Sunspot requests the first 30 Solr results. This means that you can have 100 entries that can match the search criteria, but you will only see the first 30 to see the rest, which you must add to the directory to search in solr, for example:

 search = Style.search do fulltext "my cool style" paginate :page => 2 end 

in this case you must have access to 2 pages. To update the number of sunspot requests, you need to write:

 search = Style.search do fulltext "my cool style" paginate :page => 1, :per_page => 50 end 

it should give you 50 results on one page or paginate :page => 2, :per_page => 50 , and the results should be divided into 2 pages with a maximum of 50 results.

+9
source

Try putting the backup on per_page as follows:

 @search = Style.search do fulltext params[:q] paginate(page: params[:page], per_page: (params[:per] || 15)) end 
+2
source

I also ran into the same problem. So I removed: per_page tag in my search

And changed my request as

 @styles = Style.search { fulltext params[:q]; paginate :page => params[:page]} 

To set per_page_limit, I used the following code in my initializers

 Sunspot.config.pagination.default_per_page = 20 
+1
source

You can use the "hits" method.

In the controller

 @search = User.search do with :name, 'joe' end 

In view

 <% @search.results.each do |result| %> <%= result.name %> <% end %> <%= will_paginate @search.hits unless @search.nil? %> 
+1
source

Source: https://habr.com/ru/post/1493276/


All Articles