简体   繁体   English

与Elasticsearch,Tire和Kaminari分页

[英]Pagination with Elasticsearch, Tire, and Kaminari

I'm having issues getting pagination of search results to work with Elasticsearch, Tire, and Kaminari. 我遇到了将搜索结果分页以与Elasticsearch,Tire和Kaminari合作的问题。

I am searching on all models in my application (news, paintings, books) as a general site search and therefore, need a block for the tire search, in my site controller for more fine grained control, such as showing more than the default of 10 entries: 我正在搜索我的应用程序中的所有模型(新闻,绘画,书籍)作为一般网站搜索,因此,需要一个轮胎搜索块,在我的站点控制器中进行更细粒度的控制,例如显示超过默认值10个条目:

class SiteController < ApplicationController
  def search
    # @results = Painting.search(params[:query])
    query = params[:query]
    @results = Tire.search ['news','paintings', 'books'], :page => (params[:page]||1), :per_page=> (params[:per_page] || 3) do
      query { string query }
      size 100
    end
  end
end

In my search results page, I have the following code: 在我的搜索结果页面中,我有以下代码:

- if @results
  - @results.results.each do |result|
    = result.title
#pagination
  = paginate @results

and in all my models, I have the proper mapping and includes from tire: 在我的所有模型中,我有适当的映射,包括轮胎:

  # - - - - - - - - - - - - - - - - 
  # Elasticsearch
  # - - - - - - - - - - - - - - - - 
  include Tire::Model::Search
  include Tire::Model::Callbacks

  mapping do
    indexes :id, index: :not_analyzed
    indexes :title, boost: 100
    indexes :slug, boost: 100, as: 'slug'
    indexes :content
    indexes :image, as: 'image.thumb.url'
    indexes :tags, as: 'tags'
    indexes :gallery_name, as: 'gallery.name'
    indexes :created_at, :type => 'date'
  end

I ensured all my entries are indexed properly in Elasticsearch. 我确保所有条目都在Elasticsearch中正确编入索引。

The issue I'm having is I can't get it to work, the latest error is: 我遇到的问题是我无法让它工作,最新的错误是:

undefined method `current_page' 未定义的方法`current_page'

Any thoughts would be greatly appreciated. 任何想法将不胜感激。 Thank you. 谢谢。

Can you try this??? 你能试试这个??? When I used the per_page option, I had similar issue. 当我使用per_page选项时,我遇到了类似的问题。 So, I shifted to the from size options provided by Tire. 所以,我转向了轮胎提供的尺寸选项。 I'm not sure what went wrong. 我不确定出了什么问题。 But, explicitly setting and using from and size did the trick for me... 但是,明确设置和使用from和size对我来说是个窍门......

class SiteController < ApplicationController
  def search
    # @results = Painting.search(params[:query])
    options = { :page => (params[:page] || 1), :size => 100 }
    query = params[:query]
    @results = Tire.search ['news','paintings', 'books'], options do
      query { string query }
      from options[:size].to_i * (options[:page].to_i-1)
    end
  end
end

Tire has method for Will-paginate gem included in its lib so I would prefer that rather than Kaminari gem. Tire有包含在其lib中的Will-paginate gem的方法,所以我更喜欢它而不是Kaminari gem。 If in any case you still want to be with Kaminari, collect the results of tire search. 如果在任何情况下你仍然想和Kaminari在一起,请收集轮胎搜索的结果。

@results = @results.all.to_hash

paginate @results

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM