CREATE table person ( ID INTEGER PRIMARY KEY,name TEXT );CREATE INDEX person_IDx ON person USING GIN (to_tsvector('simple',name));
现在,在使用ORM时,如何使用sqlAlchemy执行第二部分:
class Person(db.Model): ID = db.Column(db.Integer,primary_key=True) name = db.Column(db.String)解决方法 您可以使用__table_args__中的索引创建索引.我还使用一个函数来创建ts_vector,以便在需要多个字段时使其更加整洁和可重用.如下所示:
from sqlalchemy.dialects import postgresqldef create_tsvector(*args): exp = args[0] for e in args[1:]: exp += ' ' + e return func.to_tsvector('english',exp)class Person(db.Model): ID = db.Column(db.Integer,primary_key=True) name = db.Column(db.String) __ts_vector__ = create_tsvector( cast(func.coalesce(name,''),postgresql.TEXT) ) __table_args__ = ( Index( 'IDx_person_fts',__ts_vector__,postgresql_using='gin' ) )
更新:
使用索引的示例查询:
query = Person.__ts_vector__.match(Expressions,postgresql_regconfig='english')people = query.all()总结
以上是内存溢出为你收集整理的在PostgreSQL上使用SQLAlchemy创建全文搜索索引全部内容,希望文章能够帮你解决在PostgreSQL上使用SQLAlchemy创建全文搜索索引所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)