-
Notifications
You must be signed in to change notification settings - Fork 44
/
__init__.py
468 lines (369 loc) · 15.3 KB
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
import os
from functools import reduce
import sqlalchemy as sa
from sqlalchemy import event
from sqlalchemy.ext.compiler import compiles
from sqlalchemy.schema import DDL, DDLElement
from sqlalchemy.sql.expression import Executable
from sqlalchemy_utils import TSVectorType
from .vectorizers import Vectorizer
__version__ = "2.0.0"
vectorizer = Vectorizer()
"""
An instance of :class:`Vectorizer` that keeps a track of the registered vectorizers. Use
this as a decorator to register a function as a vectorizer.
"""
class SearchQueryMixin:
def search(self, search_query, vector=None, regconfig=None, sort=False):
"""
Search given query with full text search.
:param search_query: the search query
:param vector: search vector to use
:param regconfig: postgresql regconfig to be used
:param sort: order results by relevance (quality of hit)
"""
return search(self, search_query, vector=vector, regconfig=regconfig, sort=sort)
def inspect_search_vectors(entity):
return [
getattr(entity, key).property.columns[0]
for key, column in sa.inspect(entity).columns.items()
if isinstance(column.type, TSVectorType)
]
def search(query, search_query, vector=None, regconfig=None, sort=False):
"""
Search given query with full text search.
:param search_query: the search query
:param vector: search vector to use
:param regconfig: postgresql regconfig to be used
:param sort: Order the results by relevance. This uses `cover density`_ ranking
algorithm (``ts_rank_cd``) for sorting.
.. _cover density: https://www.postgresql.org/docs/devel/textsearch-controls.html#TEXTSEARCH-RANKING
"""
if not search_query.strip():
return query
if vector is None:
entity = query.column_descriptions[0]["entity"]
search_vectors = inspect_search_vectors(entity)
vector = search_vectors[0]
if regconfig is None:
regconfig = search_manager.options["regconfig"]
query = query.filter(
vector.op("@@")(sa.func.parse_websearch(regconfig, search_query))
)
if sort:
query = query.order_by(
sa.desc(sa.func.ts_rank_cd(vector, sa.func.parse_websearch(search_query)))
)
return query.params(term=search_query)
class SQLConstruct:
def __init__(self, tsvector_column, indexed_columns=None, options=None):
self.table = tsvector_column.table
self.tsvector_column = tsvector_column
self.options = self.init_options(options)
if indexed_columns:
self.indexed_columns = list(indexed_columns)
elif hasattr(self.tsvector_column.type, "columns"):
self.indexed_columns = list(self.tsvector_column.type.columns)
else:
self.indexed_columns = None
def init_options(self, options=None):
if not options:
options = {}
for key, value in SearchManager.default_options.items():
try:
option = self.tsvector_column.type.options[key]
except (KeyError, AttributeError):
option = value
options.setdefault(key, option)
return options
@property
def table_name(self):
if self.table.schema:
return f'{self.table.schema}."{self.table.name}"'
else:
return '"' + self.table.name + '"'
@property
def search_function_name(self):
return self.options["search_trigger_function_name"].format(
table=self.table.name, column=self.tsvector_column.name
)
@property
def search_trigger_name(self):
return self.options["search_trigger_name"].format(
table=self.table.name, column=self.tsvector_column.name
)
def column_vector(self, column):
value = sa.text(f"NEW.{sa.column(column.name)}")
try:
vectorizer_func = vectorizer[column]
except KeyError:
pass
else:
value = vectorizer_func(value)
value = sa.func.coalesce(value, sa.text("''"))
value = sa.func.to_tsvector(sa.literal(self.options["regconfig"]), value)
if column.name in self.options["weights"]:
weight = self.options["weights"][column.name]
value = sa.func.setweight(value, weight)
return value
def search_vector(self, compiler):
vectors = (
self.column_vector(getattr(self.table.c, column_name))
for column_name in self.indexed_columns
)
concatenated = reduce(lambda x, y: x.op("||")(y), vectors)
return compiler.sql_compiler.process(concatenated, literal_binds=True)
class CreateSearchFunctionSQL(SQLConstruct, DDLElement, Executable):
pass
@compiles(CreateSearchFunctionSQL)
def compile_create_search_function_sql(element, compiler):
return f"""CREATE FUNCTION
{element.search_function_name}() RETURNS TRIGGER AS $$
BEGIN
NEW.{element.tsvector_column.name} = {element.search_vector(compiler)};
RETURN NEW;
END
$$ LANGUAGE 'plpgsql';
"""
class CreateSearchTriggerSQL(SQLConstruct, DDLElement, Executable):
@property
def search_trigger_function_with_trigger_args(self):
if self.options["weights"] or any(
getattr(self.table.c, column) in vectorizer
for column in self.indexed_columns
):
return self.search_function_name + "()"
return "tsvector_update_trigger({arguments})".format(
arguments=", ".join(
[self.tsvector_column.name, "'%s'" % self.options["regconfig"]]
+ self.indexed_columns
)
)
@compiles(CreateSearchTriggerSQL)
def compile_create_search_trigger_sql(element, compiler):
return (
f"CREATE TRIGGER {element.search_trigger_name}"
f" BEFORE UPDATE OR INSERT ON {element.table_name}"
" FOR EACH ROW EXECUTE PROCEDURE"
f" {element.search_trigger_function_with_trigger_args}"
)
class DropSearchFunctionSQL(SQLConstruct, DDLElement, Executable):
pass
@compiles(DropSearchFunctionSQL)
def compile_drop_search_function_sql(element, compiler):
return "DROP FUNCTION IF EXISTS %s()" % element.search_function_name
class DropSearchTriggerSQL(SQLConstruct, DDLElement, Executable):
pass
@compiles(DropSearchTriggerSQL)
def compile_drop_search_trigger_sql(element, compiler):
return (
f"DROP TRIGGER IF EXISTS {element.search_trigger_name} ON {element.table_name}"
)
class SearchManager:
default_options = {
"search_trigger_name": "{table}_{column}_trigger",
"search_trigger_function_name": "{table}_{column}_update",
"regconfig": "pg_catalog.english",
"weights": (),
"auto_index": True,
}
def __init__(self, options={}):
self.options = self.default_options
self.options.update(options)
self.processed_columns = []
self.classes = set()
self.listeners = []
def option(self, column, name):
try:
return column.type.options[name]
except (AttributeError, KeyError):
return self.options[name]
def inspect_columns(self, table):
"""
Inspects all searchable columns for given class.
:param table: SQLAlchemy Table
"""
return [column for column in table.c if isinstance(column.type, TSVectorType)]
def append_index(self, cls, column):
sa.Index(
"_".join(("ix", column.table.name, column.name)),
column,
postgresql_using="gin",
)
def process_mapper(self, mapper, cls):
columns = self.inspect_columns(mapper.persist_selectable)
for column in columns:
if column in self.processed_columns:
continue
if self.option(column, "auto_index"):
self.append_index(cls, column)
self.processed_columns.append(column)
def add_listener(self, args):
self.listeners.append(args)
event.listen(*args)
def remove_listeners(self):
for listener in self.listeners:
event.remove(*listener)
self.listeners = []
def attach_ddl_listeners(self):
# Remove all previously added listeners, so that same listener don't
# get added twice in situations where class configuration happens in
# multiple phases (issue #31).
self.remove_listeners()
for column in self.processed_columns:
# This sets up the trigger that keeps the tsvector column up to
# date.
if column.type.columns:
table = column.table
if self.option(column, "weights") or vectorizer.contains_tsvector(
column
):
self.add_listener(
(table, "after_create", CreateSearchFunctionSQL(column))
)
self.add_listener(
(table, "after_drop", DropSearchFunctionSQL(column))
)
self.add_listener(
(table, "after_create", CreateSearchTriggerSQL(column))
)
search_manager = SearchManager()
def sync_trigger(
conn, table_name, tsvector_column, indexed_columns, metadata=None, options=None
):
"""Synchronize the search trigger and trigger function for the given table and
search vector column. Internally, this function executes the following SQL
queries:
- Drop the search trigger for the given table and column if it exists.
- Drop the search function for the given table and column if it exists.
- Create the search function for the given table and column.
- Create the search trigger for the given table and column.
- Update all rows for the given search vector by executing a column=column update
query for the given table.
Example::
from sqlalchemy_searchable import sync_trigger
sync_trigger(
conn,
'article',
'search_vector',
['name', 'content']
)
This function is especially useful when working with Alembic migrations. In the
following example, we add a ``content`` column to the ``article`` table and then
synchronize the trigger to contain this new column::
from alembic import op
from sqlalchemy_searchable import sync_trigger
def upgrade():
conn = op.get_bind()
op.add_column('article', sa.Column('content', sa.Text))
sync_trigger(conn, 'article', 'search_vector', ['name', 'content'])
# ... same for downgrade
If you are using vectorizers, you need to initialize them in your migration
file and pass them to this function::
import sqlalchemy as sa
from alembic import op
from sqlalchemy.dialects.postgresql import HSTORE
from sqlalchemy_searchable import sync_trigger, vectorizer
def upgrade():
vectorizer.clear()
conn = op.get_bind()
op.add_column('article', sa.Column('name_translations', HSTORE))
metadata = sa.MetaData(bind=conn)
articles = sa.Table('article', metadata, autoload=True)
@vectorizer(articles.c.name_translations)
def hstore_vectorizer(column):
return sa.cast(sa.func.avals(column), sa.Text)
op.add_column('article', sa.Column('content', sa.Text))
sync_trigger(
conn,
'article',
'search_vector',
['name_translations', 'content'],
metadata=metadata
)
# ... same for downgrade
:param conn: SQLAlchemy Connection object
:param table_name: name of the table to apply search trigger syncing
:param tsvector_column:
TSVector typed column which is used as the search index column
:param indexed_columns:
Full text indexed column names as a list
:param metadata:
Optional SQLAlchemy metadata object that is being used for autoloaded
Table. If None is given, then a new MetaData object is initialized within
this function.
:param options: Dictionary of configuration options
"""
if metadata is None:
metadata = sa.MetaData()
table = sa.Table(table_name, metadata, autoload_with=conn)
params = dict(
tsvector_column=getattr(table.c, tsvector_column),
indexed_columns=indexed_columns,
options=options,
)
classes = [
DropSearchTriggerSQL,
DropSearchFunctionSQL,
CreateSearchFunctionSQL,
CreateSearchTriggerSQL,
]
for class_ in classes:
conn.execute(class_(**params))
update_sql = table.update().values(
{indexed_columns[0]: sa.text(indexed_columns[0])}
)
conn.execute(update_sql)
def drop_trigger(conn, table_name, tsvector_column, metadata=None, options=None):
"""
Drop the search trigger and trigger function for the given table and
search vector column. Internally, this function executes the following SQL
queries:
- Drop the search trigger for the given table if it exists.
- Drop the search function for the given table if it exists.
Example::
from alembic import op
from sqlalchemy_searchable import drop_trigger
def downgrade():
conn = op.get_bind()
drop_trigger(conn, 'article', 'search_vector')
op.drop_index('ix_article_search_vector', table_name='article')
op.drop_column('article', 'search_vector')
:param conn: SQLAlchemy Connection object
:param table_name: name of the table to apply search trigger dropping
:param tsvector_column:
TSVector typed column which is used as the search index column
:param metadata:
Optional SQLAlchemy metadata object that is being used for autoloaded
Table. If None is given, then a new MetaData object is initialized within
this function.
:param options: Dictionary of configuration options
"""
if metadata is None:
metadata = sa.MetaData()
table = sa.Table(table_name, metadata, autoload_with=conn)
params = dict(tsvector_column=getattr(table.c, tsvector_column), options=options)
classes = [
DropSearchTriggerSQL,
DropSearchFunctionSQL,
]
for class_ in classes:
conn.execute(class_(**params))
path = os.path.dirname(os.path.abspath(__file__))
with open(os.path.join(path, "expressions.sql")) as file:
sql_expressions = DDL(file.read())
def make_searchable(metadata, mapper=sa.orm.Mapper, manager=search_manager, options={}):
"""
Configure SQLAlchemy-Searchable for given SQLAlchemy metadata object.
:param metadata: SQLAlchemy metadata object
:param options: Dictionary of configuration options
"""
manager.options.update(options)
event.listen(mapper, "instrument_class", manager.process_mapper)
event.listen(mapper, "after_configured", manager.attach_ddl_listeners)
event.listen(metadata, "before_create", sql_expressions)
def remove_listeners(metadata, manager=search_manager, mapper=sa.orm.Mapper):
event.remove(mapper, "instrument_class", manager.process_mapper)
event.remove(mapper, "after_configured", manager.attach_ddl_listeners)
manager.remove_listeners()
event.remove(metadata, "before_create", sql_expressions)