pat / thinking-sphinx

Sphinx/Manticore plugin for ActiveRecord/Rails
http://freelancing-gods.com/thinking-sphinx
MIT License
1.63k stars 469 forks source link

scope based upsert or delete #1258

Closed akostadinov closed 1 month ago

akostadinov commented 6 months ago

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 commented 6 months ago

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?

pat commented 1 month ago

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 🤞🏻