3

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.

4
  • 1
    Why would you do that? – dezso Apr 10 '15 at 9:28
  • @dezso the reason I do so I can input '*' as parameter instead of writing different query based on different parameter. – Dzung Nguyen Apr 14 '15 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. – dezso Apr 14 '15 at 8:48
  • @dezso yeah, I feel the same way. I think it's better to add additional full text search query instead. – Dzung Nguyen Apr 15 '15 at 17:42
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 :) – Dzung Nguyen Nov 30 '15 at 5:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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