Sophie

Sophie

distrib > Mandriva > 2008.1 > x86_64 > media > main-testing > by-pkgid > bab02a23fa9f3df8d66a9a3231b50245 > files > 399

postgresql8.3-docs-8.3.6-2mdv2008.1.x86_64.rpm

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<HTML
><HEAD
><TITLE
>pg_trgm</TITLE
><META
NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.79"><LINK
REV="MADE"
HREF="mailto:pgsql-docs@postgresql.org"><LINK
REL="HOME"
TITLE="PostgreSQL 8.3.6 Documentation"
HREF="index.html"><LINK
REL="UP"
TITLE="Additional Supplied Modules"
HREF="contrib.html"><LINK
REL="PREVIOUS"
TITLE="pgstattuple"
HREF="pgstattuple.html"><LINK
REL="NEXT"
TITLE="seg"
HREF="seg.html"><LINK
REL="STYLESHEET"
TYPE="text/css"
HREF="stylesheet.css"><META
HTTP-EQUIV="Content-Type"
CONTENT="text/html; charset=ISO-8859-1"><META
NAME="creation"
CONTENT="2009-02-03T04:34:16"></HEAD
><BODY
CLASS="SECT1"
><DIV
CLASS="NAVHEADER"
><TABLE
SUMMARY="Header navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TH
COLSPAN="5"
ALIGN="center"
VALIGN="bottom"
>PostgreSQL 8.3.6 Documentation</TH
></TR
><TR
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="top"
><A
HREF="pgstattuple.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="top"
><A
HREF="contrib.html"
>Fast Backward</A
></TD
><TD
WIDTH="60%"
ALIGN="center"
VALIGN="bottom"
>Appendix F. Additional Supplied Modules</TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="top"
><A
HREF="contrib.html"
>Fast Forward</A
></TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="top"
><A
HREF="seg.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><DIV
CLASS="SECT1"
><H1
CLASS="SECT1"
><A
NAME="PGTRGM"
>F.25. pg_trgm</A
></H1
><A
NAME="AEN103707"
></A
><P
>  The <TT
CLASS="FILENAME"
>pg_trgm</TT
> module provides functions and operators
  for determining the similarity of text based on trigram matching, as
  well as index operator classes that support fast searching for similar
  strings.
 </P
><DIV
CLASS="SECT2"
><H2
CLASS="SECT2"
><A
NAME="AEN103711"
>F.25.1. Trigram (or Trigraph) Concepts</A
></H2
><P
>   A trigram is a group of three consecutive characters taken
   from a string.  We can measure the similarity of two strings by
   counting the number of trigrams they share.  This simple idea
   turns out to be very effective for measuring the similarity of
   words in many natural languages.
  </P
><DIV
CLASS="NOTE"
><BLOCKQUOTE
CLASS="NOTE"
><P
><B
>Note: </B
>    A string is considered to have two spaces
    prefixed and one space suffixed when determining the set
    of trigrams contained in the string.
    For example, the set of trigrams in the string
    <SPAN
CLASS="QUOTE"
>"<TT
CLASS="LITERAL"
>cat</TT
>"</SPAN
> is
    <SPAN
CLASS="QUOTE"
>"<TT
CLASS="LITERAL"
>  c</TT
>"</SPAN
>,
    <SPAN
CLASS="QUOTE"
>"<TT
CLASS="LITERAL"
> ca</TT
>"</SPAN
>,
    <SPAN
CLASS="QUOTE"
>"<TT
CLASS="LITERAL"
>cat</TT
>"</SPAN
>, and
    <SPAN
CLASS="QUOTE"
>"<TT
CLASS="LITERAL"
>at </TT
>"</SPAN
>.
   </P
></BLOCKQUOTE
></DIV
></DIV
><DIV
CLASS="SECT2"
><H2
CLASS="SECT2"
><A
NAME="AEN103726"
>F.25.2. Functions and Operators</A
></H2
><DIV
CLASS="TABLE"
><A
NAME="PGTRGM-FUNC-TABLE"
></A
><P
><B
>Table F-29. <TT
CLASS="FILENAME"
>pg_trgm</TT
> functions</B
></P
><TABLE
BORDER="1"
CLASS="CALSTABLE"
><COL><COL><COL><THEAD
><TR
><TH
>Function</TH
><TH
>Returns</TH
><TH
>Description</TH
></TR
></THEAD
><TBODY
><TR
><TD
><CODE
CLASS="FUNCTION"
>similarity(text, text)</CODE
></TD
><TD
><TT
CLASS="TYPE"
>real</TT
></TD
><TD
>       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).
      </TD
