1 from django.db.models import Func
2 from django.contrib.postgres.search import SearchVector, SearchQuery, SearchQueryField, SearchHeadline as SH
6 class UnaccentTSVector(Func):
8 template = '%(function)s(%(expressions)s::text)::tsvector'
15 class ConcatTSVector(Func):
17 template = '%(function)s(%(expressions)s)::tsvector'
20 class UnaccentTSQuery(Func):
22 template = '%(function)s(%(expressions)s::text)::tsquery'
23 output_field = SearchQueryField()
27 function='to_tsvector'
28 template = '''unaccent(
29 %(function)s('polish', %(expressions)s)::text)::tsvector ||
32 unaccent(%(expressions)s)
36 def build_search_vector(*fields):
40 def build_search_query(*fields, **kwargs):
41 return UnaccentTSQuery(SearchQuery(*fields, **kwargs))
45 class SearchHeadline(SH):
60 fragment_delimiter=None,
63 "StartSel": start_sel,
65 "MaxWords": max_words,
66 "MinWords": min_words,
67 "ShortWord": short_word,
68 "HighlightAll": highlight_all,
69 "MaxFragments": max_fragments,
70 "FragmentDelimiter": fragment_delimiter,
73 option: value for option, value in options.items() if value is not None
75 expressions = (expression, query)
76 if config is not None:
77 config = SearchConfig.from_parameter(config)
78 expressions = (config,) + expressions
79 Func.__init__(self, *expressions)