思考 Sphinx 并搜索多个模型
我正在寻找一种对多个模型执行搜索的方法(请参阅 这篇文章),并得到了一些答案,说 Thinking Sphinx 非常适合此类事情。
事实上,它看起来很不错,而且似乎应用程序范围的搜索功能 (ThinkingSphinx.search
) 很接近我想要的。但文档指出这将返回各种模型对象,具体取决于找到匹配的位置。
我有一个有点像这样的模型:
- 员工
- 公司
- 市县
- ,
其中员工仅通过公司链接到县,而公司又链接到市,而市又链接到实际的县。
现在,根据我的搜索结果,我真的只想要 Employee 对象。例如,搜索字符串“joe tulsa”应返回所有员工,其中这两个单词都可以在命名模型中的某处找到。我会得到一些误报,但至少我应该让塔尔萨县的每个员工都命名为“Joe”。
这可以通过 Thinking Sphinx 的内置功能来实现吗?
I'm looking for a way to perform a search against multiple models (see this post), and got a couple of answers saying that Thinking Sphinx would be a good match for this kind of thing.
Indeed, it looks sweet, and it seems the application-wide search capability (ThinkingSphinx.search
) is close to what I want. But the docs state this will return various kinds of model objects, depending on where a match was found.
I have a models somewhat like this:
- Employee
- Company
- Municipality
- County
Where employees are linked to County only though Company, which in turn is linked to a Municipality, which in turn is linked to the actual County.
Now as a result from my search, I really only want Employee objects. For example a search for the string "joe tulsa" should return all Employees where both words can be found somewhere in the named models. I'll get some false positives, but at least I should get every employee named "Joe" in Tulsa county.
Is this something that can be achieved with built in functionality of Thinking Sphinx?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为在这种情况下你应该做的是为你的
Employee
模型定义关联(你可能已经有了),例如:编辑: 我测试了多级
has_one
关系,但它不是这样工作的。在不进行非规范化的情况下对这 4 层进行建模似乎相当复杂。如果我想出一些东西,我会更新。无论如何,如果您进行反规范化(即将冗余的外部 ID 添加到您的员工表的所有模型中),关联就会很简单,并且会大大增加索引生成时间。同时,它可能需要更多的工作来确保一致性。设置关联后,您可以在
Employee
模型中定义 Sphinx 索引,如下所示:这样,关联中的列也会被索引,并且 Sphinx 会在以下情况下自动将所有这些表连接在一起:建立索引。
I think what you should do in this case is define associations for your
Employee
model (which you probably have already), e.g.:Edit: I tested the multi-level
has_one
relationship, and it doesn't work like that. Seems to be fairly complex to model these 4 layers without denormalizing. I'll update if I come up with something. In any case, if you denormalize (i.e. add redundant foreign IDs to all models to your employees table), the associations are straightforward and you massively increase your index generation time. At the same time, it may involve more work to insure consistency.Once the associations are set up, you can define the Sphinx index in your
Employee
model, like this:That way the columns in your associations are indexed as well, and Sphinx will automatically join all those tables together when building the index.