使用思维狮身人面像搜索多个多态项
我有一个具有多个值的附件表(通过多态关联)。我希望能够通过thinking-sphinx 搜索多个值(如SQL 中的AND)。
例如:
class FieldValue < ActiveRecord::Base
belongs_to :customized, :polymorphic => true
end
class Attachment < ActiveRecord::Base
has_many :field_values, :as => :customized, :dependent => :destroy
define_index do
has field_values.field_id, :as => :field_ids, :type => :multi
indexes field_values.value, :as => :field_values, :type => :multi
set_property :enable_star => 1
set_property :min_infix_len => 3
end
end
我的 FieldValue 模型有一个字段(值),因此使用上面的索引定义,我可以执行以下操作:
Attachment.search :conditions => { :field_values => ["*5100*", "1"] }, :with => { :field_ids => [23, 24] }
但这在技术上并不能达到我所希望的效果。 field_values 应与 field_ids 匹配(类似于 select * from attachments where (field_value.id = 23 and field_value.value like '*5100*) and (field_value.id = 23 and field_value.value = '1')
(我知道上面缺少连接:P)
是否可以进行与此类似的搜索?
I have an attachments table that has multiple values (via polymorphic assoc). I'd like to be able to search for multiple values (like an AND in SQL) via thinking-sphinx.
For example:
class FieldValue < ActiveRecord::Base
belongs_to :customized, :polymorphic => true
end
class Attachment < ActiveRecord::Base
has_many :field_values, :as => :customized, :dependent => :destroy
define_index do
has field_values.field_id, :as => :field_ids, :type => :multi
indexes field_values.value, :as => :field_values, :type => :multi
set_property :enable_star => 1
set_property :min_infix_len => 3
end
end
My FieldValue model has one field (value), so using the above index definition, I could do something like:
Attachment.search :conditions => { :field_values => ["*5100*", "1"] }, :with => { :field_ids => [23, 24] }
But this doesn't technically do what I'm hoping for. The field_values should match the field_ids (similar to a select * from attachments where (field_value.id = 23 and field_value.value like '*5100*) and (field_value.id = 23 and field_value.value = '1')
(i know there are joins missing above :P)
Is it possible to do a search similar to this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Sphinx 没有键/值对的概念,因此您不能要求它匹配给定 field_values 上的值和 field_ids。
如果您想搜索与单个值/ID 对匹配的附件,一种可能的解决方法是搜索 FieldValue,并按
customized_id
分组:分组将确保您只能获得一个匹配项依恋。
然而,恐怕这对匹配附件的多个值/ID 对没有帮助。我想不出有什么办法可以让狮身人面像脱离我的脑海。
Sphinx has no concept of key/value pairs, so you can't ask it to match values and field_ids on given field_values.
If you wanted to search on attachments matching a single value/id pair, a possible way around this would be to search on FieldValue instead, and group by
customized_id
:The grouping will ensure you only get one match per Attachment.
However, this is no help matching multiple value/id pairs for an attachment, I'm afraid. I can't think of a way to do that with Sphinx off the top of my head.
这可以通过几个步骤来完成。
您需要在附件索引中引用 FieldValue 的 id:
有 field_values.id, :as => :field_values_ids
获取您需要的 FieldValue 对的 ID:
v1 = FieldValue.search_for_ids(条件: { value: "*5100*" }, with: { field_id: 23 })
v2 = FieldValue.search_for_ids(conditions: { value: "1" }, with: { field_id: 24 })
搜索具有这些对的附件:
r1 = Attachment.search(with: { field_values_ids: v1 })
r2 = Attachment.search(with: { field_values_ids: v2 })
结果相交:
r1.to_a & r2.to_a
警告:您会丢失分页和排序(请参阅 Pat 的回答我的问题)。
This can be done in several steps.
You'll need to reference FieldValue's ids in the Attachment index:
has field_values.id, :as => :field_values_ids
Get the ids for the FieldValue pairs you need:
v1 = FieldValue.search_for_ids(conditions: { value: "*5100*" }, with: { field_id: 23 })
v2 = FieldValue.search_for_ids(conditions: { value: "1" }, with: { field_id: 24 })
Search for Attachments that have these pairs:
r1 = Attachment.search(with: { field_values_ids: v1 })
r2 = Attachment.search(with: { field_values_ids: v2 })
Intersect the results:
r1.to_a & r2.to_a
Caveat: you lose pagination and ordering (See Pat's answer to my question).