SQLAlchemy search on ts_vector column without to_tsvector

Viewed 384

I have a computed column that is a tsvector.

The api sends a search query, but, of course, these are not valid tsvectors. Postgres's plainto_tsquery converts text input to a correctly formatted tsvector for matching.

This breaks with SQLAlchemy.

column.match(func.plainto_tsquery('english', search)) does not work because SQLAlchemy converts that to:

column @@ to_tsquery(plainto_tsquery('english', 'the search query'))

what I actually want is the correct operator (@@) but without the magic conversion

column @@ plainto_tsquery('english', 'the search query')

A dumb way that works but is not what I want is:

column.match(
  cast(func.plainto_tsquery('english', search), String)
)
1 Answers

How about

column.op("@@")(func.plainto_tsquery('english', search))
Related