Thinking Sphinx 延迟增量索引 - 记录删除时索引未更新

发布于 2024-10-30 23:19:25 字数 1201 浏览 0 评论 0原文

我在使用 Thinking_sphinx 的延迟增量时遇到问题。这是模型:

Event
  has_many :subscriptions
  has_many :users, :through => :subscriptions
  ...
  define_index do
    indexes name
    indexes users(:id), :as => :user_id
    set_property :delta => :delayed
  ...

Subscription
  belongs_to :event
  belongs_to :user

User
  has_many :subscriptions
  has_many :events, :through => :subscriptions

当用户添加/删除订阅时,我通过订阅模型回调将增量标志设置为所有相应的事件,如下所示:

after_save :set_events_delta_flag
after_destroy :set_events_delta_flag

def set_events_delta_flag
  Event.define_indexes
  sql = "UPDATE events SET delta = true FROM subscriptions"
  sql << " WHERE events.id = subscriptions.event_id AND (subscriptions.id = #{self.id})"
  Event.connection.update(sql)
  Event.index_delta
end

当用户添加订阅时,它工作正常:回调运行,然后 ThinkingSphinx:: Deltas::DeltaJob 作业运行并更新索引。但是,当用户删除订阅时,回调和 DeltaJob 会运行,但索引似乎没有更新:

如果我执行以下操作:

Event.search("". :with => {:user_id => XX}).search_count

删除订阅之前和之后,计数不会更改(它在 < 之前和之后发生变化)强>添加订阅)

这是预期的行为吗?我做错了什么?

更新:Sphinx 存储同一文档的两份副本(一份在核心索引中,一份在增量索引中)似乎是问题的原因。

I'm having trouble using delayed deltas with thinking_sphinx. This is the model:

Event
  has_many :subscriptions
  has_many :users, :through => :subscriptions
  ...
  define_index do
    indexes name
    indexes users(:id), :as => :user_id
    set_property :delta => :delayed
  ...

Subscription
  belongs_to :event
  belongs_to :user

User
  has_many :subscriptions
  has_many :events, :through => :subscriptions

When a user adds/removes a subscription I set the delta flag to all the corresponding events through the subscription model callbacks like this:

after_save :set_events_delta_flag
after_destroy :set_events_delta_flag

def set_events_delta_flag
  Event.define_indexes
  sql = "UPDATE events SET delta = true FROM subscriptions"
  sql << " WHERE events.id = subscriptions.event_id AND (subscriptions.id = #{self.id})"
  Event.connection.update(sql)
  Event.index_delta
end

It works ok when a user adds a subscription: the callback runs and then a ThinkingSphinx::Deltas::DeltaJob job runs and updates the index. However when a user removes a subscription the callback and the DeltaJob run, but it seems that the index does not get updated:

If I do something like:

Event.search("". :with => {:user_id => XX}).search_count

before and after remove a subscription, the count does not change (it changes before and after adding a subscription)

is this an expected behavior? What am I doing wrong?

Update: It seems that Sphinx storig two copies of the same document (one in the core index and one in the delta index) is the cause of the problem.

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

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

发布评论

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

评论(1

倾其所爱 2024-11-06 23:19:25

升级到 Sphinx 2.0.3 后,拥有 2 个索引的问题将消失。索引将合并为单个索引。您可以尝试升级它。

With upgrade to Sphinx 2.0.3, this problem of having 2 indices will go away. the indices will be merged into single. You can try upgrading it.

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