Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sunspot Solr fulltext search and will_paginate

The following query is working like a charm:

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

The problem I'm having is with pagination. Here is the same query with pagination:

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

I have 11 Style records.

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

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

[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 the point was to paginate the search results, not the actual records in their entirety

What's going on here, and how do I fix it?

EDIT

Ok, let me try explaining this another way. Let's say I have these six records:

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

Let's say I try to search for '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 is [6 => 'f']

like image 314
Abram Avatar asked Dec 11 '22 12:12

Abram


2 Answers

my thoughts:

get the results from solr then search your model by ids that solr returned and paginate them, 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 docs: I didn't tried it

By default, Sunspot requests the first 30 results from Solr. That means you can have 100 of records that might match the searching criteria but you'll see only the first 30, to see the others you have to add the paginate to your solr search, like:

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

in this case, you should be able to access 2 page. 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 in one page, or paginate :page => 2, :per_page => 50 and results should be divided in 2 pages with max 50 results each.

like image 142
rmagnum2002 Avatar answered Dec 28 '22 01:12

rmagnum2002


Try supply a fallback to per_page like so:

@search = Style.search do
  fulltext params[:q]
  paginate(page: params[:page], per_page: (params[:per] || 15))
end
like image 31
mathieugagne Avatar answered Dec 28 '22 01:12

mathieugagne