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.