我在获取搜索结果分页与Elasticsearch、Tire和Kaminari一起工作时遇到了问题。
我正在搜索应用程序中的所有模型(新闻、绘画、书籍)作为一般站点搜索,因此,需要在站点控制器中为轮胎搜索设置一个块,以获得更细粒度的控件,例如显示超过默认的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在我的搜索结果页面中,我有以下代码:
- if @results
- @results.results.each do |result|
= result.title
#pagination
= paginate @results在我所有的模型中,我有正确的映射,包括来自轮胎的:
# - - - - - - - - - - - - - - - -
# 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我确保在Elasticsearch中所有条目都被正确地索引了。
我遇到的问题是我无法让它工作,最近的错误是:
未定义方法`current_page‘
任何想法都将不胜感激。谢谢。
发布于 2013-06-06 07:34:05
你能试试这个吗??当我使用per_page选项时,我遇到了类似的问题。于是,我转向了Tire提供的from大小选项。我不知道哪里出了问题。但是,显式地设置和使用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发布于 2013-03-20 07:23:04
轮胎有将分页宝石的方法,包括在它的库,所以我宁愿那样,而不是卡米纳里宝石。如果你仍然想和Kaminari在一起,收集轮胎搜索的结果。
@results = @results.all.to_hash
paginate @resultshttps://stackoverflow.com/questions/15366795
复制相似问题