Skip to content

Instantly share code, notes, and snippets.

@njakobsen
Last active May 19, 2019 16:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save njakobsen/2135706c35139d484a56d660b089cc5d to your computer and use it in GitHub Desktop.
Save njakobsen/2135706c35139d484a56d660b089cc5d to your computer and use it in GitHub Desktop.
Automatic removal of stale Sphinx documents during Realtime Indexing
# Improve indexing to remove records whose sphinx document id is stale due to changes to the number/order of indices
module SphinxIndexing
# Delete existing record before inserting new ones. This avoids the need to rebuild the index after adding or removing
# an index. Without this, the old indexed record would not be deleted since Sphinx's calculation of that record's id
# would not match now that the number/order of indices has changed.
module Transcriber
def copy(*instances)
return unless instances.present?
ids = instances.map(&:id).compact
sphinxql = <<~SQL
DELETE FROM #{@index.name} WHERE sphinx_internal_id IN (#{ids.join(', ')})
SQL
ThinkingSphinx::Logger.log :query, sphinxql do
ThinkingSphinx::Connection.take do |connection|
connection.execute sphinxql
end
end
super
end
end
# Delete record using their ActiveRecord id instead of their sphinx document id. This avoids the need to rebuild the
# index after adding or removing an index. Without this, the old indexed record would not be deleted since Sphinx's
# calculation of that record's id would not match now that the number/order of indices has changed.
module RealtimeDeletion
def perform
return unless callbacks_enabled?
sphinxql = <<~SQL
DELETE FROM #{@index.name} WHERE sphinx_internal_id IN (#{ids.join(', ')})
SQL
execute sphinxql
end
end
end
ThinkingSphinx::RealTime::Transcriber.prepend(SphinxIndexing::Transcriber)
ThinkingSphinx::Deletion::RealtimeDeletion.prepend(SphinxIndexing::RealtimeDeletion)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment