按多态关联思维狮身人面像过滤

发布于 2024-12-15 08:54:20 字数 752 浏览 4 评论 0原文

我有以下型号。

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

我想通过thinking-sphinx获取属于特定演员的所有帖子。

ActiveRecord 中,我会这样做

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

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

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

I have the following models.

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

I want to get all the posts belonging to specific actors with thinking-sphinx.

In ActiveRecord I would do like

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

My question is what indexes have to define and what query have to use to get the same results with thinking-sphinx?

Update
However I also need to filter the posts by different actor types like:
Post.where('timeline_items.actor_type = "Group" AND timeline_items.actor_id IN [1,2,3]).includes(:timeline_items)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

从来不烧饼 2024-12-22 08:54:20

尝试以下操作:

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

Give the following a try:

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

And then for searching:

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

As detailed in the comments, if you need to filter on a specific class's values in those polymorphic associations, then you're going to need to create another association for just that class. This is because Sphinx has no concept of hashes/key-value pairs, and so can't track which ids belongs to which classes.

Once you have that new association in place, then add an attribute for that much like above - perhaps like this:

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