使用思维狮身人面像搜索多个多态项 [英] searching multiple polymorphic items using thinking sphinx

查看:67
本文介绍了使用思维狮身人面像搜索多个多态项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含多个值的附件表(通过多态关联).我希望能够通过 think-sphinx 搜索多个值(如 SQL 中的 AND).

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.

例如:

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 模型有一个字段(值),因此使用上面的索引定义,我可以执行以下操作:

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] }

但这在技术上并没有达到我所希望的.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')

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

(我知道上面缺少连接:P)

(i know there are joins missing above :P)

是否可以进行类似的搜索?

Is it possible to do a search similar to this?

推荐答案

Sphinx 没有键/值对的概念,所以你不能要求它匹配给定 field_values 上的值和 field_ids.

Sphinx has no concept of key/value pairs, so you can't ask it to match values and field_ids on given field_values.

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

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.

但是,恐怕这对匹配附件的多个值/id 对没有帮助.如果斯芬克斯不在我的头顶,我想不出办法做到这一点.

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.

这篇关于使用思维狮身人面像搜索多个多态项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