使用思维狮身人面像搜索多个多态项

发布于 2024-12-26 10:00:08 字数 1045 浏览 5 评论 0原文

我有一个具有多个值的附件表(通过多态关联)。我希望能够通过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 技术交流群。

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

发布评论

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

评论(2

梅窗月明清似水 2025-01-02 10:00:08

Sphinx 没有键/值对的概念,因此您不能要求它匹配给定 field_values 上的值和 field_ids。

如果您想搜索与单个值/ID 对匹配的附件,一种可能的解决方法是搜索 FieldValue,并按 customized_id 分组:

class FieldValue < ActiveRecord::Base
  define_index do
    indexes value
    has field_id, customized_id
  end
end

FieldValue.search :conditions => {:value => '1'}, :with => {:field_id => 23},
  :group_by => 'customized_id', :group_function => :attr

分组将确保您只能获得一个匹配项依恋。

然而,恐怕这对匹配附件的多个值/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:

class FieldValue < ActiveRecord::Base
  define_index do
    indexes value
    has field_id, customized_id
  end
end

FieldValue.search :conditions => {:value => '1'}, :with => {:field_id => 23},
  :group_by => 'customized_id', :group_function => :attr

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.

双手揣兜 2025-01-02 10:00:08

这可以通过几个步骤来完成。

  1. 您需要在附件索引中引用 FieldValue 的 id:

    有 field_values.id, :as => :field_values_ids

  2. 获取您需要的 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 })

  3. 搜索具有这些对的附件:

    r1 = Attachment.search(with: { field_values_ids: v1 })

    r2 = Attachment.search(with: { field_values_ids: v2 })

  4. 结果相交:

    r1.to_a & r2.to_a

警告:您会丢失分页和排序(请参阅 Pat 的回答我的问题)。

This can be done in several steps.

  1. You'll need to reference FieldValue's ids in the Attachment index:

    has field_values.id, :as => :field_values_ids

  2. 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 })

  3. Search for Attachments that have these pairs:

    r1 = Attachment.search(with: { field_values_ids: v1 })

    r2 = Attachment.search(with: { field_values_ids: v2 })

  4. Intersect the results:

    r1.to_a & r2.to_a

Caveat: you lose pagination and ordering (See Pat's answer to my question).

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