tsvector in sqlalchemy

こ雲淡風輕ζ 提交于 2019-12-10 16:20:54

问题


I've been looking for a way to use tsvector in sqlalchemy (simply like other ones such as INTEGER, etc), but so far it's not clear to me how to do this. I've read that tsvector can be implemented as a type using UserDefinedType. After some attempts I'm getting nowhere, someone has a simple way to do this? thanks


回答1:


If you want SQLAlchemy to be able to create schemas with the tsvector type and just retrieve the serialized value in queries, this is what you need:

from sqlalchemy import types
class tsvector(types.TypeDecorator):
    impl = types.UnicodeText

@compiles(tsvector, 'postgresql')
def compile_tsvector(element, compiler, **kw):
    return 'tsvector'

tsvector works like a regular type and you can use it within a table definition. (I forgot where I found the snippet, probably on the SQLAlchemy mailing list or wiki.)

If you need to actually parse tsvector data, it's a little more complicated. The latest version's hstore support might be a good example to follow. However you may find the following snippet useful too. It's old code that's been known to work and is written with pyparsing:

from pyparsing import ParserElement, QuotedString, Regex, Group, Suppress, Group, delimitedList, ZeroOrMore, StringEnd
ParserElement.enablePackrat()
lexeme = QuotedString("'")
occurrence_marker = Regex('[1-9][0-9]*[A-D]?')
atom = Group(lexeme('lexeme') + Suppress(':') + Group(delimitedList(occurrence_marker))('markers'))('atom')
tsvector = ZeroOrMore(atom) + StringEnd()
parse_tsvector = tsvector.parseString

Update:

To query the tsvector column, use the .op() method like this:

session.query(Model).filter(Model.tsvector.op('@@')(func.plainto_tsquery('search string')))


来源:https://stackoverflow.com/questions/13837111/tsvector-in-sqlalchemy

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!