class ReindexCheckpoint < ApplicationRecord
validates :name, presence: true, uniqueness: true
end
class Search::ReindexSnipsJob < ApplicationJob
queue_as :search
def perform(batch_size: 500)
checkpoint = ReindexCheckpoint.find_or_create_by!(name: 'snips') { |c| c.last_id = 0 }
Snip.where('id > ?', checkpoint.last_id).order(id: :asc).limit(batch_size).each do |snip|
SearchIndex.upsert(document_id: snip.id, body: { title: snip.title })
checkpoint.update!(last_id: snip.id)
end
end
end
Full reindexes can be long and fragile. Add checkpoints (last processed id), process in batches, and make it resumable. That turns a scary operation into a routine one.