Full text support in peewee
Adds support for full text search in peewee with the creation of two new field types: `FullIndexedCharField` and `FullIndexedTextField`. Note that this change depends upon https://github.com/zzzeek/sqlalchemy/pull/339 [Delivers #137453279] [Delivers #137453317]
This commit is contained in:
parent
048f932094
commit
d89c79b92d
4 changed files with 105 additions and 2 deletions
|
@ -1,5 +1,5 @@
|
|||
from sqlalchemy import (Table, MetaData, Column, ForeignKey, Integer, String, Boolean, Text,
|
||||
DateTime, Date, BigInteger, Index)
|
||||
DateTime, Date, BigInteger, Index, text)
|
||||
from peewee import (PrimaryKeyField, CharField, BooleanField, DateTimeField, TextField,
|
||||
ForeignKeyField, BigIntegerField, IntegerField, DateField)
|
||||
|
||||
|
@ -28,6 +28,7 @@ def gen_sqlalchemy_metadata(peewee_model_list):
|
|||
meta = model._meta
|
||||
|
||||
all_indexes = set(meta.indexes)
|
||||
fulltext_indexes = []
|
||||
|
||||
columns = []
|
||||
for field in meta.sorted_fields:
|
||||
|
@ -60,6 +61,10 @@ def gen_sqlalchemy_metadata(peewee_model_list):
|
|||
else:
|
||||
raise RuntimeError('Unknown column type: %s' % field)
|
||||
|
||||
if hasattr(field, '__fulltext__'):
|
||||
# Add the fulltext index for the field, based on whether we are under MySQL or Postgres.
|
||||
fulltext_indexes.append(field.name)
|
||||
|
||||
for option_name in OPTIONS_TO_COPY:
|
||||
alchemy_option_name = (OPTION_TRANSLATIONS[option_name]
|
||||
if option_name in OPTION_TRANSLATIONS else option_name)
|
||||
|
@ -81,4 +86,11 @@ def gen_sqlalchemy_metadata(peewee_model_list):
|
|||
col_refs = [getattr(new_table.c, col_name) for col_name in col_names]
|
||||
Index(index_name, *col_refs, unique=unique)
|
||||
|
||||
for col_field_name in fulltext_indexes:
|
||||
index_name = '%s_%s__fulltext' % (meta.db_table, col_field_name)
|
||||
col_ref = getattr(new_table.c, col_field_name)
|
||||
Index(index_name, col_ref, postgresql_ops={col_field_name: 'gin_trgm_ops'},
|
||||
postgresql_using='gin',
|
||||
mysql_prefix='FULLTEXT')
|
||||
|
||||
return metadata
|
||||
|
|
Reference in a new issue