Escaping special characters in to_tsquery

后端 未结 3 718
栀梦
栀梦 2021-02-12 17:52

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

select to_tsquery(\'AT&T\');
3条回答
  •  别跟我提以往
    2021-02-12 18:21

    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

提交回复
热议问题