></TR
><TR
><TD
><CODE
CLASS="FUNCTION"
>show_trgm(text)</CODE
></TD
><TD
><TT
CLASS="TYPE"
>text[]</TT
></TD
><TD
>       Returns an array of all the trigrams in the given string.
       (In practice this is seldom useful except for debugging.)
      </TD
></TR
><TR
><TD
><CODE
CLASS="FUNCTION"
>show_limit()</CODE
></TD
><TD
><TT
CLASS="TYPE"
>real</TT
></TD
><TD
>       Returns the current similarity threshold used by the <TT
CLASS="LITERAL"
>%</TT
>
       operator.  This sets the minimum similarity between
       two words for them to be considered similar enough to
       be misspellings of each other, for example.
      </TD
></TR
><TR
><TD
><CODE
CLASS="FUNCTION"
>set_limit(real)</CODE
></TD
><TD
><TT
CLASS="TYPE"
>real</TT
></TD
><TD
>       Sets the current similarity threshold that is used by the <TT
CLASS="LITERAL"
>%</TT
>
       operator.  The threshold must be between 0 and 1 (default is 0.3).
       Returns the same value passed in.
      </TD
></TR
></TBODY
></TABLE
></DIV
><DIV
CLASS="TABLE"
><A
NAME="PGTRGM-OP-TABLE"
></A
><P
><B
>Table F-30. <TT
CLASS="FILENAME"
>pg_trgm</TT
> operators</B
></P
><TABLE
BORDER="1"
CLASS="CALSTABLE"
><COL><COL><COL><THEAD
><TR
><TH
>Operator</TH
><TH
>Returns</TH
><TH
>Description</TH
></TR
></THEAD
><TBODY
><TR
><TD
><TT
CLASS="TYPE"
>text</TT
> <TT
CLASS="LITERAL"
>%</TT
> <TT
CLASS="TYPE"
>text</TT
></TD
><TD
><TT
CLASS="TYPE"
>boolean</TT
></TD
><TD
>       Returns <TT
CLASS="LITERAL"
>true</TT
> if its arguments have a similarity that is
       greater than the current similarity threshold set by
       <CODE
CLASS="FUNCTION"
>set_limit</CODE
>.
      </TD
></TR
></TBODY
></TABLE
></DIV
></DIV
><DIV
CLASS="SECT2"
><H2
CLASS="SECT2"
><A
NAME="AEN103784"
>F.25.3. Index Support</A
></H2
><P
>   The <TT
CLASS="FILENAME"
>pg_trgm</TT
> module provides GiST and GIN index
   operator classes that allow you to create an index over a text column for
   the purpose of very fast similarity searches.  These index types support
   the <TT
CLASS="LITERAL"
>%</TT
> similarity operator (and no other operators, so you may
   want a regular btree index too).
  </P
><P
>   Example:

  </P><PRE
CLASS="PROGRAMLISTING"
>CREATE TABLE test_trgm (t text);
CREATE INDEX trgm_idx ON test_trgm USING gist (t gist_trgm_ops);
  </PRE
><P>
or
  </P><PRE
CLASS="PROGRAMLISTING"
>CREATE INDEX trgm_idx ON test_trgm USING gin (t gin_trgm_ops);
  </PRE
><P>
  </P
><P
>   At this point, you will have an index on the <TT
CLASS="STRUCTFIELD"
>t</TT
> column that
   you can use for similarity searching.  A typical query is
  </P
