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

发布于 2024-12-21 02:36:42 字数 595 浏览 1 评论 0原文

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

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

@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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

难得心□动 2024-12-28 02:36:42

如果您想同时搜索这两种类型,您可以使用以下形式:

# 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 /使用搜索#启动搜索

If you want to search both types at once, you can use this form:

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

This is documented in the wiki:
https://github.com/sunspot/sunspot/wiki/Working-with-search#initiating-a-search

帅的被狗咬 2024-12-28 02:36:42

将 sunspot solr 中的可搜索指令添加到模型中以进行索引。例如:

class Post < ActiveRecord::Base
  searchable do
    text :title, :body
  end
end

class Group < ActiveRecord::Base
  searchable do
    text :name
  end
end

如果数据库中有现有数据,请确保运行 rake sunspot:solr:reindex 进行索引。对于新数据,索引将在钩子中完成。

现在您可以搜索:

@posts = Post.search {fulltext params[:search]}
@groups = Group.search {fulltext params[:search]}

现在您拥有两列的数据。

Add the searchable directive from sunspot solr to your models for indexing. For example:

class Post < ActiveRecord::Base
  searchable do
    text :title, :body
  end
end

class Group < ActiveRecord::Base
  searchable do
    text :name
  end
end

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:

@posts = Post.search {fulltext params[:search]}
@groups = Group.search {fulltext params[:search]}

Now you have the data for your two columns.

捂风挽笑 2024-12-28 02:36:42

这不是答案,但我发现了一些有用的东西。它是一个可以帮助您跨多个表进行搜索的宝石:

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文