Home Explore Blog CI



postgresql

21th chunk of `doc/src/sgml/textsearch.sgml`
e226be15c00c212eb91468e33eba7069978e2c03e293bc650000000100000fb3
 parts of a document to be weighted differently by ranking functions.
      </para>

      <para>
       Note that weight labels apply to <emphasis>positions</emphasis>, not
       <emphasis>lexemes</emphasis>.  If the input vector has been stripped of
       positions then <function>setweight</function> does nothing.
      </para>
     </listitem>
    </varlistentry>

    <varlistentry>
     <term>
     <indexterm>
      <primary>length(tsvector)</primary>
     </indexterm>

      <literal>length(<replaceable class="parameter">vector</replaceable> <type>tsvector</type>) returns <type>integer</type></literal>
     </term>

     <listitem>
      <para>
       Returns the number of lexemes stored in the vector.
      </para>
     </listitem>
    </varlistentry>

    <varlistentry>

     <term>
     <indexterm>
      <primary>strip</primary>
     </indexterm>

      <literal>strip(<replaceable class="parameter">vector</replaceable> <type>tsvector</type>) returns <type>tsvector</type></literal>
     </term>

     <listitem>
      <para>
       Returns a vector that lists the same lexemes as the given vector, but
       lacks any position or weight information.  The result is usually much
       smaller than an unstripped vector, but it is also less useful.
       Relevance ranking does not work as well on stripped vectors as
       unstripped ones.  Also,
       the <literal>&lt;-&gt;</literal> (FOLLOWED BY) <type>tsquery</type> operator
       will never match stripped input, since it cannot determine the
       distance between lexeme occurrences.
      </para>
     </listitem>

    </varlistentry>

   </variablelist>

   <para>
    A full list of <type>tsvector</type>-related functions is available
    in <xref linkend="textsearch-functions-table"/>.
   </para>

  </sect2>

  <sect2 id="textsearch-manipulate-tsquery">
   <title>Manipulating Queries</title>

   <para>
    <xref linkend="textsearch-parsing-queries"/> showed how raw textual
    queries can be converted into <type>tsquery</type> values.
    <productname>PostgreSQL</productname> also provides functions and
    operators that can be used to manipulate queries that are already
    in <type>tsquery</type> form.
   </para>

   <variablelist>

    <varlistentry>

     <term>
      <literal><type>tsquery</type> &amp;&amp; <type>tsquery</type></literal>
     </term>

     <listitem>
      <para>
       Returns the AND-combination of the two given queries.
      </para>
     </listitem>

    </varlistentry>

    <varlistentry>

     <term>
      <literal><type>tsquery</type> || <type>tsquery</type></literal>
     </term>

     <listitem>
      <para>
       Returns the OR-combination of the two given queries.
      </para>
     </listitem>

    </varlistentry>

    <varlistentry>

     <term>
      <literal>!! <type>tsquery</type></literal>
     </term>

     <listitem>
      <para>
       Returns the negation (NOT) of the given query.
      </para>
     </listitem>

    </varlistentry>

    <varlistentry>

     <term>
      <literal><type>tsquery</type> &lt;-&gt; <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>&lt;-&gt;</literal> (FOLLOWED BY)
       <type>tsquery</type> operator.  For example:

<screen>
SELECT to_tsquery('fat') &lt;-&gt; to_tsquery('cat | rat');
          ?column?
----------------------------
 'fat' &lt;-&gt; ( '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>

Title: tsvector and tsquery Manipulation: Stripping Vectors and Combining Queries
Summary
This section covers manipulating tsvector documents and tsquery queries in PostgreSQL. The strip function removes position and weight information from a tsvector, resulting in a smaller but less useful vector. Relevance ranking and the FOLLOWED BY operator are less effective on stripped vectors. Additionally, it details how to combine tsquery queries using AND (&&), OR (||), and NOT (!!) operators. It also explains the FOLLOWED BY (<->) operator and the tsquery_phrase function, which searches for a match to one query immediately followed by another.