如何使用 Sunspot 设置具有多对多关系的构面搜索?

发布于 2024-08-26 15:02:51 字数 896 浏览 13 评论 0原文

我之前没有实现过搜索功能,感觉有点卡住。我有一个太阳黑子搜索功能,可以根据关键字查找结果 - 这很好用 - 但我现在想实现多选方面功能,但我什至不知道如何设置基本的方面搜索。

我有一个多对多的关系(在 Rails 中而不是在现实生活中):

Class People has_many :skills, :through =>经验(反之亦然等)

Class People < ActiveRecord::Base
  has_many :skills, :through => experience

  searchable do
    text :first_name, :surname
  end
end

在控制器中

@search = Sunspot.search(People)
   facet :skill_ids
end

这是我无法工作的基本示例。它会生成以下错误:

Sunspot::UnrecognizedFieldError: No field configured for People with name 'skill_ids'

如何创建指向 :skill_ids 的链接

我想我一定在模型中缺少一些引用 - 但我找不到任何示例来引用关系的 Id。我发现的大多数示例在使用构面功能时都使用该模型中已有的列。

  • 我怎样才能让基本的实施工作正常进行?
  • 我将如何在视图中使用它 - 我是否必须调用 hit.facet 并迭代技能?显示此内容的代码会是什么样子?
  • 我如何选择多个方面进行搜索?
  • 我应该将其放入社区维基吗?

谢谢您的宝贵时间!

I haven't implemented a search feature before and feel a bit stuck. I have a Sunspot search feature which finds results based on keywords - this works great - but I now want to implement the multi select facet feature, but I can't even seem to figure out how to set-up a basic facet search.

I have a many to many relationship (in rails not in real life):

Class People has_many :skills, :through => experience (and vice versa etc)

Class People < ActiveRecord::Base
  has_many :skills, :through => experience

  searchable do
    text :first_name, :surname
  end
end

In the controller

@search = Sunspot.search(People)
   facet :skill_ids
end

This is the basic example I can't get working. It generates this error:

Sunspot::UnrecognizedFieldError: No field configured for People with name 'skill_ids'

How do I create the link to :skill_ids

I think I must be missing some reference in the model - but no examples I can find do reference the Ids of a relationship. Most of the examples I found use columns that are already in that model when using the facet functionality.

  • How can I get the basic implementation working?
  • How would I use this in the view - do I have to call hits.facet and iterate over the skills? What would the code look like to display this?
  • How would I select multiple facets to search by?
  • Should I put this in the community wiki?

Thank you for your time!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

安静被遗忘 2024-09-02 15:02:51

任何您想要过滤、分面或订购的内容,太阳黑子都需要了解。所以在你的模型中:

searchable do
  text :first_name, :surname
  integer :skill_ids, :multiple => true, :references => Skill
end

你的控制器中的 #search 调用看起来是正确的。在您看来,您应该按照以下方式执行操作:

- @search.facet(:skill_ids).rows.each do |row|
  = row.instance.name

row.instance 将返回该行值引用的 Skill 实例(这就是 :references 选项的作用)在 searchable 定义中执行)。

我不确定“选择多个方面进行搜索”是什么意思 - 通过调用方面可以生成多个方面(为用户提供进一步搜索细化的选择)一次搜索中多次使用code>方法;然后,您可以使用 with 方法在范围限制下使用它们的方面选择,您也可以根据需要多次调用该方法。

说到 wiki,大部分信息都可以在 Sunspot wiki 中找到(有更多解释):

Anything you want to filter, facet, or order on, Sunspot needs to know about. So in your model:

searchable do
  text :first_name, :surname
  integer :skill_ids, :multiple => true, :references => Skill
end

Your #search call in your controller looks right. In your view, you'd do something along these lines:

- @search.facet(:skill_ids).rows.each do |row|
  = row.instance.name

row.instance will return the instance of Skill that the row's value refers to (that's what the :references option is doing in the searchable definition).

I'm not sure what you mean by "select multiple facets to search by" -- one can generate multiple facets (which give users choices for further search refinement) by calling the facet method multiple times in a search; and you can then use their facet choices with scope restrictions using the with method, which you can also call as many times as you'd like.

Speaking of wikis, most of this information is available (with more explanation) in the Sunspot wiki:

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