<term>
<literal><type>tsquery</type> <-> <type>tsquery</type></literal>
</term>
<listitem>
<para>
Returns a query that searches for a match to the first given query
immediately followed by a match to the second given query, using
the <literal><-></literal> (FOLLOWED BY)
<type>tsquery</type> operator. For example:
<screen>
SELECT to_tsquery('fat') <-> to_tsquery('cat | rat');
?column?
----------------------------
'fat' <-> ( 'cat' | 'rat' )
</screen>
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>
<indexterm>
<primary>tsquery_phrase</primary>
</indexterm>
<literal>tsquery_phrase(<replaceable class="parameter">query1</replaceable> <type>tsquery</type>, <replaceable class="parameter">query2</replaceable> <type>tsquery</type> [, <replaceable class="parameter">distance</replaceable> <type>integer</type> ]) returns <type>tsquery</type></literal>
</term>
<listitem>
<para>
Returns a query that searches for a match to the first given query
followed by a match to the second given query at a distance of exactly
<replaceable>distance</replaceable> lexemes, using
the <literal><<replaceable>N</replaceable>></literal>
<type>tsquery</type> operator. For example:
<screen>
SELECT tsquery_phrase(to_tsquery('fat'), to_tsquery('cat'), 10);
tsquery_phrase
------------------
'fat' <10> 'cat'
</screen>
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>
<indexterm>
<primary>numnode</primary>
</indexterm>
<literal>numnode(<replaceable class="parameter">query</replaceable> <type>tsquery</type>) returns <type>integer</type></literal>
</term>
<listitem>
<para>
Returns the number of nodes (lexemes plus operators) in a
<type>tsquery</type>. This function is useful
to determine if the <replaceable>query</replaceable> is meaningful
(returns > 0), or contains only stop words (returns 0).
Examples:
<screen>
SELECT numnode(plainto_tsquery('the any'));
NOTICE: query contains only stopword(s) or doesn't contain lexeme(s), ignored
numnode
---------
0
SELECT numnode('foo & bar'::tsquery);
numnode
---------
3
</screen>
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>
<indexterm>
<primary>querytree</primary>
</indexterm>
<literal>querytree(<replaceable class="parameter">query</replaceable> <type>tsquery</type>) returns <type>text</type></literal>
</term>
<listitem>
<para>
Returns the portion of a <type>tsquery</type> that can be used for
searching an index. This function is useful for detecting
unindexable queries, for example those containing only stop words
or only negated terms. For example:
<screen>
SELECT querytree(to_tsquery('defined'));
querytree
-----------
'defin'
SELECT querytree(to_tsquery('!defined'));
querytree
-----------
T
</screen>
</para>
</listitem>
</varlistentry>
</variablelist>
<sect3 id="textsearch-query-rewriting">
<title>Query Rewriting</title>
<indexterm zone="textsearch-query-rewriting">
<primary>ts_rewrite</primary>
</indexterm>
<para>
The <function>ts_rewrite</function> family of functions search a
given <type>tsquery</type> for occurrences of a target
subquery, and replace each occurrence with a
substitute subquery. In essence this operation is a
<type>tsquery</type>-specific version of substring replacement.
A target and substitute combination can be
thought of as a <firstterm>query rewrite rule</firstterm>. A collection
of such rewrite rules can be a powerful search aid.
For example, you can expand the search using synonyms
(e.g., <literal>new york</literal>, <literal>big apple</literal>,