Search code examples
postgresqlfull-text-search

Escaping special characters in to_tsquery


How do you espace special characters in string passed to to_tsquery? For instance, this kind of query:

select to_tsquery('AT&T');

Produces:

NOTICE:  text-search query contains only stop words or doesn't contain lexemes, ignored

 to_tsquery 
------------

(1 row)

Edit: I also noticed that there is the same issue in to_tsvector.


Solution

  • A simple solution is to create the tsquery as follows:

    select $$'AT&T'$$::tsquery;
    

    You can make more complex queries:

    select $$'AT&T' & Phone | '|Bang!'$$::tsquery;
    

    See the text search docs for more.