如何在Sunspot Solr中使用will_paginate

nfr*_*d21 0 solr will-paginate sunspot-rails

我的问题是非常具体的。我有一个来自Sunspot查询的结果集合,

  @search = Product.search do
    with(:website_id, id)
    with(:archived_at, nil)
    with(:category_id, restrictive_category_ids)
    fulltext search_string do
      phrase_fields(:name => 1, :artist => 3, :tag_list => 2)
      minimum_match 0 #enables search to be performed by each word, not the whole phrase.
    end
    paginate :page => 1, :per_page => 2
  end

  @products = @search.results
Run Code Online (Sandbox Code Playgroud)

这将产生几页结果,每页2张。所有这些都已在Sunspot控制台中进行了验证。

我的问题是尝试将will_paginate用于结果。例如,在视图中:

<%= will_paginate @products, param_name: 'products', class: 'pagination-nav' %>
Run Code Online (Sandbox Code Playgroud)

由于@products在初始查询中仅返回2个结果,因此will_paginate认为只有2个结果,因此甚至不会出现。如果将@products替换为Product.all,则它显然会出现并显示结果页面。

因此,我不确定要告诉will_paginate实际有多少结果,因此不确定要显示多少页。当然,我可以对所有这些记录进行单独的查询,但这将对应用程序造成重大影响,并且鉴于Sunspot声称可以与will_paginate一起使用,我认为必须有一种更简单的方法。

谢谢你的帮助!

nfr*_*d21 5

我找到了答案。

Instead of using @search.results within the "will_paginate" method in the view, I needed to use @search.hits.

using "hits" is key. "results" only returns the quantity of results from the paginated aspect in the model.search method. "hits", on the other hand, provides an array of records that reflects the quantity of the entire search, therefore allowing the paginated results to work properly.