Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update index after soft delete #1057

Closed
chris-roerig opened this issue May 17, 2017 · 5 comments
Closed

Update index after soft delete #1057

chris-roerig opened this issue May 17, 2017 · 5 comments

Comments

@chris-roerig
Copy link

Pat,

I introduced soft delete to a couple models using ActsAsParanoid.

Strangely, I have one record that has been destroyed (has a "deleted_at" value) but it still shows up in the TS query. I read on another thread that TS has its own post destroy hook to update the indexed record. I'm guessing that hook was not called for this particular record. I've tried ts:rebuild and ts:reindex but I can't get that record to stop showing up. Any ideas?

@pat
Copy link
Owner

pat commented May 17, 2017

Hi Chris

Thinking Sphinx won't automatically ignore soft-deleted records when indexing - you'll want to have a WHERE clause in your index definition to ensure that.

As for the callbacks - Thinking Sphinx will mark a record as 'deleted' when destroy (and the corresponding callbacks) is called, but this is just a temporary measure until the next indexing happens. I'm not sure if ActsAsParanoid invokes those callbacks, so it could be that this isn't happening? If you want to invoke this callback manually, use ThinkingSphinx::ActiveRecord::Callbacks::DeleteCallbacks.after_destroy(instance)

@chris-roerig
Copy link
Author

Hi Pat,

The callback you noted is what I was looking for and it fixed my issue. It appears as long as the record is marked as deleted in Sphinx no additional query options are needed to filter out "deleted" records.

Thanks for your help.

@akostadinov
Copy link
Contributor

Unfortunately there seems to be no clean way to delete instance in case callbacks are disabled. For example disable all callbacks and insert own callbacks.

@pat
Copy link
Owner

pat commented Jan 25, 2022

Hi @akostadinov - can you provide an example of the callbacks you're using instead? Also, may be worth continuing this discussion as a new issue (and if you can, do let me know what versions of Thinking Sphinx and Rails you're using - callbacks in TS have evolved in the last few years).

@akostadinov
Copy link
Contributor

akostadinov commented Jan 25, 2022

This is current background worker that we use:

class SphinxAccountIndexationWorker < SphinxIndexationWorker
  def perform(account)
    if account.will_be_deleted?
      to_delete = account.buyers.pluck(:id) << account.id
      # This is how deletion is performed by ThinkingSphinx::ActiveRecord::Callbacks::DeleteCallbacks#delete_from_sphinx
      # Alternatively, we could delete individual records one by one with:
      # ThinkingSphinx::ActiveRecord::Callbacks::DeleteCallbacks.after_destroy(instance)
      indices(account).each do |index|
        ThinkingSphinx::Deletion.perform index, to_delete
      end
    else
      super
    end
  end
end

This is thinking-sphinx 3.4, upgrading to 5.4 right now, seems to work the same as far as I can tell.

I opened a related issue #1215, #1216

One option would be to check whether model instance can be found in the index scope or not. When it can no longer be found in scope, then delete it. In this way

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants