跨多个模型搜索多个词 [英] Search for multiple words, across multiple models

查看:41
本文介绍了跨多个模型搜索多个词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在站点中创建搜索功能,并且我希望用户能够搜索多个单词,并根据各种模型中存在的条件执行子字符串匹配.

I'm trying to create search functionality in a site, and I want the user to be able to search for multiple words, performing substring matching against criteria which exist in various models.

为了这个例子,假设我有以下模型:

For the sake of this example, let's say I have the following models:

  • 员工
  • 公司
  • 市政府

一个县有多个市,有多个公司,有多个员工.

A county has multiple municipalities, which has multiple companies, which have multiple employees.

我希望搜索能够针对 Employee.firstname、Employee.lastname、Company.name、Municipity.name 和 County.name 的组合进行搜索,并且我希望最终结果是 Employee 实例.

I want the search to be able to search against a combination of Employee.firstname, Employee.lastname, Company.name, Municipality.name and County.name, and I want the end result to be Employee instances.

例如,搜索字符串joe tulsa"应该返回所有员工,其中这两个词都可以在我在上一句中命名的属性中的某处找到.我会得到一些误报,但至少我应该让塔尔萨县的每个员工都命名为乔".

For example a search for the string "joe tulsa" should return all Employees where both words can be found somewhere in the properties I named in the previous sentence. I'll get some false positives, but at least I should get every employee named "Joe" in Tulsa county.

我尝试了几种方法,但我不确定我是否走上了正确的道路.我正在寻找一种很好的 RoR 式方法来执行此操作,我希望有更多 RoR 智慧的人可以帮助勾勒出正确的解决方案.

I've tried a couple of approaches, but I'm not sure I'm going down the right path. I'm looking for a nice RoR-ish way of doing this, and I'm hoping someone with more RoR wisdom can help outline a proper solution.

我尝试过的:

我对这种搜索不是很有经验,但在 RoR 之外,我会手动创建 SQL 语句将所有表连接在一起,为每个单独的搜索词创建 where 子句,涵盖不同的表.也许使用构建器.然后只需执行查询并遍历结果,手动实例化 Employee 对象并将它们添加到数组中.

I'm not very experienced with this kind of search, but outside RoR, I'd manually create an SQL statement to join all the tables together, create where clauses for each separate search word, covering the different tables. Perhaps use a builder. Then just execute the query and loop through the results, instantiate Employee objects manually and adding them to an array.

为了在 RoR 中解决这个问题,我一直在:

To solve this in RoR, I've been:

1) 在我的项目对应于 Employee 模型的内容中涉足命名范围,但是当我需要加入两个或更多步骤"(市政和县)之外的表时,我陷入了困境.

1) Dabbling with named scopes in what in my project corresponds to the Employee model, but I got stuck when I needed to join in tables two or more "steps" away (Municipality and County).

2) 创建一个视图(称为search_view")将所有表连接在一起,以简化查询.然后想我会在这个表上使用 Employee.find_by_sql() ,这会给我带来这些不错的 Employee 对象.我以为我会使用构建器来创建 SQL,而且似乎可以使用 Arel,所以我尝试执行以下操作:

2) Created a view (called "search_view") joining all the tables together, to simplify the query. Then thought I'd use Employee.find_by_sql() on this table, which would yield me these nice Employee objects. I thought I'd use a builder to create the SQL, and it seemed that Arel was the thing to use, so I tried doing something like:

view = Arel::Table.new(:search_view)

但是生成的 Ariel::Table 不包含任何列,因此无法用于构建我的查询.在这一点上,我有点卡住了,因为我不知道如何获得一个有效的查询构建器.

But the resulting Ariel::Table does not contain any columns, so it's not usable to build my query. At this point I'm a bit stuck as I don't know how to get a working query builder.

推荐答案

我强烈建议您使用合适的搜索引擎来处理此类问题,它会让您的生活更轻松.我有一个类似的问题,我想男孩,我敢打赌设置像 Sphinx 这样的东西意味着我必须先阅读成千上万的手册和教程".好吧,事实并非如此.

I strongly recommend using a proper search engine for something like this, it will make life a lot easier for you. I had a similar problem and I thought "Boy, I bet setting up something like Sphinx means I have to read thousands of manuals and tutorials first". Well, that's not the case.

Thinking Sphinx 是我推荐的 Rails gem,它使集成 Sphinx 变得非常容易.您根本不需要有太多经验即可开始使用:

Thinking Sphinx is a Rails gem I recommend which makes it very easy to integrate Sphinx. You don't need to have much experience at all to get started:

http://freelancing-god.github.com/ts/en/

我没有尝试过其他搜索引擎,但我对 Sphinx 非常满意.我设法在不到一天的时间内建立了一个相对复杂的实时搜索.

I haven't tried other search engines, but I'm very satisfied with Sphinx. I managed to set up a relatively complex real-time search in less than a day.

这篇关于跨多个模型搜索多个词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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