Escaping special characters in to_tsquery

无人久伴 提交于 2020-02-20 06:37:23

问题


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.


回答1:


If you want 'AT&T' to be treated as a search word, you're going to need some customised components, because the default parser splits it as two words:

steve@steve@[local] =# select * from ts_parse('default', 'AT&T');
 tokid | token 
-------+-------
     1 | AT
    12 | &
     1 | T
(3 rows)
steve@steve@[local] =# select * from ts_debug('simple', 'AT&T');
   alias   |   description   | token | dictionaries | dictionary | lexemes 
-----------+-----------------+-------+--------------+------------+---------
 asciiword | Word, all ASCII | AT    | {simple}     | simple     | {at}
 blank     | Space symbols   | &     | {}           |            | 
 asciiword | Word, all ASCII | T     | {simple}     | simple     | {t}
(3 rows)

As you can see from the documentation for CREATE TEXT PARSER this is not very trivial, as the parser appears to need to be a C function.

You might find this post of someone getting "underscore_word" to be recognised as a single token useful: http://postgresql.1045698.n5.nabble.com/Configuring-Text-Search-parser-td2846645.html




回答2:


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.




回答3:


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



来源:https://stackoverflow.com/questions/14103880/escaping-special-characters-in-to-tsquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!