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
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
21 </p><pre class="programlisting">
22 SELECT 'a fat cat sat on a mat and ate a fat rat'::tsvector;
24 ----------------------------------------------------
25 'a' 'and' 'ate' 'cat' 'fat' 'mat' 'on' 'rat' 'sat'
29 lexemes containing whitespace or punctuation, surround them with quotes:
31 </p><pre class="programlisting">
32 SELECT $$the lexeme ' ' contains spaces$$::tsvector;
34 -------------------------------------------
35 ' ' 'contains' 'lexeme' 'spaces' 'the'
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:
42 </p><pre class="programlisting">
43 SELECT $$the lexeme 'Joe''s' contains a quote$$::tsvector;
45 ------------------------------------------------
46 'Joe''s' 'a' 'contains' 'lexeme' 'quote' 'the'
49 Optionally, integer <em class="firstterm">positions</em>
50 can be attached to lexemes:
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;
55 -------------------------------------------------------------------------------
56 'a':1,6,10 'and':8 'ate':9 'cat':3 'fat':2,11 'mat':7 'on':5 'rat':12 'sat':4
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.
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:
70 </p><pre class="programlisting">
71 SELECT 'a:1A fat:2B,4C cat:5D'::tsvector;
73 ----------------------------
74 'a':1A 'cat':5 'fat':2B,4C
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
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,
87 </p><pre class="programlisting">
88 SELECT 'The Fat Rats'::tsvector;
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
100 </p><pre class="programlisting">
101 SELECT to_tsvector('english', 'The Fat Rats');
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">&</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"><-></code> (FOLLOWED BY). There is also a variant
114 <code class="literal"><<em class="replaceable"><code>N</code></em>></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"><-></code> is equivalent to <code class="literal"><1></code>.
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"><-></code> (FOLLOWED BY) next most tightly, then
122 <code class="literal">&</code> (AND), with <code class="literal">|</code> (OR) binding
125 Here are some examples:
127 </p><pre class="programlisting">
128 SELECT 'fat & rat'::tsquery;
133 SELECT 'fat & (rat | cat)'::tsquery;
135 ---------------------------
136 'fat' & ( 'rat' | 'cat' )
138 SELECT 'fat & rat & ! cat'::tsquery;
140 ------------------------
141 'fat' & 'rat' & !'cat'
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:
148 </p><pre class="programlisting">
149 SELECT 'fat:ab & cat'::tsquery;
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;
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>.
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:
172 </p><pre class="programlisting">
173 SELECT to_tsquery('Fat:ab & Cats');
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:
182 </p><pre class="programlisting">
183 SELECT to_tsvector( 'postgraduate' ) @@ to_tsquery( 'postgres:*' );
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':*
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>