Skip to content

Commit

Permalink
fix: add unaccent to the search filters
Browse files Browse the repository at this point in the history
to allow for search with base characters instead
of diacritics we add posgres unaccent to filters
resolves #353
  • Loading branch information
sennierer committed Nov 8, 2024
1 parent 63e0c87 commit 0c9d012
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions apis_ontology/filtersets.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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")
)

Expand All @@ -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(
Expand Down

0 comments on commit 0c9d012

Please sign in to comment.