Simplest PSQL Web-Like Text Search

August 03, 2024

The simplest way yet to do a web-like search in PSQL:

SELECT to_tsvector('a good option') @@
  -- The query «"goo opt"» is converted to: «'goo' <-> 'opt' »
  -- and finally becomes: «'goo':*<-> 'opt':*»
  replace(
    websearch_to_tsquery('simple', '"goo opt"')::text || ' ',
    ''' ',
    ''':*'
  )::tsquery

Be aware that a column index will be needed for speed when there's a large amount of data.