通过思考sphinx的多态关联过滤

时间:2011-11-11 11:22:47

标签: ruby-on-rails ruby filtering sphinx thinking-sphinx

我有以下型号。

class Post < ActiveRecord::Base
  has_many :timeline_items, :as=>:subject

  define_index do
    ....?
  end
end

class TimelineItem < ActiveRecord::Base
  belongs_to :subject, :polymorphic=>true
  belongs_to :actor, :polymorphic=>:true
end

我想通过思考 - 狮身人面像来获取属于特定演员的所有帖子。

ActiveRecord我会喜欢

Post.where('timeline_items.actor_type = "User" AND timeline_items.actor_id IN [1,2,3]).includes(:timeline_items)

我的问题是索引必须定义哪些查询以及使用think-sphinx获得相同结果的查询?

更新 但是我还需要按不同的actor类型过滤帖子,例如: Post.where('timeline_items.actor_type = "Group" AND timeline_items.actor_id IN [1,2,3]).includes(:timeline_items)

1 个答案:

答案 0 :(得分:1)

尝试以下内容:

define_index do
  # ...
  has timeline_items.actor.id, :as => :actor_ids
end

然后搜索:

Post.search 'foo', :with => {:actor_ids => [1,2,3]}

如评论中所详述,如果您需要在这些多态关联中过滤特定类的值,那么您将需要为该类创建另一个关联。这是因为Sphinx没有散列/键值对的概念,因此无法跟踪哪些ID属于哪些类。

一旦你有了新的关联,那么就像上面那样添加一个属性 - 也许是这样的:

has user_timeline_items.actor.id, :as => :user_actor_ids
相关问题