Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

tsvector in sqlalchemy

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

like image 742
Gery Avatar asked Dec 12 '12 09:12

Gery


People also ask

What is Tsvector?

tsvector. A tsvector value is a sorted list of distinct lexemes, which are words that have been normalized to merge different variants of the same word (see Chapter 12 for details).

What is declarative base in SQLAlchemy?

A base class stores a catlog of classes and mapped tables in the Declarative system. This is called as the declarative base class. There will be usually just one instance of this base in a commonly imported module. The declarative_base() function is used to create base class. This function is defined in sqlalchemy.

Does SQLAlchemy support Postgres?

PostgreSQL supports sequences, and SQLAlchemy uses these as the default means of creating new primary key values for integer-based primary key columns.

What is the difference between psycopg2 and SQLAlchemy?

The psycopg2 is over 2x faster than SQLAlchemy on small table. This behavior is expected as psycopg2 is a database driver for postgresql while SQLAlchemy is general ORM library.


1 Answers

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')))
like image 156
jd. Avatar answered Oct 07 '22 05:10

jd.