<replaceable>querytext</replaceable>, which must consist of single tokens
separated by the <type>tsquery</type> operators <literal>&</literal> (AND),
<literal>|</literal> (OR), <literal>!</literal> (NOT), and
<literal><-></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 & Fat & Rats');
to_tsquery
---------------
'fat' & '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 & star:A*B');
to_tsquery
--------------------------
'supern':*A & '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'' & !crab');
to_tsquery
---------------
'sn' & !'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>&</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' & '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 & Rats:C');
plainto_tsquery
---------------------
'fat' & 'rat' & '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><-></literal> (FOLLOWED BY) operator between
surviving words instead of the <literal>&</literal> (AND) operator.