Only re-index search when records are updated

This commit is contained in:
Justin Edmund 2025-01-18 22:18:49 -08:00
parent 1c1ed0dd9d
commit b791353e24
2 changed files with 18 additions and 6 deletions

View file

@ -85,7 +85,8 @@ module PostDeployment
def rebuild_search_indices def rebuild_search_indices
SearchIndexer.new( SearchIndexer.new(
test_mode: @test_mode, test_mode: @test_mode,
verbose: @verbose verbose: @verbose,
new_records: @new_records
).rebuild_all ).rebuild_all
end end

View file

@ -6,9 +6,10 @@ module PostDeployment
class SearchIndexer class SearchIndexer
include LoggingHelper include LoggingHelper
def initialize(test_mode:, verbose:) def initialize(test_mode:, verbose:, new_records: {})
@test_mode = test_mode @test_mode = test_mode
@verbose = verbose @verbose = verbose
@new_records = new_records
end end
def rebuild_all def rebuild_all
@ -38,12 +39,22 @@ module PostDeployment
end end
def rebuild_index_for(model) def rebuild_index_for(model)
# Determine model type (lowercase, pluralized)
model_type = model.name.downcase.pluralize
# Check if there are new records for this model type
new_records = @new_records[model_type] || []
if @test_mode if @test_mode
log_step "Would rebuild search index for #{model.name}" log_step "Would rebuild search index for #{model.name}" if new_records.any?
else else
log_verbose "#{model.name}... " if new_records.any?
PgSearch::Multisearch.rebuild(model) log_verbose "#{model.name}... "
log_verbose "✅ done!\n" PgSearch::Multisearch.rebuild(model)
log_verbose "✅ done! (#{new_records.size} new records)\n"
else
log_step "Skipping #{model.name} - no new records" if @verbose
end
end end
rescue StandardError => e rescue StandardError => e
log_error("Failed to rebuild index for #{model.name}: #{e.message}") log_error("Failed to rebuild index for #{model.name}: #{e.message}")