@@ -13,6 +13,9 @@ def fetch_studies_for(search_term, group: nil, page_size: 1000)
1313 attributes = { term : search_term . downcase }
1414 attributes [ :group ] = group if group . present?
1515 search_term_record = SearchTerm . find_by ( term : attributes [ :term ] ) || SearchTerm . create! ( attributes )
16+
17+ # current logic: remove all existing search results for the term and insert fresh results
18+ SearchTermResult . where ( search_term_id : search_term_record . id ) . delete_all
1619
1720 @api_client . search_studies ( query : search_term , page_size : page_size ) do |studies |
1821 persist ( studies , search_term_record )
@@ -35,6 +38,7 @@ def refresh_search_results_for(group)
3538
3639 def persist ( studies , search_term )
3740 silence_active_record do
41+ # covers potential discrepancy between API and aact nct_ids ran outside of updater process
3842 nct_ids = studies . map { |study | study . dig ( *@api_client . nct_id_path ) } . compact
3943 valid_nct_ids = Study . where ( nct_id : nct_ids ) . pluck ( :nct_id )
4044
@@ -47,13 +51,9 @@ def persist(studies, search_term)
4751 }
4852 end
4953
50- # current logic: remove all existing search results for the term and insert fresh results
51- ActiveRecord ::Base . transaction do
52- SearchTermResult . where ( search_term_id : search_term . id ) . delete_all
53- return if study_results . empty?
54- SearchTermResult . insert_all ( study_results )
55- end
56-
54+ return if study_results . empty?
55+ SearchTermResult . insert_all ( study_results )
56+
5757 Rails . logger . info ( "Imported #{ study_results . size } search results for term: #{ search_term . term } " )
5858 end
5959 rescue => e
0 commit comments