Question

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

Was it helpful?

Solution

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')))
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top