@@ -497,31 +497,51 @@ def search(
497
497
498
498
499
499
def related_media (uuid : str , index : str , filter_dead : bool ) -> list [Hit ]:
500
- """Given a UUID, find related search results based on title and tags."""
500
+ """
501
+ Given a UUID, finds 10 related search results based on title and tags.
502
+
503
+ Uses Match query for title or SimpleQueryString for tags.
504
+ If the item has no title and no tags, returns items by the same creator.
505
+ If the item has no title, no tags or no creator, returns empty list.
506
+
507
+ :param uuid: The UUID of the item to find related results for.
508
+ :param index: The Elasticsearch index to search (e.g. 'image')
509
+ :param filter_dead: Whether dead links should be removed.
510
+ :return: List of related results.
511
+ """
501
512
502
513
# Search the default index for the item itself as it might be sensitive.
503
514
item_search = Search (index = index )
504
- item_hit = item_search .query ("match" , identifier = uuid ).execute ().hits [0 ]
515
+ # TODO: remove `__keyword` after
516
+ # https://github.com/WordPress/openverse/pull/3143 is merged.
517
+ item_hit = item_search .query (Term (identifier__keyword = uuid )).execute ().hits [0 ]
505
518
506
519
# Match related using title.
507
520
title = item_hit .title
508
- title_query = SimpleQueryString (query = title , fields = ["title" ])
509
- related_query = title_query
521
+ tags = getattr (item_hit , "tags" , None )
522
+ creator = item_hit .creator
523
+
524
+ if not title and not tags :
525
+ if not creator :
526
+ return []
527
+ related_query = Term (creator__keyword = creator )
528
+ else :
529
+ related_query = None if not title else Match (title = title )
510
530
511
- # Match related using tags, if the item has any.
512
- if tags := getattr ( item_hit , "tags" , None ) :
513
- # Only use the first 10 tags
514
- tags = ", " .join ([tag .name for tag in tags [:10 ]])
515
- tags_query = SimpleQueryString (fields = ["tags.name" ], query = tags )
516
- related_query |= tags_query
531
+ # Match related using tags, if the item has any.
532
+ if tags :
533
+ # Only use the first 10 tags
534
+ tags = " | " .join ([tag .name for tag in tags [:10 ]])
535
+ tags_query = SimpleQueryString (fields = ["tags.name" ], query = tags )
536
+ related_query = related_query | tags_query if related_query else tags_query
517
537
518
538
# Search the filtered index for related items.
519
539
s = Search (index = f"{ index } -filtered" )
520
540
521
541
# Exclude the current item and mature content.
522
542
# TODO: remove `__keyword` after
523
543
# https://github.com/WordPress/openverse/pull/3143 is merged.
524
- s = s .query (related_query & ~ Match (identifier__keyword = uuid ) & ~ Term (mature = True ))
544
+ s = s .query (related_query & ~ Term (identifier__keyword = uuid ) & ~ Term (mature = True ))
525
545
# Exclude the dynamically disabled sources.
526
546
s = _exclude_filtered (s )
527
547
0 commit comments