><PRE
CLASS="PROGRAMLISTING"
>SELECT t, similarity(t, '<TT
CLASS="REPLACEABLE"
><I
>word</I
></TT
>') AS sml
  FROM test_trgm
  WHERE t % '<TT
CLASS="REPLACEABLE"
><I
>word</I
></TT
>'
  ORDER BY sml DESC, t;
  </PRE
><P
>   This will return all values in the text column that are sufficiently
   similar to <TT
CLASS="REPLACEABLE"
><I
>word</I
></TT
>, sorted from best match to worst.  The
   index will be used to make this a fast operation even over very large data
   sets.
  </P
><P
>   The choice between GiST and GIN indexing depends on the relative
   performance characteristics of GiST and GIN, which are discussed elsewhere.
   As a rule of thumb, a GIN index is faster to search than a GiST index, but
   slower to build or update; so GIN is better suited for static data and GiST
   for often-updated data.
  </P
></DIV
><DIV
CLASS="SECT2"
><H2
CLASS="SECT2"
><A
NAME="AEN103800"
>F.25.4. Text Search Integration</A
></H2
><P
>   Trigram matching is a very useful tool when used in conjunction
   with a full text index.  In particular it can help to recognize
   misspelled input words that will not be matched directly by the
   full text search mechanism.
  </P
><P
>   The first step is to generate an auxiliary table containing all
   the unique words in the documents:
  </P
><PRE
CLASS="PROGRAMLISTING"
>CREATE TABLE words AS SELECT word FROM
        ts_stat('SELECT to_tsvector(''simple'', bodytext) FROM documents');
  </PRE
><P
>   where <TT
CLASS="STRUCTNAME"
>documents</TT
> is a table that has a text field
   <TT
CLASS="STRUCTFIELD"
>bodytext</TT
> that we wish to search.  The reason for using
   the <TT
CLASS="LITERAL"
>simple</TT
> configuration with the <CODE
CLASS="FUNCTION"
>to_tsvector</CODE
>
   function, instead of using a language-specific configuration,
   is that we want a list of the original (unstemmed) words.
  </P
><P
>   Next, create a trigram index on the word column:
  </P
><PRE
CLASS="PROGRAMLISTING"
>CREATE INDEX words_idx ON words USING gin(word gin_trgm_ops);
  </PRE
><P
>   Now, a <TT
CLASS="COMMAND"
>SELECT</TT
> query similar to the previous example can
   be used to suggest spellings for misspelled words in user search terms.
   A useful extra test is to require that the selected words are also of
   similar length to the misspelled word.
  </P
><DIV
CLASS="NOTE"
><BLOCKQUOTE
CLASS="NOTE"
><P
><B
>Note: </B
>    Since the <TT
CLASS="STRUCTNAME"
>words</TT
> table has been generated as a separate,
    static table, it will need to be periodically regenerated so that
    it remains reasonably up-to-date with the document collection.
    Keeping it exactly current is usually unnecessary.
   </P
></BLOCKQUOTE
></DIV
></DIV
><DIV
CLASS="SECT2"
><H2
CLASS="SECT2"
><A
NAME="AEN103817"
>F.25.5. References</A
></H2
><P
>   GiST Development Site
   <A
HREF="http://www.sai.msu.su/~megera/postgres/gist/"
TARGET="_top"
>http://www.sai.msu.su/~megera/postgres/gist/</A
>
  </P
><P
>   Tsearch2 Development Site
   <A
HREF="http://www.sai.msu.su/~megera/postgres/gist/tsearch/V2/"
TARGET="_top"
>http://www.sai.msu.su/~megera/postgres/gist/tsearch/V2/</A
>
  </P
></DIV
><DIV
CLASS="SECT2"
><H2
CLASS="SECT2"
><A
NAME="AEN103823"
>F.25.6. Authors</A
></H2
><P
>   Oleg Bartunov <CODE
CLASS="EMAIL"
>&#60;<A
HREF="mailto:oleg@sai.msu.su"
>oleg@sai.msu.su</A
>&#62;</CODE
>, Moscow, Moscow University, Russia
  </P
><P
>   Teodor Sigaev <CODE
CLASS="EMAIL"
>&#60;<A
HREF="mailto:teodor@sigaev.ru"
>teodor@sigaev.ru</A
>&#62;</CODE
>, Moscow, Delta-Soft Ltd.,Russia
  </P
><P
>   Documentation: Christopher Kings-Lynne
  </P
><P
>   This module is sponsored by Delta-Soft Ltd., Moscow, Russia.
  </P
></DIV
></DIV
><DIV
CLASS="NAVFOOTER"
><HR
ALIGN="LEFT"
WIDTH="100%"><TABLE
SUMMARY="Footer navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
><A
HREF="pgstattuple.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="index.html"
ACCESSKEY="H"
>Home</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
><A
HREF="seg.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>pgstattuple</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="contrib.html"
ACCESSKEY="U"
>Up</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>seg</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>