作者:落地有声800_491_431 | 来源:互联网 | 2023-02-07 19:27
Imlookingforawaytoperformasearchagainstmultiplemodels(seethispost),andgotacouple
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.
事实上,它看起来很甜美,似乎应用程序范围的搜索功能(ThinkingSphinx.search)接近我想要的。但文档声明这将返回各种模型对象,具体取决于找到匹配的位置。
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.
现在作为搜索的结果,我真的只想要Employee对象。例如,搜索字符串“joe tulsa”应返回所有Employees,其中两个单词都可以在命名模型中的某处找到。我会得到一些误报,但至少我应该让塔尔萨县的每个员工都叫“乔”。
Is this something that can be achieved with built in functionality of Thinking Sphinx?
这是可以通过Thinking Sphinx的内置功能实现的吗?
1 个解决方案
3
I think what you should do in this case is define associations for your Employee
model (which you probably have already), e.g.:
我认为在这种情况下你应该做的是定义你的Employee模型的关联(你可能已经有),例如:
class Employee :company
has_one :county, :through => :company
...
end
class Company :municipality
...
end
class Municipality
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.
编辑:我测试了多级has_one关系,它不能像那样工作。对这4层进行建模而不进行非规范化似乎相当复杂。如果我拿出一些东西,我会更新。在任何情况下,如果您进行非规范化(即向所有模型添加冗余外部ID到您的employees表),则关联很简单,并且您会大量增加索引生成时间。同时,它可能涉及更多工作以确保一致性。
Once the associations are set up, you can define the Sphinx index in your Employee
model, like this:
设置关联后,您可以在Employee模型中定义Sphinx索引,如下所示:
define_index do
indexes :name, :sortable => :true
indexes company(:name), :as => :company
indexes municipality(:name), :as => :municipality
indexes county(:name), :as => :county
...
end
That way the columns in your associations are indexed as well, and Sphinx will automatically join all those tables together when building the index.
这样,关联中的列也会被编入索引,并且Sphinx会在构建索引时自动将所有这些表连接在一起。