Search code examples
postgresqltsvector

Easiest way to build a PostgreSQL tsquery from an array of word prefixes


Is there an easy way to generate a tsquery from a list of word prefixes?

For example, an array of {'mat', 'gra'} would generate a tsquery of 'mat:*&gra:*'


Solution

  • That could be done like this:

    SELECT string_agg(s || ':*', '&')::tsquery
    FROM unnest('{mat,gra}'::text[]) AS s;
    
        string_agg     
    -------------------
     'mat':* & 'gra':*
    (1 row)