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

How to remove records on update with real-time indexes #1253

Open
JasonBarnabe opened this issue Oct 18, 2023 · 3 comments
Open

How to remove records on update with real-time indexes #1253

JasonBarnabe opened this issue Oct 18, 2023 · 3 comments

Comments

@JasonBarnabe
Copy link

With a real-time index, I can provide a scope to filter out records I don't want indexed:

This allows eager loading of associations, or even filtering out specific values. However, keep in mind the default callbacks don’t use this scope, so a record that does not get included in this scope but is then altered will be added to your Sphinx data.

and

For real-time indices you can define a custom scope to preload associations or apply custom conditions:

scope { Article.includes(:comments) }

This scope only comes into play when populating all records at once, not when single records are created or updated.

So I need separate logic on update to filter out things I don't want indexed. Closest I can find is this info in callbacks...

If you wish to have your callbacks update Sphinx only in certain conditions, you can either define your own callback and then invoke TS if/when needed:

after_save :populate_to_sphinx

# ...

def populate_to_sphinx
  return unless indexing?

  ThinkingSphinx::RealTime::Callbacks::RealTimeCallbacks.new(
    :article
  ).after_save self
end

Or supply a block to the callback instantiation which returns an array of instances to process:

# if your model is app/models/article.rb:
ThinkingSphinx::Callbacks.append(self, :behaviours => [:real_time]) { |instance|
  instance.indexing? ? [instance] : []
}

However this is changing what records are updated in Sphinx and not what records are in Sphinx. Specifically, if a record previously had indexing? return true, but now after an update returns false, the logic provided just means that no update will be sent to Sphinx (and it will remain in Sphinx), not that the record will be removed in Sphinx.

How do I remove a record from Sphinx on an update with a callback? Or am I thinking about this wrong?

@akostadinov
Copy link
Contributor

This seems related to #1216
You can also check the discussion under #1215

Please check #1258 and see if it resolves the issue for you.

@pat
Copy link
Owner

pat commented Jul 7, 2024

As per @akostadinov's great work, this should now be covered by ThinkingSphinx::Processor.new(instance: self).sync - it'll update records if they're still within the scope, or otherwise delete them.

@pat
Copy link
Owner

pat commented Jul 7, 2024

… and this is part of v5.6.0 which has just been released!

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

Successfully merging a pull request may close this issue.

3 participants