The tsquery data type is used to search for certain lexeme. Lexeme can be combined with the & (AND), | (OR), and ! (NOT) operators. Note that the NOT operator has the highest precedence, followed by AND and then OR. Also, parentheses can be used to group lexemes and operators to force a certain order. The following example shows how we can search for certain lexemes using tsquery, tsvector, and the match operator @@:
car_portal=# SELECT 'A wise man always has something to say, whereas a fool always needs to say something'::tsvector @@ 'wise'::tsquery;
?column?
----------
t
(1 row)
The tsquery also has the to_tsquery function to convert text to lexemes, as shown here:
car_portal=# SELECT to_tsquery('english', 'wise & man');
to_tsquery
----------------
'wise' & 'man'
(1 row)
Searching for phrases in tsquery is possible by using the followed by tsquery operator <->, this means the result is matched if the two words are adjacent and in the given order:
car_portal=# SELECT to_tsvector('A wise man always has something to say, whereas a fool always needs to say something') @@ to_tsquery('wise <-> man');
?column?
----------
t
(1 row)