From a5b6b9ecec5d02f8231882f4a30554904d4064de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matthias=20Schl=C3=B6gl?= Date: Thu, 5 Dec 2024 14:37:24 +0100 Subject: [PATCH] fix: change facet filter to overlap resolves gitlab issues #145 #188 #184 #186 --- apis_ontology/api/filters.py | 24 ++++++++++++++++-------- apis_ontology/api/views.py | 5 +++++ 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/apis_ontology/api/filters.py b/apis_ontology/api/filters.py index f74dbd8..d715f4a 100644 --- a/apis_ontology/api/filters.py +++ b/apis_ontology/api/filters.py @@ -16,6 +16,14 @@ logger = logging.getLogger(__name__) +class MultipleChoiceOverlap(django_filters.MultipleChoiceFilter): + def filter(self, qs, value): + if len(value) == 0: + return qs + filter = {f"{self.field_name}__overlap": value} + return qs.filter(**filter) + + class WorkPreviewSearchFilter(django_filters.FilterSet): text_filter = django_filters.CharFilter( field_name=[ @@ -27,23 +35,23 @@ class WorkPreviewSearchFilter(django_filters.FilterSet): ), method=fuzzy_search_unaccent_trigram, ) - facet_language = django_filters.MultipleChoiceFilter( + facet_language = MultipleChoiceOverlap( field_name="facet_language", label=_("Language of the expression."), - lookup_expr="icontains", + lookup_expr="overlap", choices=Expression.LanguagesIso6393.choices, ) - facet_topic = django_filters.MultipleChoiceFilter( + facet_topic = MultipleChoiceOverlap( field_name="facet_topic", label=_("Topic of the expression."), - lookup_expr="icontains", + lookup_expr="overlap", choices=Topic.objects.all().values_list("name", "name"), ) - facet_work_type = django_filters.MultipleChoiceFilter( - field_name="work_type", + facet_work_type = MultipleChoiceOverlap( + field_name="work_type_names", label=_("Type of the work."), - lookup_expr="icontains", - choices=WorkType.objects.all().values_list("id", "name"), + lookup_expr="overlap", + choices=WorkType.objects.all().values_list("name", "name"), ) start_year = django_filters.NumberFilter( field_name="min_year", diff --git a/apis_ontology/api/views.py b/apis_ontology/api/views.py index 7c71ce8..6c5df47 100644 --- a/apis_ontology/api/views.py +++ b/apis_ontology/api/views.py @@ -350,6 +350,10 @@ def get_queryset(self): ) ) ) + work_type_names = WorkType.objects.filter( + triple_set_from_obj__subj_id=OuterRef("pk"), + triple_set_from_obj__prop__name_forward__in=["has type"], + ).values_list("name", flat=True) expression_publisher = Organisation.objects.filter( triple_set_from_subj__obj_id=OuterRef("pk"), @@ -412,6 +416,7 @@ def get_queryset(self): .annotate( expression_data=ArraySubquery(related_expressions), work_type=ArraySubquery(work_types), + work_type_names=ArraySubquery(work_type_names), facet_language=ArraySubquery(facet_languages), facet_topic=ArraySubquery(facet_topics), min_year=Subquery(