]> begriffs open source - ai-pg/blob - full-docs/src/sgml/html/datatype-textsearch.html
WIP: toc builder
[ai-pg] / full-docs / src / sgml / html / datatype-textsearch.html
1 <?xml version="1.0" encoding="UTF-8" standalone="no"?>
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>8.11. Text Search Types</title><link rel="stylesheet" type="text/css" href="stylesheet.css" /><link rev="made" href="pgsql-docs@lists.postgresql.org" /><meta name="generator" content="DocBook XSL Stylesheets Vsnapshot" /><link rel="prev" href="datatype-bit.html" title="8.10. Bit String Types" /><link rel="next" href="datatype-uuid.html" title="8.12. UUID Type" /></head><body id="docContent" class="container-fluid col-10"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="5" align="center">8.11. Text Search Types</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="datatype-bit.html" title="8.10. Bit String Types">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="datatype.html" title="Chapter 8. Data Types">Up</a></td><th width="60%" align="center">Chapter 8. Data Types</th><td width="10%" align="right"><a accesskey="h" href="index.html" title="PostgreSQL 18.0 Documentation">Home</a></td><td width="10%" align="right"> <a accesskey="n" href="datatype-uuid.html" title="8.12. UUID Type">Next</a></td></tr></table><hr /></div><div class="sect1" id="DATATYPE-TEXTSEARCH"><div class="titlepage"><div><div><h2 class="title" style="clear: both">8.11. Text Search Types <a href="#DATATYPE-TEXTSEARCH" class="id_link">#</a></h2></div></div></div><div class="toc"><dl class="toc"><dt><span class="sect2"><a href="datatype-textsearch.html#DATATYPE-TSVECTOR">8.11.1. <code class="type">tsvector</code></a></span></dt><dt><span class="sect2"><a href="datatype-textsearch.html#DATATYPE-TSQUERY">8.11.2. <code class="type">tsquery</code></a></span></dt></dl></div><a id="id-1.5.7.19.2" class="indexterm"></a><a id="id-1.5.7.19.3" class="indexterm"></a><p>
3     <span class="productname">PostgreSQL</span> provides two data types that
4     are designed to support full text search, which is the activity of
5     searching through a collection of natural-language <em class="firstterm">documents</em>
6     to locate those that best match a <em class="firstterm">query</em>.
7     The <code class="type">tsvector</code> type represents a document in a form optimized
8     for text search; the <code class="type">tsquery</code> type similarly represents
9     a text query.
10     <a class="xref" href="textsearch.html" title="Chapter 12. Full Text Search">Chapter 12</a> provides a detailed explanation of this
11     facility, and <a class="xref" href="functions-textsearch.html" title="9.13. Text Search Functions and Operators">Section 9.13</a> summarizes the
12     related functions and operators.
13    </p><div class="sect2" id="DATATYPE-TSVECTOR"><div class="titlepage"><div><div><h3 class="title">8.11.1. <code class="type">tsvector</code> <a href="#DATATYPE-TSVECTOR" class="id_link">#</a></h3></div></div></div><a id="id-1.5.7.19.5.2" class="indexterm"></a><p>
14      A <code class="type">tsvector</code> value is a sorted list of distinct
15      <em class="firstterm">lexemes</em>, which are words that have been
16      <em class="firstterm">normalized</em> to merge different variants of the same word
17      (see <a class="xref" href="textsearch.html" title="Chapter 12. Full Text Search">Chapter 12</a> for details).  Sorting and
18      duplicate-elimination are done automatically during input, as shown in
19      this example:
20
21 </p><pre class="programlisting">
22 SELECT 'a fat cat sat on a mat and ate a fat rat'::tsvector;
23                       tsvector
24 ----------------------------------------------------
25  'a' 'and' 'ate' 'cat' 'fat' 'mat' 'on' 'rat' 'sat'
26 </pre><p>
27
28      To represent
29      lexemes containing whitespace or punctuation, surround them with quotes:
30
31 </p><pre class="programlisting">
32 SELECT $$the lexeme '    ' contains spaces$$::tsvector;
33                  tsvector
34 -------------------------------------------
35  '    ' 'contains' 'lexeme' 'spaces' 'the'
36 </pre><p>
37
38      (We use dollar-quoted string literals in this example and the next one
39      to avoid the confusion of having to double quote marks within the
40      literals.)  Embedded quotes and backslashes must be doubled:
41
42 </p><pre class="programlisting">
43 SELECT $$the lexeme 'Joe''s' contains a quote$$::tsvector;
44                     tsvector
45 ------------------------------------------------
46  'Joe''s' 'a' 'contains' 'lexeme' 'quote' 'the'
47 </pre><p>
48
49      Optionally, integer <em class="firstterm">positions</em>
50      can be attached to lexemes:
51
52 </p><pre class="programlisting">
53 SELECT 'a:1 fat:2 cat:3 sat:4 on:5 a:6 mat:7 and:8 ate:9 a:10 fat:11 rat:12'::tsvector;
54                                   tsvector
55 -------------------------------------------------------------------​------------
56  'a':1,6,10 'and':8 'ate':9 'cat':3 'fat':2,11 'mat':7 'on':5 'rat':12 'sat':4
57 </pre><p>
58
59      A position normally indicates the source word's location in the
60      document.  Positional information can be used for
61      <em class="firstterm">proximity ranking</em>.  Position values can
62      range from 1 to 16383; larger numbers are silently set to 16383.
63      Duplicate positions for the same lexeme are discarded.
64     </p><p>
65      Lexemes that have positions can further be labeled with a
66      <em class="firstterm">weight</em>, which can be <code class="literal">A</code>,
67      <code class="literal">B</code>, <code class="literal">C</code>, or <code class="literal">D</code>.
68      <code class="literal">D</code> is the default and hence is not shown on output:
69
70 </p><pre class="programlisting">
71 SELECT 'a:1A fat:2B,4C cat:5D'::tsvector;
72           tsvector
73 ----------------------------
74  'a':1A 'cat':5 'fat':2B,4C
75 </pre><p>
76
77      Weights are typically used to reflect document structure, for example
78      by marking title words differently from body words.  Text search
79      ranking functions can assign different priorities to the different
80      weight markers.
81     </p><p>
82      It is important to understand that the
83      <code class="type">tsvector</code> type itself does not perform any word
84      normalization; it assumes the words it is given are normalized
85      appropriately for the application.  For example,
86
87 </p><pre class="programlisting">
88 SELECT 'The Fat Rats'::tsvector;
89       tsvector
90 --------------------
91  'Fat' 'Rats' 'The'
92 </pre><p>
93
94      For most English-text-searching applications the above words would
95      be considered non-normalized, but <code class="type">tsvector</code> doesn't care.
96      Raw document text should usually be passed through
97      <code class="function">to_tsvector</code> to normalize the words appropriately
98      for searching:
99
100 </p><pre class="programlisting">
101 SELECT to_tsvector('english', 'The Fat Rats');
102    to_tsvector
103 -----------------
104  'fat':2 'rat':3
105 </pre><p>
106
107      Again, see <a class="xref" href="textsearch.html" title="Chapter 12. Full Text Search">Chapter 12</a> for more detail.
108     </p></div><div class="sect2" id="DATATYPE-TSQUERY"><div class="titlepage"><div><div><h3 class="title">8.11.2. <code class="type">tsquery</code> <a href="#DATATYPE-TSQUERY" class="id_link">#</a></h3></div></div></div><a id="id-1.5.7.19.6.2" class="indexterm"></a><p>
109      A <code class="type">tsquery</code> value stores lexemes that are to be
110      searched for, and can combine them using the Boolean operators
111      <code class="literal">&amp;</code> (AND), <code class="literal">|</code> (OR), and
112      <code class="literal">!</code> (NOT), as well as the phrase search operator
113      <code class="literal">&lt;-&gt;</code> (FOLLOWED BY).  There is also a variant
114      <code class="literal">&lt;<em class="replaceable"><code>N</code></em>&gt;</code> of the FOLLOWED BY
115      operator, where <em class="replaceable"><code>N</code></em> is an integer constant that
116      specifies the distance between the two lexemes being searched
117      for.  <code class="literal">&lt;-&gt;</code> is equivalent to <code class="literal">&lt;1&gt;</code>.
118     </p><p>
119      Parentheses can be used to enforce grouping of these operators.
120      In the absence of parentheses, <code class="literal">!</code> (NOT) binds most tightly,
121      <code class="literal">&lt;-&gt;</code> (FOLLOWED BY) next most tightly, then
122      <code class="literal">&amp;</code> (AND), with <code class="literal">|</code> (OR) binding
123      the least tightly.
124     </p><p>
125      Here are some examples:
126
127 </p><pre class="programlisting">
128 SELECT 'fat &amp; rat'::tsquery;
129     tsquery
130 ---------------
131  'fat' &amp; 'rat'
132
133 SELECT 'fat &amp; (rat | cat)'::tsquery;
134           tsquery
135 ---------------------------
136  'fat' &amp; ( 'rat' | 'cat' )
137
138 SELECT 'fat &amp; rat &amp; ! cat'::tsquery;
139         tsquery
140 ------------------------
141  'fat' &amp; 'rat' &amp; !'cat'
142 </pre><p>
143     </p><p>
144      Optionally, lexemes in a <code class="type">tsquery</code> can be labeled with
145      one or more weight letters, which restricts them to match only
146      <code class="type">tsvector</code> lexemes with one of those weights:
147
148 </p><pre class="programlisting">
149 SELECT 'fat:ab &amp; cat'::tsquery;
150     tsquery
151 ------------------
152  'fat':AB &amp; 'cat'
153 </pre><p>
154     </p><p>
155      Also, lexemes in a <code class="type">tsquery</code> can be labeled with <code class="literal">*</code>
156      to specify prefix matching:
157 </p><pre class="programlisting">
158 SELECT 'super:*'::tsquery;
159   tsquery
160 -----------
161  'super':*
162 </pre><p>
163      This query will match any word in a <code class="type">tsvector</code> that begins
164      with <span class="quote">“<span class="quote">super</span>”</span>.
165     </p><p>
166      Quoting rules for lexemes are the same as described previously for
167      lexemes in <code class="type">tsvector</code>; and, as with <code class="type">tsvector</code>,
168      any required normalization of words must be done before converting
169      to the <code class="type">tsquery</code> type.  The <code class="function">to_tsquery</code>
170      function is convenient for performing such normalization:
171
172 </p><pre class="programlisting">
173 SELECT to_tsquery('Fat:ab &amp; Cats');
174     to_tsquery
175 ------------------
176  'fat':AB &amp; 'cat'
177 </pre><p>
178
179      Note that <code class="function">to_tsquery</code> will process prefixes in the same way
180      as other words, which means this comparison returns true:
181
182 </p><pre class="programlisting">
183 SELECT to_tsvector( 'postgraduate' ) @@ to_tsquery( 'postgres:*' );
184  ?column?
185 ----------
186  t
187 </pre><p>
188      because <code class="literal">postgres</code> gets stemmed to <code class="literal">postgr</code>:
189 </p><pre class="programlisting">
190 SELECT to_tsvector( 'postgraduate' ), to_tsquery( 'postgres:*' );
191   to_tsvector  | to_tsquery
192 ---------------+------------
193  'postgradu':1 | 'postgr':*
194 </pre><p>
195      which will match the stemmed form of <code class="literal">postgraduate</code>.
196     </p></div></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="datatype-bit.html" title="8.10. Bit String Types">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="datatype.html" title="Chapter 8. Data Types">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="datatype-uuid.html" title="8.12. UUID Type">Next</a></td></tr><tr><td width="40%" align="left" valign="top">8.10. Bit String Types </td><td width="20%" align="center"><a accesskey="h" href="index.html" title="PostgreSQL 18.0 Documentation">Home</a></td><td width="40%" align="right" valign="top"> 8.12. <acronym class="acronym">UUID</acronym> Type</td></tr></table></div></body></html>