我希望用户能够使用一个搜索框来搜索不同的对象。我会在结果页面上区分它们。两者都是全文搜索。
我是否应该制作一个搜索 Controller 并将所有内容加载到索引操作中,例如:
@posts = Post.all
@groups = Group.all
有些事情告诉我,这将是非常低效的。
我真的不知道从哪里开始,我还没有设法在互联网上找到任何解决这个问题的东西,但如果我忽略了一些事情,请告诉我。
谢谢
编辑:
这是我的搜索栏,可在我的网站上全局使用:
-form_tag posts_path, :method => :get do
=text_field_tag :search, params[:search], :id => 'searchfield'
=submit_tag '',:name => nil, :id => 'searchbutton'
它现在只搜索“Post”模型,并在 Post#index View 中显示结果
我希望能够在 Post 和 Group 表中搜索输入到搜索框中的查询,并将结果显示在同一页面上的两个单独的列中。也许通过搜索 Controller / View
最佳答案
如果你想同时搜索这两种类型,你可以使用这个表单:
# Pass your models in by class constant
Sunspot.search(Post,Group) do |s|
s.fulltext(params[:search])
end
这记录在 wiki 中:
https://github.com/sunspot/sunspot/wiki/Working-with-search#initiating-a-search
关于ruby-on-rails - 如何使用 Sunspot Solr 搜索多个表(模型)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8480841/