Home Explore Blog CI



postgresql

2nd chunk of `doc/src/sgml/pgtrgm.sgml`
fd38d6a1bca5d0518f4d767ae0731cdcbd9000322dfbb8a40000000100000fa6
 <para>
        Returns a number that indicates how similar the two arguments are.
        The range of the result is zero (indicating that the two strings are
        completely dissimilar) to one (indicating that the two strings are
        identical).
       </para></entry>
      </row>

      <row>
       <entry role="func_table_entry"><para role="func_signature">
        <indexterm><primary>show_trgm</primary></indexterm>
        <function>show_trgm</function> ( <type>text</type> )
        <returnvalue>text[]</returnvalue>
       </para>
       <para>
        Returns an array of all the trigrams in the given string.
        (In practice this is seldom useful except for debugging.)
       </para></entry>
      </row>

      <row>
       <entry role="func_table_entry"><para role="func_signature">
        <indexterm><primary>word_similarity</primary></indexterm>
        <function>word_similarity</function> ( <type>text</type>, <type>text</type> )
        <returnvalue>real</returnvalue>
       </para>
       <para>
        Returns a number that indicates the greatest similarity between
        the set of trigrams in the first string and any continuous extent
        of an ordered set of trigrams in the second string.  For details, see
        the explanation below.
       </para></entry>
      </row>

      <row>
       <entry role="func_table_entry"><para role="func_signature">
        <indexterm><primary>strict_word_similarity</primary></indexterm>
        <function>strict_word_similarity</function> ( <type>text</type>, <type>text</type> )
        <returnvalue>real</returnvalue>
       </para>
       <para>
        Same as <function>word_similarity</function>, but forces
        extent boundaries to match word boundaries.  Since we don't have
        cross-word trigrams, this function actually returns greatest similarity
        between first string and any continuous extent of words of the second
        string.
       </para></entry>
      </row>

      <row>
       <entry role="func_table_entry"><para role="func_signature">
        <indexterm><primary>show_limit</primary></indexterm>
        <function>show_limit</function> ()
        <returnvalue>real</returnvalue>
       </para>
       <para>
        Returns the current similarity threshold used by the <literal>%</literal>
        operator.  This sets the minimum similarity between
        two words for them to be considered similar enough to
        be misspellings of each other, for example.
        (<emphasis>Deprecated</emphasis>; instead use <command>SHOW</command>
        <varname>pg_trgm.similarity_threshold</varname>.)
       </para></entry>
      </row>

      <row>
       <entry role="func_table_entry"><para role="func_signature">
        <indexterm><primary>set_limit</primary></indexterm>
        <function>set_limit</function> ( <type>real</type> )
        <returnvalue>real</returnvalue>
       </para>
       <para>
        Sets the current similarity threshold that is used by the <literal>%</literal>
        operator.  The threshold must be between 0 and 1 (default is 0.3).
        Returns the same value passed in.
        (<emphasis>Deprecated</emphasis>; instead use <command>SET</command>
        <varname>pg_trgm.similarity_threshold</varname>.)
       </para></entry>
      </row>
     </tbody>
    </tgroup>
  </table>

  <para>
   Consider the following example:

<programlisting>
# SELECT word_similarity('word', 'two words');
 word_similarity
-----------------
             0.8
(1 row)
</programlisting>

   In the first string, the set of trigrams is
   <literal>{"  w"," wo","wor","ord","rd "}</literal>.
   In the second string, the ordered set of trigrams is
   <literal>{"  t"," tw","two","wo ","  w"," wo","wor","ord","rds","ds "}</literal>.
   The most similar extent of an ordered set of trigrams in the second string
   is <literal>{"  w"," wo","wor","ord"}</literal>, and the similarity is
   <literal>0.8</literal>.
  </para>

  <para>
   This function returns

Title: pg_trgm Functions: Similarity and Trigram Extraction
Summary
This section describes the functions provided by the pg_trgm module, including functions to calculate the similarity between strings (similarity, word_similarity, strict_word_similarity), extract trigrams from a string (show_trgm), and set/show the similarity threshold used by the % operator (set_limit, show_limit). The word_similarity function calculates the greatest similarity between the trigrams in the first string and any continuous extent of trigrams in the second string, while strict_word_similarity forces the extent boundaries to match word boundaries.