Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQLAlchemy search on ts_vector column without to_tsvector

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)
)
like image 203
Chris Avatar asked Sep 19 '25 02:09

Chris


1 Answers

How about

column.op("@@")(func.plainto_tsquery('english', search))
like image 99
exhuma Avatar answered Sep 22 '25 08:09

exhuma