@@ -1397,21 +1397,29 @@ def perform_search(
1397
1397
search_term_map [term ] = f"CAPE.configs.{ value } "
1398
1398
query_val = {"$exists" : True }
1399
1399
1400
+ retval = []
1400
1401
if repconf .mongodb .enabled and query_val :
1401
1402
if term in hash_searches :
1402
1403
# The file details are uniq, and we store 1 to many. So where hash type is uniq, IDs are list
1403
1404
split_by = "," if "," in query_val else " "
1404
1405
query_val = {"$in" : [val .strip () for val in query_val .split (split_by )]}
1405
- # The file details are uniq, and we store 1 to many. So where hash type is uniq, IDs are list
1406
- file_docs = list (mongo_find (FILES_COLL , {hash_searches [term ]: query_val }, {"_task_ids" : 1 }))
1407
- if not file_docs :
1408
- return []
1409
- all_ids = []
1410
- for file_doc in file_docs :
1411
- all_ids .extend (file_doc ["_task_ids" ])
1412
- ids = sorted (list (set (all_ids )), reverse = True )[:search_limit ]
1413
- term = "ids"
1414
- mongo_search_query = {"info.id" : {"$in" : ids }}
1406
+ pipeline = [
1407
+ # Stages 1-5: Find, unwind, group, sort, limit IDs
1408
+ {"$match" : {hash_searches [term ]: query_filter_list }},
1409
+ {"$unwind" : "$_task_ids" },
1410
+ {"$group" : {"_id" : "$_task_ids" }},
1411
+ {"$sort" : {"_id" : - 1 }},
1412
+ {"$limit" : limit },
1413
+ # Stage 6: Join with the tasks collection
1414
+ {"$lookup" : {"from" : "analysis" , "localField" : "_id" , "foreignField" : "info.id" , "as" : "task_doc" }},
1415
+ # Stage 7: Unpack the joined doc
1416
+ {"$unwind" : "$task_doc" },
1417
+ # Stage 8: Make the task doc the new root
1418
+ {"$replaceRoot" : {"newRoot" : "$task_doc" }},
1419
+ # Stage 9: Add your custom projection
1420
+ {"$project" : perform_search_filters },
1421
+ ]
1422
+ retval = list (mongo_aggregate (FILES_COLL , pipeline ))
1415
1423
elif isinstance (search_term_map [term ], str ):
1416
1424
mongo_search_query = {search_term_map [term ]: query_val }
1417
1425
elif isinstance (search_term_map [term ], list ):
@@ -1426,7 +1434,8 @@ def perform_search(
1426
1434
if "target.file.sha256" in projection :
1427
1435
projection = dict (** projection )
1428
1436
projection [f"target.file.{ FILE_REF_KEY } " ] = 1
1429
- retval = list (mongo_find ("analysis" , mongo_search_query , projection , limit = search_limit ))
1437
+ if not retval :
1438
+ retval = list (mongo_find ("analysis" , mongo_search_query , projection , limit = search_limit ))
1430
1439
for doc in retval :
1431
1440
target_file = doc .get ("target" , {}).get ("file" , {})
1432
1441
if FILE_REF_KEY in target_file and "sha256" not in target_file :
0 commit comments