Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: change facet filter to overlap #267

Merged
merged 1 commit into from
Dec 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading