Debian Codegraph @

django/contrib/postgres/search.py

raw
1 from django.db.models import (
2 CharField,
3 Expression,
4 Field,
5 FloatField,
6 Func,
7 Lookup,
8 TextField,
9 Value,
10 )
11 from django.db.models.expressions import CombinedExpression, register_combinable_fields
12 from django.db.models.functions import Cast, Coalesce
13
14
15 class SearchVectorExact(Lookup):
16 lookup_name = "exact"
17
18 def process_rhs(self, qn, connection):
19 if not isinstance(self.rhs, (SearchQuery, CombinedSearchQuery)):
20 config = getattr(self.lhs, "config", None)
21 self.rhs = SearchQuery(self.rhs, config=config)
22 rhs, rhs_params = super().process_rhs(qn, connection)
23 return rhs, rhs_params
24
25 def as_sql(self, qn, connection):
26 lhs, lhs_params = self.process_lhs(qn, connection)
27 rhs, rhs_params = self.process_rhs(qn, connection)
28 params = lhs_params + rhs_params
29 return "%s @@ %s" % (lhs, rhs), params
30
31
32 class SearchVectorField(Field):
33 def db_type(self, connection):
34 return "tsvector"
35
36
37 class SearchQueryField(Field):
38 def db_type(self, connection):
39 return "tsquery"
40
41
42 class _Float4Field(Field):
43 def db_type(self, connection):
44 return "float4"
45
46
47 class SearchConfig(Expression):
48 def __init__(self, config):
49 super().__init__()
50 if not hasattr(config, "resolve_expression"):
51 config = Value(config)
52 self.config = config
53
54 @classmethod
55 def from_parameter(cls, config):
56 if config is None or isinstance(config, cls):
57 return config
58 return cls(config)
59
60 def get_source_expressions(self):
61 return [self.config]
62
63 def set_source_expressions(self, exprs):
64 (self.config,) = exprs
65
66 def as_sql(self, compiler, connection):
67 sql, params = compiler.compile(self.config)
68 return "%s::regconfig" % sql, params
69
70
71 class SearchVectorCombinable:
72 ADD = "||"
73
74 def _combine(self, other, connector, reversed):
75 if not isinstance(other, SearchVectorCombinable):
76 raise TypeError(
77 "SearchVector can only be combined with other SearchVector "
78 "instances, got %s." % type(other).__name__
79 )
80 if reversed:
81 return CombinedSearchVector(other, connector, self, self.config)
82 return CombinedSearchVector(self, connector, other, self.config)
83
84
85 register_combinable_fields(
86 SearchVectorField, SearchVectorCombinable.ADD, SearchVectorField, SearchVectorField
87 )
88
89
90 class SearchVector(SearchVectorCombinable, Func):
91 function = "to_tsvector"
92 arg_joiner = " || ' ' || "
93 output_field = SearchVectorField()
94
95 def __init__(self, *expressions, config=None, weight=None):
96 super().__init__(*expressions)
97 self.config = SearchConfig.from_parameter(config)
98 if weight is not None and not hasattr(weight, "resolve_expression"):
99 weight = Value(weight)
100 self.weight = weight
101
102 def resolve_expression(
103 self, query=None, allow_joins=True, reuse=None, summarize=False, for_save=False
104 ):
105 resolved = super().resolve_expression(
106 query, allow_joins, reuse, summarize, for_save
107 )
108 if self.config:
109 resolved.config = self.config.resolve_expression(
110 query, allow_joins, reuse, summarize, for_save
111 )
112 return resolved
113
114 def as_sql(self, compiler, connection, function=None, template=None):
115 clone = self.copy()
116 clone.set_source_expressions(
117 [
118 Coalesce(
119 (
120 expression
121 if isinstance(expression.output_field, (CharField, TextField))
122 else Cast(expression, TextField())
123 ),
124 Value(""),
125 )
126 for expression in clone.get_source_expressions()
127 ]
128 )
129 config_sql = None
130 config_params = []
131 if template is None:
132 if clone.config:
133 config_sql, config_params = compiler.compile(clone.config)
134 template = "%(function)s(%(config)s, %(expressions)s)"
135 else:
136 template = clone.template
137 sql, params = super(SearchVector, clone).as_sql(
138 compiler,
139 connection,
140 function=function,
141 template=template,
142 config=config_sql,
143 )
144 extra_params = []
145 if clone.weight:
146 weight_sql, extra_params = compiler.compile(clone.weight)
147 sql = "setweight({}, {})".format(sql, weight_sql)
148
149 return sql, config_params + params + extra_params
150
151
152 class CombinedSearchVector(SearchVectorCombinable, CombinedExpression):
153 def __init__(self, lhs, connector, rhs, config, output_field=None):
154 self.config = config
155 super().__init__(lhs, connector, rhs, output_field)
156
157
158 class SearchQueryCombinable:
159 BITAND = "&&"
160 BITOR = "||"
161
162 def _combine(self, other, connector, reversed):
163 if not isinstance(other, SearchQueryCombinable):
164 raise TypeError(
165 "SearchQuery can only be combined with other SearchQuery "
166 "instances, got %s." % type(other).__name__
167 )
168 if reversed:
169 return CombinedSearchQuery(other, connector, self, self.config)
170 return CombinedSearchQuery(self, connector, other, self.config)
171
172 # On Combinable, these are not implemented to reduce confusion with Q. In
173 # this case we are actually (ab)using them to do logical combination so
174 # it's consistent with other usage in Django.
175 def __or__(self, other):
176 return self._combine(other, self.BITOR, False)
177
178 def __ror__(self, other):
179 return self._combine(other, self.BITOR, True)
180
181 def __and__(self, other):
182 return self._combine(other, self.BITAND, False)
183
184 def __rand__(self, other):
185 return self._combine(other, self.BITAND, True)
186
187
188 class SearchQuery(SearchQueryCombinable, Func):
189 output_field = SearchQueryField()
190 SEARCH_TYPES = {
191 "plain": "plainto_tsquery",
192 "phrase": "phraseto_tsquery",
193 "raw": "to_tsquery",
194 "websearch": "websearch_to_tsquery",
195 }
196
197 def __init__(
198 self,
199 value,
200 output_field=None,
201 *,
202 config=None,
203 invert=False,
204 search_type="plain",
205 ):
206 self.function = self.SEARCH_TYPES.get(search_type)
207 if self.function is None:
208 raise ValueError("Unknown search_type argument '%s'." % search_type)
209 if not hasattr(value, "resolve_expression"):
210 value = Value(value)
211 expressions = (value,)
212 self.config = SearchConfig.from_parameter(config)
213 if self.config is not None:
214 expressions = (self.config,) + expressions
215 self.invert = invert
216 super().__init__(*expressions, output_field=output_field)
217
218 def as_sql(self, compiler, connection, function=None, template=None):
219 sql, params = super().as_sql(compiler, connection, function, template)
220 if self.invert:
221 sql = "!!(%s)" % sql
222 return sql, params
223
224 def __invert__(self):
225 clone = self.copy()
226 clone.invert = not self.invert
227 return clone
228
229 def __str__(self):
230 result = super().__str__()
231 return ("~%s" % result) if self.invert else result
232
233
234 class CombinedSearchQuery(SearchQueryCombinable, CombinedExpression):
235 def __init__(self, lhs, connector, rhs, config, output_field=None):
236 self.config = config
237 super().__init__(lhs, connector, rhs, output_field)
238
239 def __str__(self):
240 return "(%s)" % super().__str__()
241
242
243 class SearchRank(Func):
244 function = "ts_rank"
245 output_field = FloatField()
246
247 def __init__(
248 self,
249 vector,
250 query,
251 weights=None,
252 normalization=None,
253 cover_density=False,
254 ):
255 from .fields.array import ArrayField
256
257 if not hasattr(vector, "resolve_expression"):
258 vector = SearchVector(vector)
259 if not hasattr(query, "resolve_expression"):
260 query = SearchQuery(query)
261 expressions = (vector, query)
262 if weights is not None:
263 if not hasattr(weights, "resolve_expression"):
264 weights = Value(weights)
265 weights = Cast(weights, ArrayField(_Float4Field()))
266 expressions = (weights,) + expressions
267 if normalization is not None:
268 if not hasattr(normalization, "resolve_expression"):
269 normalization = Value(normalization)
270 expressions += (normalization,)
271 if cover_density:
272 self.function = "ts_rank_cd"
273 super().__init__(*expressions)
274
275
276 class SearchHeadline(Func):
277 function = "ts_headline"
278 template = "%(function)s(%(expressions)s%(options)s)"
279 output_field = TextField()
280
281 def __init__(
282 self,
283 expression,
284 query,
285 *,
286 config=None,
287 start_sel=None,
288 stop_sel=None,
289 max_words=None,
290 min_words=None,
291 short_word=None,
292 highlight_all=None,
293 max_fragments=None,
294 fragment_delimiter=None,
295 ):
296 if not hasattr(query, "resolve_expression"):
297 query = SearchQuery(query)
298 options = {
299 "StartSel": start_sel,
300 "StopSel": stop_sel,
301 "MaxWords": max_words,
302 "MinWords": min_words,
303 "ShortWord": short_word,
304 "HighlightAll": highlight_all,
305 "MaxFragments": max_fragments,
306 "FragmentDelimiter": fragment_delimiter,
307 }
308 self.options = {
309 option: value for option, value in options.items() if value is not None
310 }
311 expressions = (expression, query)
312 if config is not None:
313 config = SearchConfig.from_parameter(config)
314 expressions = (config,) + expressions
315 super().__init__(*expressions)
316
317 def as_sql(self, compiler, connection, function=None, template=None):
318 options_sql = ""
319 options_params = []
320 if self.options:
321 options_params.append(
322 ", ".join(
323 connection.ops.compose_sql(f"{option}=%s", [value])
324 for option, value in self.options.items()
325 )
326 )
327 options_sql = ", %s"
328 sql, params = super().as_sql(
329 compiler,
330 connection,
331 function=function,
332 template=template,
333 options=options_sql,
334 )
335 return sql, params + options_params
336
337
338 SearchVectorField.register_lookup(SearchVectorExact)
339
340
341 class TrigramBase(Func):
342 output_field = FloatField()
343
344 def __init__(self, expression, string, **extra):
345 if not hasattr(string, "resolve_expression"):
346 string = Value(string)
347 super().__init__(expression, string, **extra)
348
349
350 class TrigramWordBase(Func):
351 output_field = FloatField()
352
353 def __init__(self, string, expression, **extra):
354 if not hasattr(string, "resolve_expression"):
355 string = Value(string)
356 super().__init__(string, expression, **extra)
357
358
359 class TrigramSimilarity(TrigramBase):
360 function = "SIMILARITY"
361
362
363 class TrigramDistance(TrigramBase):
364 function = ""
365 arg_joiner = " <-> "
366
367
368 class TrigramWordDistance(TrigramWordBase):
369 function = ""
370 arg_joiner = " <<-> "
371
372
373 class TrigramStrictWordDistance(TrigramWordBase):
374 function = ""
375 arg_joiner = " <<<-> "
376
377
378 class TrigramWordSimilarity(TrigramWordBase):
379 function = "WORD_SIMILARITY"
380
381
382 class TrigramStrictWordSimilarity(TrigramWordBase):
383 function = "STRICT_WORD_SIMILARITY"