Postgresql – Match all in a full text search

full-text-searchpattern matchingpostgresql

Using PostgreSQL 9.4, is there anyway that we can specify the tsquery to match all?

I have tried:

SELECT 'a fat cat sat on a mat and ate a fat rat'::tsvector @@ '*'::tsquery;

But it doesn't seem to work. It returns false.

Best Answer

Wrap you expression in a CASE statement:

SELECT * FROM tbl
WHERE  CASE $1 WHEN '*' THEN TRUE
       ELSE some_tsvector_column @@ $1::tsquery END;

This way you can pass '*' to disable the filter or any other valid tsquery string to actually filter.