From b5fb362259f96cef23e101d531dc6a1595d4085b Mon Sep 17 00:00:00 2001 From: Dhruv Bhanushali Date: Tue, 28 Nov 2023 23:33:45 +0400 Subject: [PATCH 1/2] Use DB to augment ES hits with required info --- api/api/views/media_views.py | 4 ++++ api/test/media_integration.py | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/api/api/views/media_views.py b/api/api/views/media_views.py index d003feec9fd..8c33a295b33 100644 --- a/api/api/views/media_views.py +++ b/api/api/views/media_views.py @@ -267,6 +267,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) diff --git a/api/test/media_integration.py b/api/test/media_integration.py index f77ae5af0eb..0a4563344ae 100644 --- a/api/test/media_integration.py +++ b/api/test/media_integration.py @@ -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( From 324fe92115bc388498adeadcc13325b38f37983a Mon Sep 17 00:00:00 2001 From: Dhruv Bhanushali Date: Fri, 1 Dec 2023 15:33:37 +0400 Subject: [PATCH 2/2] Document `get_db_results` --- api/api/views/media_views.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/api/api/views/media_views.py b/api/api/views/media_views.py index 8c33a295b33..906e5974700 100644 --- a/api/api/views/media_views.py +++ b/api/api/views/media_views.py @@ -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: