Skip to content

Commit

Permalink
Use DB to augment ES hits for related media with required info (#3408)
Browse files Browse the repository at this point in the history
  • Loading branch information
dhruvkb authored Dec 1, 2023
1 parent b60c4bb commit adb56f1
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
16 changes: 16 additions & 0 deletions api/api/views/media_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,18 @@ def _get_request_serializer(self, request):
return req_serializer

def get_db_results(self, results):
"""
Map ES hits to ORM model instances.
ORM instances have all necessary info needed for serializers whereas ES
hits only contain the subset of fields needed for indexing and search.
This function issues one query to the DB, using the ``identifier`` field
which is both unique and indexed, so it's quite performant.
:param results: the list of ES hits
:return: the corresponding list of ORM model instances
"""

identifiers = []
hits = []
for hit in results:
Expand Down Expand Up @@ -267,6 +279,10 @@ def related(self, request, identifier=None, *_, **__):

serializer_context = self.get_serializer_context()

serializer_class = self.get_serializer()
if serializer_class.needs_db:
results = self.get_db_results(results)

serializer = self.get_serializer(results, many=True, context=serializer_context)
return self.get_paginated_response(serializer.data)

Expand Down
4 changes: 4 additions & 0 deletions api/test/media_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,10 @@ def get_terms_set(res):
or result["creator"] == item["creator"]
), f"{terms_set} {get_terms_set(result)}/{result['creator']}-{item['creator']}"

assert result["license_version"] is not None
assert result["attribution"] is not None
assert result["creator_url"] is not None


def sensitive_search_and_detail(media_type):
search_res = requests.get(
Expand Down

0 comments on commit adb56f1

Please sign in to comment.