@@ -13,6 +13,9 @@ def fetch_studies_for(search_term, group: nil, page_size: 1000)
13
13
attributes = { term : search_term . downcase }
14
14
attributes [ :group ] = group if group . present?
15
15
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
16
19
17
20
@api_client . search_studies ( query : search_term , page_size : page_size ) do |studies |
18
21
persist ( studies , search_term_record )
@@ -35,6 +38,7 @@ def refresh_search_results_for(group)
35
38
36
39
def persist ( studies , search_term )
37
40
silence_active_record do
41
+ # covers potential discrepancy between API and aact nct_ids ran outside of updater process
38
42
nct_ids = studies . map { |study | study . dig ( *@api_client . nct_id_path ) } . compact
39
43
valid_nct_ids = Study . where ( nct_id : nct_ids ) . pluck ( :nct_id )
40
44
@@ -47,13 +51,9 @@ def persist(studies, search_term)
47
51
}
48
52
end
49
53
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
+
57
57
Rails . logger . info ( "Imported #{ study_results . size } search results for term: #{ search_term . term } " )
58
58
end
59
59
rescue => e
0 commit comments