2016-07-09 32 views
0

時,你得到的所有結果我有使用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 

有一種情況我需要存儲所有的結果不是過濾後的結果通過@per_page變量filter_product_ids。有可能這樣做嗎?目前,只有結果@per_page纔有結果。

獲得所有沒有分頁的結果的目標是獲得用於各種產品分類的所有產品的uniq值,以便在網站上進行過濾。

感謝您的任何提示,米羅斯拉夫

回答

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查詢(沒有分頁PARAMS傳遞)的一種方法,然後在filtered_products.records上分頁。

0

剛剛從你的代碼中刪除

params[:page], @per_page

page: params[:page], per_page: @per_page,

+1

我仍然需要分頁結果/頁面,但同時我需要訪問所有記錄的數組(無分頁)。 – Miroslav