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

scope based upsert or delete #1258

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open

Conversation

akostadinov
Copy link
Contributor

@akostadinov akostadinov commented Feb 28, 2024

When an index is defined, one can specify a scope. This scope is used to obtain records on initial indexing (ts:index) but is not respected by the model callbacks.

With this additional method one can easily implement ActiveRecord callbacks e.g.

    after_commit :index_object

    def index_object
      ThinkingSphinx::Processor.new(instance: instance).stage
    end

A little bit more efficient (preventing a database roundtrip on deletes

    after_commit :index_object, :on => %i(create update)
    after_commit :deindex_object, :on => :destroy

    def index_object
      ThinkingSphinx::Processor.new(instance: self).stage
    end

    def deindex_object
      ThinkingSphinx::Processor.new(instance: self).delete
    end

If certain models never use scopes for their indices, then #upsert can be called instead of #stage to avoid database roundtrips.

While on it, example background processing of indexing operations e.g.

    after_commit :background_index_object

    def background_index_object
      ThinkingIndexationWorker.perform_later(self.class, id) # call ThinkingSphinx::Processor#stage in worker
    end

fixes #1253
complements #1216 and #1215

@akostadinov
Copy link
Contributor Author

akostadinov commented Feb 28, 2024

Not sure why test / manticore (2.7, 5_2, mysql2, 6.0.0, manticore) (pull_request) failed. I don't see any change related to it and the postgres variant (along all other runners) pass. Maybe just rerun it?

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 this pull request may close these issues.

How to remove records on update with real-time indexes
1 participant