@@ -226,41 +226,16 @@ def search(
226226 nhs_number = nhs_number ,
227227 custodian = custodian ,
228228 pointer_types = pointer_types ,
229+ categories = categories ,
229230 )
230231
231232 key_conditions = ["patient_key = :patient_key" ]
232233 filter_expressions = []
233234 expression_names = {}
234235 expression_values = {":patient_key" : f"P#{ nhs_number } " }
235236
236- if len (pointer_types ) == 1 :
237- # Optimisation for single pointer type
238- category_id , type_id = _get_sk_ids_for_type (pointer_types [0 ])
239- patient_sort = f"C#{ category_id } #T#{ type_id } "
240- key_conditions .append ("begins_with(patient_sort, :patient_sort)" )
241- expression_values [":patient_sort" ] = patient_sort
242- else :
243- # Handle single/multiple categories and pointer types with filter expressions
244- if len (categories ) == 1 :
245- split_category = categories [0 ].split ("|" )
246- category_id = (
247- SYSTEM_SHORT_IDS [split_category [0 ]] + "-" + split_category [1 ]
248- )
249- patient_sort = f"C#{ category_id } "
250- key_conditions .append ("begins_with(patient_sort, :patient_sort)" )
251- expression_values [":patient_sort" ] = patient_sort
252-
253- if len (categories ) > 1 :
254- expression_names ["#category" ] = "category"
255- category_filters = [
256- f"#category = :category_{ i } " for i in range (len (categories ))
257- ]
258- category_filter_values = {
259- f":category_{ i } " : categories [i ] for i in range (len (categories ))
260- }
261- filter_expressions .append (f"({ ' OR ' .join (category_filters )} )" )
262- expression_values .update (category_filter_values )
263-
237+ # Add pointer_types filter if provided
238+ if pointer_types :
264239 expression_names ["#pointer_type" ] = "type"
265240 types_filters = [
266241 f"#pointer_type = :type_{ i } " for i in range (len (pointer_types ))
@@ -271,6 +246,18 @@ def search(
271246 filter_expressions .append (f"({ ' OR ' .join (types_filters )} )" )
272247 expression_values .update (types_filter_values )
273248
249+ # Add categories filter if provided
250+ if categories :
251+ expression_names ["#category" ] = "category"
252+ category_filters = [
253+ f"#category = :category_{ i } " for i in range (len (categories ))
254+ ]
255+ category_filter_values = {
256+ f":category_{ i } " : categories [i ] for i in range (len (categories ))
257+ }
258+ filter_expressions .append (f"({ ' OR ' .join (category_filters )} )" )
259+ expression_values .update (category_filter_values )
260+
274261 if custodian :
275262 logger .log (
276263 LogReference .REPOSITORY016 ,
0 commit comments