skip to Main Content

Here is an example plainto_tsquery call in postgres 15.1:

# select plainto_tsquery('english', 'postgres is the best');
  plainto_tsquery  
-------------------
 'postgr' & 'best'
(1 row)

This is the equivalent of

select to_tsquery('english', 'postgr & best');
    to_tsquery     
-------------------
 'postgr' & 'best'

The docs say

plainto_tsquery transforms the unformatted text querytext to a tsquery value. The text is parsed and normalized much as for to_tsvector, then the & (AND) tsquery operator is inserted between surviving words.

How do I get the same result (parsed and normalized) from a text phrase, but with '|' between the tokens, e.g., the equivalent of

# select to_tsquery('english', 'postgr | best');
    to_tsquery     
-------------------
 'postgr' | 'best'

but being able to pass in "postgres is the best" as input?

2

Answers


  1. I don’t know a direct way, but that many conversions isn’t goo for the performance

    Alsopossible is it to make a string and thenmake a tsquery from it instead of a String

    SELECT string_agg(quote_literal(lexeme)::text, ' | ') 
      FROM  unnest((select   to_tsvector ('english', 'postgres is the best')));
    
    string_agg
    ‘best’ | ‘postgr’
    SELECT string_agg(lexeme::text, ' | ')::tsquery 
      FROM  unnest((select   to_tsvector ('english', 'postgres is the best')));
    
    string_agg
    ‘best’ | ‘postgr’

    fiddle

    Login or Signup to reply.
  2. Use websearch_to_tsquery():

    select websearch_to_tsquery('english', 'postgres or best');
    
     websearch_to_tsquery 
    ══════════════════════
     'postgr' | 'best'
    (1 row)
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search