From 0c9d0121173f0174b30e128a389a2f15bbe6e214 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matthias=20Schl=C3=B6gl?= Date: Fri, 8 Nov 2024 08:58:08 +0100 Subject: [PATCH] fix: add unaccent to the search filters to allow for search with base characters instead of diacritics we add posgres unaccent to filters resolves #353 --- apis_ontology/filtersets.py | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/apis_ontology/filtersets.py b/apis_ontology/filtersets.py index a9392fe..9f21a41 100644 --- a/apis_ontology/filtersets.py +++ b/apis_ontology/filtersets.py @@ -29,7 +29,9 @@ def remove_quotes(token): def trigram_search_filter_person(queryset, name, value): - return trigram_search_filter(queryset, ["surname", "forename"], value) + return trigram_search_filter( + queryset, ["surname__unaccent", "forename__unaccent"], value + ) def trigram_search_filter_institution(queryset, name, value): @@ -51,7 +53,7 @@ def trigram_search_filter(queryset, fields, value): trig_vector = Greatest(*trig_vector_list, None) return ( queryset.annotate(similarity=trig_vector) - .filter(similarity__gt=0.4) + .filter(similarity__gt=0.5) .order_by("-similarity") ) @@ -75,11 +77,17 @@ class Meta(AbstractEntityFilterSet.Meta): models.CharField: { "filter_class": django_filters.CharFilter, "extra": lambda f: { - "lookup_expr": "icontains", + "lookup_expr": "unaccent__icontains", }, }, } + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + for filter in self.filters.values(): + if hasattr(filter, "label") and filter.label and "unaccent" in filter.label: + filter.label = filter.label.replace("unaccent ", "") + class PersonFilterSet(LegacyStuffMixinFilterSet): collection = django_filters.ModelMultipleChoiceFilter(