Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Escaping special characters in to_tsquery

Tags:

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.

like image 844
Konrad Garus Avatar asked Dec 31 '12 16:12

Konrad Garus


People also ask

What does to_ tsvector do?

The to_tsvector function internally calls a parser which breaks the document text into tokens and assigns a type to each token. For each token, a list of dictionaries (Section 12.6) is consulted, where the list can vary depending on the token type.

What postgresql operator checks to see if a Tsquery matches a Tsvector?

@@ operator checks if tsquery matches tsvector. For instance, if the word to be queried is “fox” for the above-mentioned example, then: SELECT to_tsvector('The quick brown fox jumped over the lazy dog') @@ to_tsquery('fox');


2 Answers

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.

like image 103
David Weber Avatar answered Sep 18 '22 12:09

David Weber


I found this comment very useful that uses the plainto_tsquery('AT&T) function https://stackoverflow.com/a/16020565/350195

like image 38
peralmq Avatar answered Sep 19 '22 12:09

peralmq