4

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.

5
  • 1
    Why would you do that? Commented Apr 10, 2015 at 9:28
  • @dezso the reason I do so I can input '*' as parameter instead of writing different query based on different parameter. Commented Apr 14, 2015 at 5:08
  • That sounds like a very nice performance sink :) On the other hand, I could not figure out a good solution for you. Commented Apr 14, 2015 at 8:48
  • @dezso yeah, I feel the same way. I think it's better to add additional full text search query instead. Commented Apr 15, 2015 at 17:42
  • word | ! word
    – Andrew
    Commented Nov 13, 2023 at 14:44

1 Answer 1

4

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.

1
  • yeah, this is a simple and understandable solution. Thanks :) Commented Nov 30, 2015 at 5:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.