如何使用 Sunspot Solr 搜索多个表(模型)?
我希望用户能够使用一个搜索框来搜索不同的对象。我会在结果页面上区分它们。两者都是全文搜索。
我是否应该创建一个搜索控制器并将所有内容加载到索引操作中,例如:
@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 视图上显示结果
我希望能够在搜索框中输入查询帖子和组表,并且结果显示在同一页面上的两个单独的列中。也许通过搜索控制器/视图
I want users to be able to use one search box to search for different objects. I would differentiate them on the results page. Both would be full text search.
Should I make a Search controller and load everything into the index action with something like:
@posts = Post.all
@groups = Group.all
Something tells me that would be fantastically inefficient.
I'm not really sure where to start, I haven't managed to find anything addressing this question on the interwebs, but if I have overlooked something let me know.
Thanks
EDIT:
here's my search bar that is available globally on my website:
-form_tag posts_path, :method => :get do
=text_field_tag :search, params[:search], :id => 'searchfield'
=submit_tag '',:name => nil, :id => 'searchbutton'
it only searches the "Post" model right now, and displays the results on the Post#index view
I want to be able to have queries typed into the search box be searched for in both the Post and Group tables, and the results be displayed in two separate columns on the same page. maybe through a search controller/view
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您想同时搜索这两种类型,您可以使用以下形式:
这在 wiki 中有记录:
https://github.com/sunspot/sunspot/wiki /使用搜索#启动搜索
If you want to search both types at once, you can use this form:
This is documented in the wiki:
https://github.com/sunspot/sunspot/wiki/Working-with-search#initiating-a-search
将 sunspot solr 中的可搜索指令添加到模型中以进行索引。例如:
如果数据库中有现有数据,请确保运行 rake sunspot:solr:reindex 进行索引。对于新数据,索引将在钩子中完成。
现在您可以搜索:
现在您拥有两列的数据。
Add the searchable directive from sunspot solr to your models for indexing. For example:
If you have existing data in DB make sure to run
rake sunspot:solr:reindex
for indexing. For new data the indexing will be done in a hook.Now you can search:
Now you have the data for your two columns.
这不是答案,但我发现了一些有用的东西。它是一个可以帮助您跨多个表进行搜索的宝石:
https://github.com/toptierlabs/acts_as_fulltextable 。
它可以帮助您将所有可搜索数据(来自不同模型)保存在一处。
This is not the answer, but I found something useful. It is a gem that helps you searching across multiple tables:
https://github.com/toptierlabs/acts_as_fulltextable.
It helps you keeping all the searchable data (from different models) in one place.