@@ -109,7 +109,8 @@ def search(searchterm:, citation:, contributors:, funding_information:, geodista
109109 query = construct_query ( searchterm , citation , contributors , funding_information , geodistance , geobox , identifiers ,
110110 locations , subjects , title , source , boolean_type , filters , per_page , query_mode )
111111
112- results = Opensearch . new . search ( from , query , Timdex ::OSClient , highlight : highlight_requested? , index : index , fulltext : fulltext , query_mode : query_mode )
112+ results = Opensearch . new . search ( from , query , Timdex ::OSClient , highlight : highlight_requested? , index : index ,
113+ fulltext : fulltext , query_mode : query_mode , requested_aggregations : requested_aggregations )
113114
114115 response = { }
115116 response [ :hits ] = results [ 'hits' ] [ 'total' ] [ 'value' ]
@@ -122,6 +123,22 @@ def highlight_requested?
122123 context [ :tracers ] . first . log_data [ :used_fields ] . include? ( 'Record.highlight' )
123124 end
124125
126+ # Convert aggregation fields to format expected by aggregations model.
127+ # We believe format was set to `content_format` because when TIMDEX was a rest REST API, the
128+ # format parameter would likely have triggered the format feature (i.e. format html, format
129+ # json, format xml, etc). We are retaining this out of caution.
130+ def requested_aggregation_field ( field_name )
131+ return :content_format if field_name == 'format'
132+
133+ field_name . underscore . to_sym
134+ end
135+
136+ def requested_aggregations
137+ used_fields = context [ :tracers ] . first . log_data [ :used_fields ]
138+ used_fields . select { |field | field . start_with? ( 'Aggregations.' ) }
139+ . map { |field | requested_aggregation_field ( field . sub ( 'Aggregations.' , '' ) ) }
140+ end
141+
125142 # Long-term, we will probably want to define these fields discretely in RecordType. However, this might end up
126143 # adding confusion while we are maintaining deprecated fields in that class. We should refactor this either soon
127144 # after removing GraphQL V1 or as part of that work.
@@ -174,17 +191,19 @@ def source_deprecation_handler(query, new_source, old_source)
174191 end
175192
176193 def collapse_buckets ( es_aggs )
194+ return nil if es_aggs . nil? || es_aggs . empty?
195+
177196 {
178- access_to_files : es_aggs [ 'access_to_files' ] [ 'only_file_access' ] [ 'access_types' ] [ 'buckets' ] ,
179- contributors : es_aggs [ 'contributors' ] [ 'contributor_names' ] [ 'buckets' ] ,
180- source : es_aggs [ 'source' ] [ 'buckets' ] ,
181- subjects : es_aggs [ 'subjects' ] [ 'subject_names' ] [ 'buckets' ] ,
182- places : es_aggs [ 'places' ] [ 'only_spatial' ] [ 'place_names' ] [ 'buckets' ] ,
183- languages : es_aggs [ 'languages' ] [ 'buckets' ] ,
184- literary_form : es_aggs [ 'literary_form' ] [ 'buckets' ] ,
185- format : es_aggs [ 'content_format' ] [ 'buckets' ] ,
186- content_type : es_aggs [ 'content_type' ] [ 'buckets' ]
187- }
197+ access_to_files : es_aggs . dig ( 'access_to_files' , 'only_file_access' , 'access_types' , 'buckets' ) ,
198+ contributors : es_aggs . dig ( 'contributors' , 'contributor_names' , 'buckets' ) ,
199+ source : es_aggs . dig ( 'source' , 'buckets' ) ,
200+ subjects : es_aggs . dig ( 'subjects' , 'subject_names' , 'buckets' ) ,
201+ places : es_aggs . dig ( 'places' , 'only_spatial' , 'place_names' , 'buckets' ) ,
202+ languages : es_aggs . dig ( 'languages' , 'buckets' ) ,
203+ literary_form : es_aggs . dig ( 'literary_form' , 'buckets' ) ,
204+ format : es_aggs . dig ( 'content_format' , 'buckets' ) ,
205+ content_type : es_aggs . dig ( 'content_type' , 'buckets' )
206+ } . compact
188207 end
189208 end
190209end
0 commit comments