1 from django.db.models import Func
2 from django.contrib.postgres.search import SearchQuery, SearchVectorField
5 class UnaccentSearchQuery(SearchQuery):
7 The idea is to run unaccent *after* the query is already passed through the language dictionary.
9 def as_sql(self, *args, **kwargs):
10 sql, params = super().as_sql(*args, **kwargs)
11 sql = f'unaccent({sql}::text)::tsquery'
15 class UnaccentSearchVector(Func):
17 We do the indexing twice, to account for non-diacritic versions.
18 For example: user enters 'róże' -> stem to 'róża' -> unaccent to 'roza'.
19 But user enters 'roze' -> stem leaves it as is, so we need original form in the vector.
21 function='to_tsvector'
22 template = '''unaccent(
23 %(function)s('polish', %(expressions)s)::text)::tsvector ||
26 unaccent(%(expressions)s)
28 output_field = SearchVectorField()