20
20
from src .core .json_typing import JsonCollation , JsonEntry , JsonLibary , JsonTag
21
21
from src .core .utils .str import strip_punctuation
22
22
from src .core .utils .web import strip_web_protocol
23
+ from src .core .enums import SearchMode
23
24
from src .core .constants import (
24
25
BACKUP_FOLDER_NAME ,
25
26
COLLAGE_FOLDER_NAME ,
@@ -1290,7 +1291,12 @@ def get_entry_id_from_filepath(self, filename: Path):
1290
1291
return - 1
1291
1292
1292
1293
def search_library (
1293
- self , query : str = None , entries = True , collations = True , tag_groups = True
1294
+ self ,
1295
+ query : str = None ,
1296
+ entries = True ,
1297
+ collations = True ,
1298
+ tag_groups = True ,
1299
+ search_mode = SearchMode .AND ,
1294
1300
) -> list [tuple [ItemType , int ]]:
1295
1301
"""
1296
1302
Uses a search query to generate a filtered results list.
@@ -1300,7 +1306,7 @@ def search_library(
1300
1306
# self.filtered_entries.clear()
1301
1307
results : list [tuple [ItemType , int ]] = []
1302
1308
collations_added = []
1303
-
1309
+ # print(f"Searching Library with query: {query} search_mode: {search_mode}")
1304
1310
if query :
1305
1311
# start_time = time.time()
1306
1312
query = query .strip ().lower ()
@@ -1320,6 +1326,7 @@ def search_library(
1320
1326
1321
1327
# Preprocess the Tag terms.
1322
1328
if query_words :
1329
+ # print(query_words, self._tag_strings_to_id_map)
1323
1330
for i , term in enumerate (query_words ):
1324
1331
for j , term in enumerate (query_words ):
1325
1332
if (
@@ -1328,6 +1335,8 @@ def search_library(
1328
1335
in self ._tag_strings_to_id_map
1329
1336
):
1330
1337
all_tag_terms .append (" " .join (query_words [i : j + 1 ]))
1338
+ # print(all_tag_terms)
1339
+
1331
1340
# This gets rid of any accidental term inclusions because they were words
1332
1341
# in another term. Ex. "3d" getting added in "3d art"
1333
1342
for i , term in enumerate (all_tag_terms ):
@@ -1403,36 +1412,8 @@ def search_library(
1403
1412
# elif query in entry.filename.lower():
1404
1413
# self.filtered_entries.append(index)
1405
1414
elif entry_tags :
1406
- # For each verified, extracted Tag term.
1407
- failure_to_union_terms = False
1408
- for term in all_tag_terms :
1409
- # If the term from the previous loop was already verified:
1410
- if not failure_to_union_terms :
1411
- cluster : set = set ()
1412
- # Add the immediate associated Tags to the set (ex. Name, Alias hits)
1413
- # Since this term could technically map to multiple IDs, iterate over it
1414
- # (You're 99.9999999% likely to just get 1 item)
1415
- for id in self ._tag_strings_to_id_map [term ]:
1416
- cluster .add (id )
1417
- cluster = cluster .union (
1418
- set (self .get_tag_cluster (id ))
1419
- )
1420
- # print(f'Full Cluster: {cluster}')
1421
- # For each of the Tag IDs in the term's ID cluster:
1422
- for t in cluster :
1423
- # Assume that this ID from the cluster is not in the Entry.
1424
- # Wait to see if proven wrong.
1425
- failure_to_union_terms = True
1426
- # If the ID actually is in the Entry,
1427
- if t in entry_tags :
1428
- # There wasn't a failure to find one of the term's cluster IDs in the Entry.
1429
- # There is also no more need to keep checking the rest of the terms in the cluster.
1430
- failure_to_union_terms = False
1431
- # print(f'FOUND MATCH: {t}')
1432
- break
1433
- # print(f'\tFailure to Match: {t}')
1434
- # If there even were tag terms to search through AND they all match an entry
1435
- if all_tag_terms and not failure_to_union_terms :
1415
+ # function to add entry to results
1416
+ def add_entry (entry : Entry ):
1436
1417
# self.filter_entries.append()
1437
1418
# self.filtered_file_list.append(file)
1438
1419
# results.append((SearchItemType.ENTRY, entry.id))
@@ -1457,6 +1438,54 @@ def search_library(
1457
1438
if not added :
1458
1439
results .append ((ItemType .ENTRY , entry .id ))
1459
1440
1441
+ if search_mode == SearchMode .AND : # Include all terms
1442
+ # For each verified, extracted Tag term.
1443
+ failure_to_union_terms = False
1444
+ for term in all_tag_terms :
1445
+ # If the term from the previous loop was already verified:
1446
+ if not failure_to_union_terms :
1447
+ cluster : set = set ()
1448
+ # Add the immediate associated Tags to the set (ex. Name, Alias hits)
1449
+ # Since this term could technically map to multiple IDs, iterate over it
1450
+ # (You're 99.9999999% likely to just get 1 item)
1451
+ for id in self ._tag_strings_to_id_map [term ]:
1452
+ cluster .add (id )
1453
+ cluster = cluster .union (
1454
+ set (self .get_tag_cluster (id ))
1455
+ )
1456
+ # print(f'Full Cluster: {cluster}')
1457
+ # For each of the Tag IDs in the term's ID cluster:
1458
+ for t in cluster :
1459
+ # Assume that this ID from the cluster is not in the Entry.
1460
+ # Wait to see if proven wrong.
1461
+ failure_to_union_terms = True
1462
+ # If the ID actually is in the Entry,
1463
+ if t in entry_tags :
1464
+ # There wasn't a failure to find one of the term's cluster IDs in the Entry.
1465
+ # There is also no more need to keep checking the rest of the terms in the cluster.
1466
+ failure_to_union_terms = False
1467
+ # print(f"FOUND MATCH: {t}")
1468
+ break
1469
+ # print(f'\tFailure to Match: {t}')
1470
+ # # failure_to_union_terms is used to determine if all terms in the query were found in the entry.
1471
+ # # If there even were tag terms to search through AND they all match an entry
1472
+ if all_tag_terms and not failure_to_union_terms :
1473
+ add_entry (entry )
1474
+
1475
+ if search_mode == SearchMode .OR : # Include any terms
1476
+ # For each verified, extracted Tag term.
1477
+ for term in all_tag_terms :
1478
+ # Add the immediate associated Tags to the set (ex. Name, Alias hits)
1479
+ # Since this term could technically map to multiple IDs, iterate over it
1480
+ # (You're 99.9999999% likely to just get 1 item)
1481
+ for id in self ._tag_strings_to_id_map [term ]:
1482
+ # If the ID actually is in the Entry,
1483
+ if id in entry_tags :
1484
+ # check if result already contains the entry
1485
+ if (ItemType .ENTRY , entry .id ) not in results :
1486
+ add_entry (entry )
1487
+ break
1488
+
1460
1489
# sys.stdout.write(
1461
1490
# f'\r[INFO][FILTER]: {len(self.filtered_file_list)} matches found')
1462
1491
# sys.stdout.flush()
0 commit comments