Thinking Sphinx 延迟增量索引 - 记录删除时索引未更新
我在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
升级到 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.