Skip to content

Commit

Permalink
Fix script to generate searchable_identifiers
Browse files Browse the repository at this point in the history
The method generate_searchable_identifiers was restored and
the script now can tell exactly how many identifiers
will be created.
  • Loading branch information
dhernandez-quoin committed Oct 16, 2024
1 parent 97d9015 commit 9c4d8b3
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 16 deletions.
14 changes: 8 additions & 6 deletions app/models/concerns/phonetic_searchable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,15 @@ def recalculate_phonetic_tokens
end

def recalculate_searchable_identifiers
return unless filterable_id_fields_to_save.present?
return unless filterable_id_fields_changed?

generate_searchable_identifiers
end

def generate_searchable_identifiers
identifiers_to_save = identifiers_to_update

filterable_id_fields_to_save.each do |field_name|
self.class.filterable_id_fields.each do |field_name|
next if data[field_name].blank? || identifiers_to_save.any? { |identifier| identifier[:field_name] == field_name }

identifiers_to_save << { field_name:, value: data[field_name] }
Expand All @@ -46,8 +50,6 @@ def recalculate_searchable_identifiers
end

def identifiers_to_update
return [] unless filterable_id_fields_to_save.present?

searchable_identifiers.map do |searchable_identifier|
{
field_name: searchable_identifier.field_name,
Expand All @@ -57,8 +59,8 @@ def identifiers_to_update
end
end

def filterable_id_fields_to_save
changes_to_save_for_record.keys & self.class.filterable_id_fields
def filterable_id_fields_changed?
(changes_to_save_for_record.keys & self.class.filterable_id_fields).present?
end

def generate_tokens
Expand Down
21 changes: 11 additions & 10 deletions db/data_migration/v2.11/calculate_searchable_identifiers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
# rails r bin/calculate_solr_fields Child,Incident,TracingRequest true file/path.txt

def print_log(message)
message = "[#{DateTime.now.strftime('%m/%d/%Y %H:%M')}]: #{message}"
message = "[#{DateTime.now.strftime('%m/%d/%Y %H:%M:%S')}]: #{message}"
puts message
end

Expand All @@ -28,17 +28,18 @@ def records_to_process(model_class, ids_file_path)
records_to_process(model, file_path).find_in_batches(batch_size: 1000).with_index do |records, batch|
print_log("Process #{model.name} batch #{batch}...")
records.each do |record|
current_size = record.searchable_identifiers.size
record.generate_searchable_identifiers
generated_size = record.searchable_identifiers.size
total = generated_size - current_size
record_info = "record_type: #{model.name}, record_id: #{record.id}"
field_names = record.searchable_identifiers.map(&:field_name)
identifiers_info = "Searchable Identifiers[#{field_names.join(', ')}]"
if save_records
current_size = record.searchable_identifiers.size
record.generate_searchable_identifiers
generated_size = record.searchable_identifiers.size
total = generated_size - current_size
print_log("#{total} Searchable Identifiers generated for record_type: #{model.name}, record_id: #{record.id}")
elsif record.searchable_identifiers.present?
identifiers_size = record.searchable_identifiers.size
print_log("record_type: #{model.name}, record_id: #{record.id} has #{identifiers_size} Searchable Identifiers")
record.save!
print_log("#{total} - #{identifiers_info} generated for #{record_info}")
else
print_log("record_type: #{model.name}, record_id: #{record.id} does not have Searchable Identifiers")
print_log("#{total} - #{identifiers_info} will be generated for #{record_info}")
end
end
end
Expand Down

0 comments on commit 9c4d8b3

Please sign in to comment.