Add SearchReindexJob to repopulate the search index (#2850)
Rebuilds search records for every published Card and every Comment on a published Card. Intended for manual invocation after a search-index loss event — not a recurring job. Uses ActiveJob::Continuable so the work survives worker restarts and resumes from the last-processed id. The job is idempotent. Removes script/maintenance/reindex_stale_search_records.rb, which was written against a different hypothesis (a date-cutoff backfill) and is superseded by this unconditional repair job.
This commit is contained in:
@@ -1,39 +0,0 @@
|
||||
# Reindex cards and comments that may be missing from the search index.
|
||||
#
|
||||
# The data import path (Account::DataTransfer) uses insert_all! which bypasses
|
||||
# ActiveRecord callbacks, so imported cards and comments never get search records
|
||||
# created. This script reindexes cards and comments created before a cutoff date.
|
||||
#
|
||||
# Usage:
|
||||
# bin/rails runner script/maintenance/reindex_stale_search_records.rb # default cutoff: 2025-11-13
|
||||
# bin/rails runner script/maintenance/reindex_stale_search_records.rb 2026-01-01 # custom cutoff
|
||||
|
||||
cutoff = Date.parse(ARGV[0] || "2025-11-13")
|
||||
|
||||
puts "Reindexing cards and comments created before #{cutoff}..."
|
||||
|
||||
cards = Card.published.where("created_at < ?", cutoff).includes(:rich_text_description)
|
||||
card_count = cards.count
|
||||
puts "Found #{card_count} cards to reindex"
|
||||
|
||||
reindexed_cards = 0
|
||||
cards.find_each do |card|
|
||||
card.reindex
|
||||
reindexed_cards += 1
|
||||
print "\rCards: #{reindexed_cards}/#{card_count}" if reindexed_cards % 100 == 0
|
||||
end
|
||||
puts "\rCards: #{reindexed_cards}/#{card_count}"
|
||||
|
||||
comments = Comment.joins(:card).merge(Card.published).where("comments.created_at < ?", cutoff).includes(:rich_text_body, :card)
|
||||
comment_count = comments.count
|
||||
puts "Found #{comment_count} comments to reindex"
|
||||
|
||||
reindexed_comments = 0
|
||||
comments.find_each do |comment|
|
||||
comment.reindex
|
||||
reindexed_comments += 1
|
||||
print "\rComments: #{reindexed_comments}/#{comment_count}" if reindexed_comments % 100 == 0
|
||||
end
|
||||
puts "\rComments: #{reindexed_comments}/#{comment_count}"
|
||||
|
||||
puts "Done! Reindexed #{reindexed_cards} cards and #{reindexed_comments} comments."
|
||||
Reference in New Issue
Block a user