-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Reindex stale search records #2843
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 | ||
|
Comment on lines
+17
to
+19
|
||
| 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 on lines
+30
to
+32
|
||
| 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." | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
indexed_card_idsis not scoped to the current account. Sincesearch_records_*tables store multiple accounts per shard, this will pluck IDs for all accounts on the shard, which is both extremely expensive and can cause incorrect “missing” detection. Filter byaccount_id: account.idwhen collecting existing records (and similarly for comments below).