跨多个模型搜索多个单词

发布于 2024-11-28 23:11:34 字数 1062 浏览 1 评论 0原文

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

就本示例而言,假设我有以下模型:

  • 员工
  • 公司
  • 直辖市

一个县有多个直辖市,其中有多个公司,这些公司有多名员工。

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

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

我尝试了几种方法,但我不确定我是否走在正确的道路上。我正在寻找一种很好的 RoR 式的方法来做到这一点,并且我希望具有更多 RoR 智慧的人可以帮助概述一个正确的解决方案。


我尝试过的:

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

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

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

2)创建一个视图(称为“search_view”),将所有表连接在一起,以简化查询。然后我想我会在这个表上使用 Employee.find_by_sql() ,这会产生这些漂亮的 Employee 对象。我想我应该使用构建器来创建 SQL,并且似乎需要使用 Arel,所以我尝试执行以下操作:

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

但生成的 Ariel::Table 不包含任何列,因此无法构建我的查询。此时我有点卡住了,因为我不知道如何获得一个可用的查询生成器。

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:

  • Employee
  • Company
  • Municipality
  • County

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

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.

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.

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.


What I have tried:

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.

To solve this in RoR, I've been:

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) 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)

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.

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

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

发布评论

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

评论(1

转身以后 2024-12-05 23:11:34

我强烈建议您使用合适的搜索引擎来进行此类操作,它会让您的生活变得更加轻松。我也遇到了类似的问题,我想“天哪,我打赌设置像 Sphinx 这样的东西意味着我必须先阅读数千本手册和教程”。嗯,事实并非如此。

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

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

我没有尝试过其他搜索引擎,但我对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 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/

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.

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