Python SQLAlchemy在Postgres中进行全文搜索

时间:2017-08-08 19:50:09

标签: python sqlalchemy

我有一个Postgres数据库,并尝试通过SQLAlchemy实现全文搜索。我跟随文档,但提供的示例代码导致错误,我无法通过它。

我可以通过postgres命令行实现全文搜索,但我无法让SQLAlchemy一起玩。

以下代码基本上直接来自文档https://sqlalchemy-searchable.readthedocs.io/en/latest/installation.html (我添加了一个省略的库,将库名称添加到create_engine的调用中,将括号括在python3的打印语句周围)

非常感谢任何帮助!

代码:

import sqlalchemy as sa
from   sqlalchemy.ext.declarative import declarative_base
from   sqlalchemy_searchable      import make_searchable, search
from   sqlalchemy_utils.types     import TSVectorType
from   sqlalchemy.orm             import sessionmaker

Base = declarative_base()

make_searchable()

class Article(Base):
    __tablename__ = 'article'
    id            = sa.Column(sa.Integer, primary_key=True)
    name          = sa.Column(sa.Unicode(255))
    content       = sa.Column(sa.UnicodeText)
    search_vector = sa.Column(TSVectorType('name', 'content'))



engine = sa.create_engine('<mypostgresconnectionstring>')

sa.orm.configure_mappers()

Base.metadata.create_all(engine)

Session = sessionmaker(bind=engine)
session = Session()

article1 = Article(name=u'First article', content=u'This is the first article')
article2 = Article(name=u'Second article', content=u'This is the second article')


session.add(article1)
session.add(article2)
session.commit()


query = session.query(Article)
query = search(query, 'first')

print(query.first().name)

当我尝试运行此代码时,会收到以下错误消息:

AttributeError: 'comparator_factory' object has no attribute 'match_tsquery'


AttributeError: Neither 'AnnotatedColumn' object nor 'comparator_factory' object has an attribute 'match_tsquery'


AttributeError: Neither 'InstrumentAttribute' object nor 'Comparator' object associated with Article.search_vector has an attribute 'match_tsquery'

0 个答案:

没有答案