Greenplum Database provides two data types that are designed to support full text search, which is the activity of searching through a collection of natural-language documents to locate those that best match a query. The tsvector type represents a document in a form optimized for text search; the tsquery type similarly represents a text query. Using Full Text Search provides a detailed explanation of this facility, and Text Search Functions and Operators summarizes the related functions and operators.

The tsvector and tsquery types cannot be part of the distribution key of a Greenplum Database table.

Parent topic: Data Types

tsvector

A tsvector value is a sorted list of distinct lexemes, which are words that have been normalized to merge different variants of the same word (see Using Full Text Search for details). Sorting and duplicate-elimination are done automatically during input, as shown in this example:

  1. SELECT 'a fat cat sat on a mat and ate a fat rat'::tsvector;
  2. tsvector
  3. ----------------------------------------------------
  4. 'a' 'and' 'ate' 'cat' 'fat' 'mat' 'on' 'rat' 'sat'

To represent lexemes containing whitespace or punctuation, surround them with quotes:

  1. SELECT $$the lexeme ' ' contains spaces$$::tsvector;
  2. tsvector
  3. -------------------------------------------
  4. ' ' 'contains' 'lexeme' 'spaces' 'the'

(We use dollar-quoted string literals in this example and the next one to avoid the confusion of having to double quote marks within the literals.) Embedded quotes and backslashes must be doubled:

  1. SELECT $$the lexeme 'Joe''s' contains a quote$$::tsvector;
  2. tsvector
  3. ------------------------------------------------
  4. 'Joe''s' 'a' 'contains' 'lexeme' 'quote' 'the'

Optionally, integer positions can be attached to lexemes:

  1. 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;
  2. tsvector
  3. -------------------------------------------------------------------------------
  4. 'a':1,6,10 'and':8 'ate':9 'cat':3 'fat':2,11 'mat':7 'on':5 'rat':12 'sat':4

A position normally indicates the source word’s location in the document. Positional information can be used for proximity ranking. Position values can range from 1 to 16383; larger numbers are silently set to 16383. Duplicate positions for the same lexeme are discarded.

Lexemes that have positions can further be labeled with a weight, which can be A, B, C, or D. D is the default and hence is not shown on output:

  1. SELECT 'a:1A fat:2B,4C cat:5D'::tsvector;
  2. tsvector
  3. ----------------------------
  4. 'a':1A 'cat':5 'fat':2B,4C

Weights are typically used to reflect document structure, for example by marking title words differently from body words. Text search ranking functions can assign different priorities to the different weight markers.

It is important to understand that the tsvector type itself does not perform any normalization; it assumes the words it is given are normalized appropriately for the application. For example,

  1. select 'The Fat Rats'::tsvector;
  2. tsvector
  3. --------------------
  4. 'Fat' 'Rats' 'The'

For most English-text-searching applications the above words would be considered non-normalized, but tsvector doesn’t care. Raw document text should usually be passed through to_tsvector to normalize the words appropriately for searching:

  1. SELECT to_tsvector('english', 'The Fat Rats');
  2. to_tsvector
  3. -----------------
  4. 'fat':2 'rat':3

tsquery

A tsquery value stores lexemes that are to be searched for, and combines them honoring the Boolean operators & (AND), | (OR), and ! (NOT). Parentheses can be used to enforce grouping of the operators:

  1. SELECT 'fat & rat'::tsquery;
  2. tsquery
  3. ---------------
  4. 'fat' & 'rat'
  5. SELECT 'fat & (rat | cat)'::tsquery;
  6. tsquery
  7. ---------------------------
  8. 'fat' & ( 'rat' | 'cat' )
  9. SELECT 'fat & rat & ! cat'::tsquery;
  10. tsquery
  11. ------------------------
  12. 'fat' & 'rat' & !'cat'

In the absence of parentheses, ! (NOT) binds most tightly, and & (AND) binds more tightly than | (OR).

Optionally, lexemes in a tsquery can be labeled with one or more weight letters, which restricts them to match only tsvector lexemes with matching weights:

  1. SELECT 'fat:ab & cat'::tsquery;
  2. tsquery
  3. ------------------
  4. 'fat':AB & 'cat'

Also, lexemes in a tsquery can be labeled with * to specify prefix matching:

  1. SELECT 'super:*'::tsquery;
  2. tsquery
  3. -----------
  4. 'super':*

This query will match any word in a tsvector that begins with “super”. Note that prefixes are first processed by text search configurations, which means this comparison returns true:

  1. SELECT to_tsvector( 'postgraduate' ) @@ to_tsquery( 'postgres:*' );
  2. ?column?
  3. ----------
  4. t
  5. (1 row)

because postgres gets stemmed to postgr:

  1. SELECT to_tsquery('postgres:*');
  2. to_tsquery
  3. ------------
  4. 'postgr':*
  5. (1 row)

which then matches postgraduate.

Quoting rules for lexemes are the same as described previously for lexemes in tsvector; and, as with tsvector, any required normalization of words must be done before converting to the tsquery type. The to_tsquery function is convenient for performing such normalization:

  1. SELECT to_tsquery('Fat:ab & Cats');
  2. to_tsquery
  3. ------------------
  4. 'fat':AB & 'cat'