using tsvector

41 views
Skip to first unread message

Gery Herbozo

unread,
Dec 14, 2012, 6:52:44 AM12/14/12
to geoal...@googlegroups.com
Hello,

I have a question about tsvector used as a type in GeoAlchemy. I've
been looking for a way to use tsvector (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? I'd appreciate if someone could share some examples with me
about using this.

Thanks in advance, best regards,

Eric Lemoine

unread,
Dec 14, 2012, 8:48:51 AM12/14/12
to geoal...@googlegroups.com
It isn't a GeoAlchemy question.

This is what we have been using here:

class TsVector(types.UserDefinedType):
""" A custom type for PostgreSQL's tsvector type. """
def get_col_spec(self):
return 'TSVECTOR'


class FullTextSearch(GeoInterface, Base):
__tablename__ = 'tsearch'
__table_args__ = (
Index('tsearch_ts_idx', 'ts', postgresql_using='gin'),
{'schema': _schema}
)
id = Column('id', types.Integer, primary_key=True)
ts = Column('ts', TsVector)
the_geom = GeometryColumn(Geometry(srid=_srid))



--
Eric

Gery Herbozo

unread,
Dec 17, 2012, 11:21:23 AM12/17/12
to geoal...@googlegroups.com
thanks a lot Eric, it works nicely.

> class TsVector(types.UserDefinedType):
> """ A custom type for PostgreSQL's tsvector type. """
> def get_col_spec(self):
> return 'TSVECTOR'
>
>
> class FullTextSearch(GeoInterface, Base):
> __tablename__ = 'tsearch'
> __table_args__ = (
> Index('tsearch_ts_idx', 'ts', postgresql_using='gin'),
> {'schema': _schema}
> )
> id = Column('id', types.Integer, primary_key=True)
> ts = Column('ts', TsVector)
> the_geom = GeometryColumn(Geometry(srid=_srid))

based on this example, how could I do a simple query using this ts? in
postgresql it should be like this: select * from tsearch where ts @@
to_tsquery('something');

I think this could work but it says ts is not defined:
ts.op('@@@')(func.to_tsvector("something"))

thanks,

Eric Lemoine

unread,
Dec 17, 2012, 11:47:33 AM12/17/12
to geoal...@googlegroups.com
We use text here:

_filter = "%(tsvector)s @@ to_tsquery('%(lang)s', '%(terms)s')" % \
{'tsvector': 'ts', 'lang': lang, 'terms': terms}
query = DBSession.query(FullTextSearch).filter(_filter)


There may be better ways.





--
Eric

Gery Herbozo

unread,
Dec 18, 2012, 6:14:19 AM12/18/12
to geoal...@googlegroups.com
thanks a world Eric, this works also:
session.query(Model).filter(Model.tsvector.op('@@')(func.plainto_tsquery('search
string')))
Reply all
Reply to author
Forward
0 new messages