Skip to content

Commit 07d6b65

Browse files
committed
Add handling of the case when title and tags are absent
1 parent d7a13a2 commit 07d6b65

File tree

1 file changed

+31
-11
lines changed

1 file changed

+31
-11
lines changed

api/api/controllers/search_controller.py

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -497,31 +497,51 @@ def search(
497497

498498

499499
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+
"""
501512

502513
# Search the default index for the item itself as it might be sensitive.
503514
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]
505518

506519
# Match related using title.
507520
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)
510530

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
517537

518538
# Search the filtered index for related items.
519539
s = Search(index=f"{index}-filtered")
520540

521541
# Exclude the current item and mature content.
522542
# TODO: remove `__keyword` after
523543
# 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))
525545
# Exclude the dynamically disabled sources.
526546
s = _exclude_filtered(s)
527547

0 commit comments

Comments
 (0)