Ruby on rails 如何使用sunspot仅搜索已批准的文章
我有一个默认状态为“等待”的文章模型,在我的文章模型中我有:Ruby on rails 如何使用sunspot仅搜索已批准的文章,ruby-on-rails,ruby,search,ruby-on-rails-4,sunspot,Ruby On Rails,Ruby,Search,Ruby On Rails 4,Sunspot,我有一个默认状态为“等待”的文章模型,在我的文章模型中我有: scope :awaiting, -> {where(status: "Awaiting") } scope :approved, -> {where(status: "Approved") } scope :rejected, -> {where(status: "Rejected")} 为了只允许搜索已批准的文章,我的文章模型中有以下内容: searchable do text :title
scope :awaiting, -> {where(status: "Awaiting") }
scope :approved, -> {where(status: "Approved") }
scope :rejected, -> {where(status: "Rejected")}
为了只允许搜索已批准的文章,我的文章模型中有以下内容:
searchable do
text :title
string :status
end
这是我的搜索控制器
def search
@search = Sunspot.search(Article) do
fulltext params[:query]
with(:status, "Approved")
order_by :score, :desc
paginate(page: params[:page], per_page: 10)
end
@articles = @search.results
respond_to do |format|
format.html { render :action => "search" }
end
end
但是当我试图搜索任何东西时,这都不会产生结果,为什么会这样?如果您的模型在添加太阳黑子之前已经存在,那么您需要使用
bundle exec rake Sunspot:solr:reindex
创建索引
未来的更新应自动添加到索引中。您是否为模型编制了索引:
bundle exec rake sunspot:solr:reindex
?谢谢,它现在已经工作了