Postgresql – to_tsquery – Does it always accept quert text

full-text-searchpostgresql

I am very new to FTS features in Postgres, in fact, have started exploring them since yesterday only.

I wanted to pass list of values stored in columns of a DB tables instead of query text in to_tsquery.

Something like this:

select  * 
from t1
 where to_tsvector("col1") @@ to_tsquery((select col2 from t2 where id = 123))

Not sure if this is a possibility? Is there any alternative by which such kind of implementation can be achieved using to_tsquery?

Best Answer

If the values can be arbitrary strings, you can get errors:

SELECT to_tsquery('a&');
ERROR:  no operand in tsquery: "a&"
SELECT to_tsquery('''q');
ERROR:  syntax error in tsquery: "'q"

Use plainto_tsquery, which is designed to accept plain text strings.