thinking-sphinx icon indicating copy to clipboard operation
thinking-sphinx copied to clipboard

scope based upsert or delete

Open akostadinov opened this issue 1 year ago • 1 comments

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 avatar Feb 28 '24 12:02 akostadinov

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?

akostadinov avatar Feb 28 '24 23:02 akostadinov

Thanks @akostadinov - I've rebased this to resolve the build failures (not your fault!), renamed the method from stage to sync, and merged it in. Hoping to get a release of the gem out this evening 🤞🏻

pat avatar Jul 07 '24 10:07 pat