具有多种模型的太阳黑子(Rails 中) [英] Sunspot with multiple models (in Rails)

查看:46
本文介绍了具有多种模型的太阳黑子(Rails 中)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有三个模型(UserTagProduct)并且它们相互交互.User 有很多 TagsProducts.

I have three models (User, Tag, Product) and they interact s.t. User has many Tags and Products.

出于搜索目的,我希望能够(使用一个搜索栏)搜索用户名、标签名和产品描述.我还想在产品页面上进行搜索,但这仅与标签名称和产品描述相关.

For searching purposes, I would like to be able to search (with one search bar) on user names, tag names, and product descriptions. I would also like to search on product pages, but that is only relevant for tag names and product descriptions.

这里有两个例子:

搜索:Linus Torvalds"返回三个模型中 Linus Torvalds 的所有实例,其中用户名的任何实例都放在更高的位置.

Search: "Linus Torvalds" returns all instances of Linus Torvalds in the three models with any instances of the user name being placed higher.

搜索:Linux"和年龄:20-25"返回产品名称/描述中包含Linux"并属于该年龄段的用户的所有实例,以及标签包含Linux"的用户以及谁拥有该年龄范围内的产品.请注意,如果搜索不包括年龄,那么它将默认为所有适合Linux"部分的人,而不是没有.

Search: "Linux" with age: "20-25" returns all instances of Users with Products that include "Linux" in their name/description and fall in that age range, as well as Users with tags that include "Linux" and who have Products that fall in that age range. Note that if the search didnt include the age, then it would default to all who fit the "Linux" part rather than none.

我的问题是这样做的最佳方法是什么?我应该用自己的控制器创建一个搜索模型吗?我应该忽略它并在共享文件夹中包含搜索部分吗?还有哪些方法?

My question is what would be the best way of doing this? Should I create a search model with its own controller? Should I just ignore that and include a search partial in a shared folder? What other methods are there?

非常感谢.

推荐答案

如果您正在处理任何复杂的条件,我喜欢使用 Search 对象的想法.

I like the idea of a Search object if you are doing any complicated conditions.

但是要使用 Sunspot 搜索对象:

But to search across objects using Sunspot:

@sunspot_search = Sunspot.search User, Tag, Product do |query| 
  query.keywords @search_query
  query.with(:age).greater_than 20
  query.with(:age).less_than 25
  query.paginate(:page => params[:page], :per_page => 30)
end

这篇关于具有多种模型的太阳黑子(Rails 中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