-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merged in r2-3050-searchable-identifiers (pull request #6955)
R2-3050 - Add searchable_identifiers table to search records
- Loading branch information
Showing
15 changed files
with
250 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# frozen_string_literal: true | ||
|
||
# Copyright (c) 2014 - 2023 UNICEF. All rights reserved. | ||
|
||
# Class for SearchableIdentifier | ||
class SearchableIdentifier < ApplicationRecord | ||
belongs_to :record, polymorphic: true | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
db/data_migration/v2.11/calculate_searchable_identifiers.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
# frozen_string_literal: true | ||
|
||
# Copyright (c) 2014 - 2023 UNICEF. All rights reserved. | ||
|
||
# Example of usage: | ||
# 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}" | ||
puts message | ||
end | ||
|
||
models = (ARGV[0] || '').split(',') | ||
save_records = ARGV[1] == 'true' | ||
file_path = ARGV[2] | ||
|
||
return unless models.present? | ||
|
||
def records_to_process(model_class, ids_file_path) | ||
return model_class unless ids_file_path.present? | ||
|
||
print_log("Loading record ids from #{ids_file_path}...") | ||
ids_to_update = File.read(ids_file_path).split | ||
model_class.where(id: ids_to_update) | ||
end | ||
|
||
models.map(&:constantize).each do |model| | ||
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| | ||
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") | ||
else | ||
print_log("record_type: #{model.name}, record_id: #{record.id} does not have Searchable Identifiers") | ||
end | ||
end | ||
end | ||
end |
20 changes: 20 additions & 0 deletions
20
db/migrate/20241009000000_create_searchable_identifiers.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# frozen_string_literal: true | ||
|
||
# Copyright (c) 2014 - 2023 UNICEF. All rights reserved. | ||
class CreateSearchableIdentifiers < ActiveRecord::Migration[6.1] | ||
enable_extension 'pg_trgm' unless ENV['PRIMERO_PG_APP_ROLE'] || extension_enabled?('pg_trgm') | ||
|
||
def change | ||
create_table :searchable_identifiers do |t| | ||
t.references :record, polymorphic: true, type: :uuid | ||
t.string 'field_name' | ||
t.string 'value' | ||
end | ||
|
||
add_index :searchable_identifiers, | ||
:value, | ||
using: :gin, | ||
opclass: :gin_trgm_ops, | ||
name: 'searchable_identifiers_value_idx' | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.