Home Explore Blog CI



postgresql

5th chunk of `doc/src/sgml/textsearch.sgml`
75c3dcfddf46be06a24f0d426a822148b38e915b8ccb0a6c0000000100000fa1
 The form <type>text</type> <literal>@@</literal> <type>text</type>
    is equivalent to <literal>to_tsvector(x) @@ plainto_tsquery(y)</literal>.
   </para>

   <para>
    Within a <type>tsquery</type>, the <literal>&amp;</literal> (AND) operator
    specifies that both its arguments must appear in the document to have a
    match.  Similarly, the <literal>|</literal> (OR) operator specifies that
    at least one of its arguments must appear, while the <literal>!</literal> (NOT)
    operator specifies that its argument must <emphasis>not</emphasis> appear in
    order to have a match.
    For example, the query <literal>fat &amp; ! rat</literal> matches documents that
    contain <literal>fat</literal> but not <literal>rat</literal>.
   </para>

   <para>
    Searching for phrases is possible with the help of
    the <literal>&lt;-&gt;</literal> (FOLLOWED BY) <type>tsquery</type> operator, which
    matches only if its arguments have matches that are adjacent and in the
    given order.  For example:

<programlisting>
SELECT to_tsvector('fatal error') @@ to_tsquery('fatal &lt;-&gt; error');
 ?column?
----------
 t

SELECT to_tsvector('error is not fatal') @@ to_tsquery('fatal &lt;-&gt; error');
 ?column?
----------
 f
</programlisting>

    There is a more general version of the FOLLOWED BY operator having the
    form <literal>&lt;<replaceable>N</replaceable>&gt;</literal>,
    where <replaceable>N</replaceable> is an integer standing for the difference between
    the positions of the matching lexemes.  <literal>&lt;1&gt;</literal> is
    the same as <literal>&lt;-&gt;</literal>, while <literal>&lt;2&gt;</literal>
    allows exactly one other lexeme to appear between the matches, and so
    on.  The <literal>phraseto_tsquery</literal> function makes use of this
    operator to construct a <literal>tsquery</literal> that can match a multi-word
    phrase when some of the words are stop words.  For example:

<programlisting>
SELECT phraseto_tsquery('cats ate rats');
       phraseto_tsquery
-------------------------------
 'cat' &lt;-&gt; 'ate' &lt;-&gt; 'rat'

SELECT phraseto_tsquery('the cats ate the rats');
       phraseto_tsquery
-------------------------------
 'cat' &lt;-&gt; 'ate' &lt;2&gt; 'rat'
</programlisting>
   </para>

   <para>
    A special case that's sometimes useful is that <literal>&lt;0&gt;</literal>
    can be used to require that two patterns match the same word.
   </para>

   <para>
    Parentheses can be used to control nesting of the <type>tsquery</type>
    operators.  Without parentheses, <literal>|</literal> binds least tightly,
    then <literal>&amp;</literal>, then <literal>&lt;-&gt;</literal>,
    and <literal>!</literal> most tightly.
   </para>

   <para>
    It's worth noticing that the AND/OR/NOT operators mean something subtly
    different when they are within the arguments of a FOLLOWED BY operator
    than when they are not, because within FOLLOWED BY the exact position of
    the match is significant.  For example, normally <literal>!x</literal> matches
    only documents that do not contain <literal>x</literal> anywhere.
    But <literal>!x &lt;-&gt; y</literal> matches <literal>y</literal> if it is not
    immediately after an <literal>x</literal>; an occurrence of <literal>x</literal>
    elsewhere in the document does not prevent a match.  Another example is
    that <literal>x &amp; y</literal> normally only requires that <literal>x</literal>
    and <literal>y</literal> both appear somewhere in the document, but
    <literal>(x &amp; y) &lt;-&gt; z</literal> requires <literal>x</literal>
    and <literal>y</literal> to match at the same place, immediately before
    a <literal>z</literal>.  Thus this query behaves differently from
    <literal>x &lt;-&gt; z &amp; y &lt;-&gt; z</literal>, which will match a
    document containing two separate sequences <literal>x z</literal> and
    <literal>y z</literal>.  (This specific query is useless as written,
    since <literal>x</literal>

Title: Phrase Searching and Operators in tsquery
Summary
The text @@ text form is equivalent to to_tsvector(x) @@ plainto_tsquery(y). The & (AND), | (OR), and ! (NOT) operators in tsquery specify conditions for matching terms in a document. The <-> (FOLLOWED BY) operator searches for phrases with adjacent and ordered matches, with <N> specifying the difference in positions between matching lexemes. phraseto_tsquery constructs tsqueries that match multi-word phrases even with stop words. <0> requires two patterns to match the same word. Parentheses control operator nesting. The AND/OR/NOT operators within FOLLOWED BY have different meanings, considering the position of the match.