Escaping special characters in to_tsquery

后端 未结 3 727
栀梦
栀梦 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

    0 讨论(0)
  • 2021-02-12 18:22

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

    0 讨论(0)
  • 2021-02-12 18:44

    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.

    0 讨论(0)
提交回复
热议问题