Skip to content

Commit

Permalink
fix: change facet filter to overlap
Browse files Browse the repository at this point in the history
resolves gitlab issues #145 #188 #184 #186
  • Loading branch information
sennierer committed Dec 5, 2024
1 parent 5febcf9 commit d366504
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 8 deletions.
24 changes: 16 additions & 8 deletions apis_ontology/api/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -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=[
Expand All @@ -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",
Expand Down
5 changes: 5 additions & 0 deletions apis_ontology/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit d366504

Please sign in to comment.