From dd42f0a098f74f41946c41ea0f2c5d90769a74ea Mon Sep 17 00:00:00 2001 From: Jorge Manrubia Date: Tue, 14 Apr 2026 18:37:52 +0200 Subject: [PATCH] Add script to reindex stale search records (#2843) The data import path (Account::DataTransfer) uses insert_all! which bypasses ActiveRecord callbacks, so imported cards and comments never get search records created and are invisible to search and card-table filtering. This script reindexes all published cards and comments created before a cutoff date (default 2026-04-01). The reindex method is an upsert so already-indexed records get a harmless update. Usage: bin/rails runner script/maintenance/reindex_stale_search_records.rb bin/rails runner script/maintenance/reindex_stale_search_records.rb 2026-01-01 --- .../reindex_stale_search_records.rb | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 script/maintenance/reindex_stale_search_records.rb diff --git a/script/maintenance/reindex_stale_search_records.rb b/script/maintenance/reindex_stale_search_records.rb new file mode 100644 index 000000000..fd848f67d --- /dev/null +++ b/script/maintenance/reindex_stale_search_records.rb @@ -0,0 +1,39 @@ +# 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."