如何使用Sunspot Solr搜索多个表(模型)?

Bri*_*ian 2 search solr ruby-on-rails sunspot ruby-on-rails-3

我希望用户能够使用一个搜索框来搜索不同的对象.我会在结果页面上区分它们.两者都是全文搜索.

我应该创建一个搜索控制器并将所有内容加载到索引操作中,例如:

@posts = Post.all
@groups = Group.all
Run Code Online (Sandbox Code Playgroud)

有些东西告诉我,这将是非常低效的.

我不确定从哪里开始,我还没有设法在互联网上找到任何解决这个问题的东西,但是如果我忽略了某些东西让我知道.

谢谢

编辑:这是我的搜索栏,可在我的网站上全球使用:

    -form_tag posts_path, :method => :get do
     =text_field_tag :search, params[:search], :id => 'searchfield'
     =submit_tag '',:name => nil, :id => 'searchbutton'
Run Code Online (Sandbox Code Playgroud)

它现在只搜索"Post"模型,并在Post#index视图中显示结果

我希望能够在Post和Group表中搜索搜索框中输入的查询,并将结果显示在同一页面上的两个单独的列中.也许通过搜索控制器/视图

kno*_*wuh 6

如果要一次搜索两种类型,可以使用以下格式:

# Pass your models in by class constant
Sunspot.search(Post,Group) do |s| 
    s.fulltext(params[:search])
end
Run Code Online (Sandbox Code Playgroud)

这在wiki中有记录:https: //github.com/sunspot/sunspot/wiki/Working-with-search#initiating-a-search