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(