ezis
ezis

Reputation: 311

Tire + Will_Paginate not taking Tire.options

I'm trying to integrate Tire into my site and I'm having difficulty with pagination. I've tried paginating the results outside of the context of Tire and will_paginate is working on that Array. However, when I try will_paginate within the context of Tire I'm having one large problem.

Will_Paginate will display the correct number of pages with consideration of :per_page but when I click on that page the results are not loaded, rather they are the same as on the first page. The page number is highlighted in the will_paginate navigation.

@results.inspect yields this:

    #<Tire::Search::Search:0x007f88ab9153d0 @indices=["deja-set-development"], @types=[],        @options={:load=>true, :page=>1, :per_page=>2}, @path="/deja-set-development/_search", @query=#<Tire::Search::Query:0x007f88ab915088 @value={:query_string=>{:query=>"oh"}}>, @facets={"type"=>{:terms=>{:field=>:_type, :size=>10, :all_terms=>false}}}>

Here is where I call will_paginate:

= will_paginate @search_results.results, params

Here is where I iterate through the results

@search_results.results.each

Does anyone have any thoughts?

Edit ---

I'm not sure what is going on, but I did this and it is working.

@search_results = @search_results.paginate(:page => params[:page], :per_page => 5)

Upvotes: 1

Views: 629

Answers (2)

ezis
ezis

Reputation: 311

So to clarify, I've attached my github correspondence with @karmi here.

https://github.com/karmi/tire/issues/627#issuecomment-13449368

I was using Tire.search as opposed to searching by model. As @karmi notes, at the moment :per_page and :page are not supported with Tire.

Here is how I solved this:

    @search_results = Tire.search [:index1, :index2, :index3], :load => true, :from => from, :size => size  do
        query do
            string q, :default_operator => 'AND', :fields => [:name1, :name2]
        end
    end

I ended up having to spin my own small pagination system to increment 'size' and 'from'. Here's the elasticsearch link on the topic.

http://www.elasticsearch.org/guide/reference/api/search/from-size.html

You're able to still access

 = @search_results.results.total_entries/next_page/previous_page 

which helps with pagination.

Thank you again @karmi.

Upvotes: 0

karmi
karmi

Reputation: 14419

Please see the integration test in Tire, and make sure you're passing all options properly.

Upvotes: 1

Related Questions