使用分页时,Searchkick会获得所有结果

时间:2016-07-09 21:42:54

标签: ruby-on-rails searchkick

我使用searchkick对产品进行了以下搜索查询:

def index
  @filter = params[:filter].blank? ? nil : Search.find(params[:filter])

  if @filter.present?
      @products = @filter.products(set_order_column(@sort), @direction, params[:page], @per_page)
  else
      query = @query.presence || "*"

      @products = Product.search(
        query, 
        where: {
          active: true
        }, 
        page: params[:page], 
        per_page: @per_page, 
        order: [{
          "#{set_order_column(@sort)}" => "#{@direction}"
        }],
        misspellings: { distance: 2 },
        fields: fields)
  end

  @filter_product_ids = @products.map(&:id)
end

有变量filter_product_ids,我需要存储所有结果,而不是@per_page过滤结果。有可能吗?现在,结果只有结果@per_page。

无需分页即可获得所有结果的目标是获取用于各种产品分类的所有产品的uniq值,以便在网站上进行过滤。

感谢您提供任何提示,Miroslav

2 个答案:

答案 0 :(得分:2)

我的解决方案是将它放在它自己的类(服务类)中,然后

class ProductFilter
  attr_reader :search_params, :search_query, :pagination_params

  def initialize(search_params, search_query, pagination_params)
    @search_params = search_params
    @search_query = search_query
    @pagination_params = pagination_params
  end

  # Will return all records from searchkick, unpaginated
  def filtered_products
    Product.search(search_query, search_param_stuff)
  end

  # Will return all filtered_products & then paginate on that object
  def paginated_products
    filtered_products.records.paginate(pagination_params)
  end

  private
  def search_param_stuff
    search_params
  end
end

在任何情况下,searchkick的分页方法实际上都适用于searchkick记录对象,因此您只需在一个方法中提取ES查询(不传递分页参数),然后在filtered_products.records上分页。

答案 1 :(得分:0)

只需删除

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <div id="TBL1" class="ShipEst"> <table align="right" style="width: 100%"> <tbody> <tr> <td align="left"> <b>Subtotal: </b> </td> <td align="center"></td> <td align="right"> <nobr>25.00</nobr> </td> </tr> <tr> <td align="left"> <b>Shipping: </b> </td> <td align="center"></td> <td align="right"> <nobr>0.00</nobr> </td> </tr> </tbody> </table> </div> <div id="myDiv"> Hide this if td contains "Shipping" </div>

params[:page], @per_page

来自您的代码