Home Explore Blog CI



postgresql

12th chunk of `doc/src/sgml/textsearch.sgml`
24314b82dae3228689dc15e7f7a9ddef6e34da910748974a0000000100000fa9
 <replaceable>querytext</replaceable>, which must consist of single tokens
    separated by the <type>tsquery</type> operators <literal>&amp;</literal> (AND),
    <literal>|</literal> (OR), <literal>!</literal> (NOT), and
    <literal>&lt;-&gt;</literal> (FOLLOWED BY), possibly grouped
    using parentheses.  In other words, the input to
    <function>to_tsquery</function> must already follow the general rules for
    <type>tsquery</type> input, as described in <xref
    linkend="datatype-tsquery"/>.  The difference is that while basic
    <type>tsquery</type> input takes the tokens at face value,
    <function>to_tsquery</function> normalizes each token into a lexeme using
    the specified or default configuration, and discards any tokens that are
    stop words according to the configuration.  For example:

<screen>
SELECT to_tsquery('english', 'The &amp; Fat &amp; Rats');
  to_tsquery
---------------
 'fat' &amp; 'rat'
</screen>

    As in basic <type>tsquery</type> input, weight(s) can be attached to each
    lexeme to restrict it to match only <type>tsvector</type> lexemes of those
    weight(s).  For example:

<screen>
SELECT to_tsquery('english', 'Fat | Rats:AB');
    to_tsquery
------------------
 'fat' | 'rat':AB
</screen>

    Also, <literal>*</literal> can be attached to a lexeme to specify prefix matching:

<screen>
SELECT to_tsquery('supern:*A &amp; star:A*B');
        to_tsquery
--------------------------
 'supern':*A &amp; 'star':*AB
</screen>

    Such a lexeme will match any word in a <type>tsvector</type> that begins
    with the given string.
   </para>

   <para>
    <function>to_tsquery</function> can also accept single-quoted
    phrases.  This is primarily useful when the configuration includes a
    thesaurus dictionary that may trigger on such phrases.
    In the example below, a thesaurus contains the rule <literal>supernovae
    stars : sn</literal>:

<screen>
SELECT to_tsquery('''supernovae stars'' &amp; !crab');
  to_tsquery
---------------
 'sn' &amp; !'crab'
</screen>

    Without quotes, <function>to_tsquery</function> will generate a syntax
    error for tokens that are not separated by an AND, OR, or FOLLOWED BY
    operator.
   </para>

   <indexterm>
    <primary>plainto_tsquery</primary>
   </indexterm>

<synopsis>
plainto_tsquery(<optional> <replaceable class="parameter">config</replaceable> <type>regconfig</type>, </optional> <replaceable class="parameter">querytext</replaceable> <type>text</type>) returns <type>tsquery</type>
</synopsis>

   <para>
    <function>plainto_tsquery</function> transforms the unformatted text
    <replaceable>querytext</replaceable> to a <type>tsquery</type> value.
    The text is parsed and normalized much as for <function>to_tsvector</function>,
    then the <literal>&amp;</literal> (AND) <type>tsquery</type> operator is
    inserted between surviving words.
   </para>

   <para>
    Example:

<screen>
SELECT plainto_tsquery('english', 'The Fat Rats');
 plainto_tsquery
-----------------
 'fat' &amp; 'rat'
</screen>

    Note that <function>plainto_tsquery</function> will not
    recognize <type>tsquery</type> operators, weight labels,
    or prefix-match labels in its input:

<screen>
SELECT plainto_tsquery('english', 'The Fat &amp; Rats:C');
   plainto_tsquery
---------------------
 'fat' &amp; 'rat' &amp; 'c'
</screen>

    Here, all the input punctuation was discarded.
   </para>

   <indexterm>
    <primary>phraseto_tsquery</primary>
   </indexterm>

<synopsis>
phraseto_tsquery(<optional> <replaceable class="parameter">config</replaceable> <type>regconfig</type>, </optional> <replaceable class="parameter">querytext</replaceable> <type>text</type>) returns <type>tsquery</type>
</synopsis>

   <para>
    <function>phraseto_tsquery</function> behaves much like
    <function>plainto_tsquery</function>, except that it inserts
    the <literal>&lt;-&gt;</literal> (FOLLOWED BY) operator between
    surviving words instead of the <literal>&amp;</literal> (AND) operator.

Title: More on tsquery Parsing with to_tsquery, plainto_tsquery and phraseto_tsquery
Summary
This section dives deeper into `to_tsquery`, `plainto_tsquery`, and `phraseto_tsquery`. `to_tsquery` normalizes tokens and supports weights (e.g., `:AB`) and prefix matching (using `*`). It also handles single-quoted phrases, useful for thesaurus-based configurations. `plainto_tsquery` transforms unformatted text into a `tsquery` by parsing and normalizing, then inserting AND operators between words. `phraseto_tsquery` functions similarly to `plainto_tsquery`, but inserts FOLLOWED BY operators instead of AND operators.