Movatterモバイル変換


[0]ホーム

URL:



Facebook
Postgres Pro
Facebook
Downloads
9.13. Text Search Functions and Operators
Prev UpChapter 9. Functions and OperatorsHome Next

9.13. Text Search Functions and Operators#

Table 9.42,Table 9.43 andTable 9.44 summarize the functions and operators that are provided for full text searching. SeeChapter 12 for a detailed explanation ofPostgreSQL's text search facility.

Table 9.42. Text Search Operators

Operator

Description

Example(s)

tsvector@@tsqueryboolean

tsquery@@tsvectorboolean

Doestsvector matchtsquery? (The arguments can be given in either order.)

to_tsvector('fat cats ate rats') @@ to_tsquery('cat & rat')t

text@@tsqueryboolean

Does text string, after implicit invocation ofto_tsvector(), matchtsquery?

'fat cats ate rats' @@ to_tsquery('cat & rat')t

tsvector||tsvectortsvector

Concatenates twotsvectors. If both inputs contain lexeme positions, the second input's positions are adjusted accordingly.

'a:1 b:2'::tsvector || 'c:1 d:2 b:3'::tsvector'a':1 'b':2,5 'c':3 'd':4

tsquery&&tsquerytsquery

ANDs twotsquerys together, producing a query that matches documents that match both input queries.

'fat | rat'::tsquery && 'cat'::tsquery( 'fat' | 'rat' ) & 'cat'

tsquery||tsquerytsquery

ORs twotsquerys together, producing a query that matches documents that match either input query.

'fat | rat'::tsquery || 'cat'::tsquery'fat' | 'rat' | 'cat'

!!tsquerytsquery

Negates atsquery, producing a query that matches documents that do not match the input query.

!! 'cat'::tsquery!'cat'

tsquery<->tsquerytsquery

Constructs a phrase query, which matches if the two input queries match at successive lexemes.

to_tsquery('fat') <-> to_tsquery('rat')'fat' <-> 'rat'

tsquery@>tsqueryboolean

Does firsttsquery contain the second? (This considers only whether all the lexemes appearing in one query appear in the other, ignoring the combining operators.)

'cat'::tsquery @> 'cat & rat'::tsqueryf

tsquery<@tsqueryboolean

Is firsttsquery contained in the second? (This considers only whether all the lexemes appearing in one query appear in the other, ignoring the combining operators.)

'cat'::tsquery <@ 'cat & rat'::tsqueryt

'cat'::tsquery <@ '!cat & rat'::tsqueryt


In addition to these specialized operators, the usual comparison operators shown inTable 9.1 are available for typestsvector andtsquery. These are not very useful for text searching but allow, for example, unique indexes to be built on columns of these types.

Table 9.43. Text Search Functions

Function

Description

Example(s)

array_to_tsvector (text[] ) →tsvector

Converts an array of text strings to atsvector. The given strings are used as lexemes as-is, without further processing. Array elements must not be empty strings orNULL.

array_to_tsvector('{fat,cat,rat}'::text[])'cat' 'fat' 'rat'

get_current_ts_config ( ) →regconfig

Returns the OID of the current default text search configuration (as set bydefault_text_search_config).

get_current_ts_config()english

length (tsvector ) →integer

Returns the number of lexemes in thetsvector.

length('fat:2,4 cat:3 rat:5A'::tsvector)3

numnode (tsquery ) →integer

Returns the number of lexemes plus operators in thetsquery.

numnode('(fat & rat) | cat'::tsquery)5

plainto_tsquery ( [configregconfig,]querytext ) →tsquery

Converts text to atsquery, normalizing words according to the specified or default configuration. Any punctuation in the string is ignored (it does not determine query operators). The resulting query matches documents containing all non-stopwords in the text.

plainto_tsquery('english', 'The Fat Rats')'fat' & 'rat'

phraseto_tsquery ( [configregconfig,]querytext ) →tsquery

Converts text to atsquery, normalizing words according to the specified or default configuration. Any punctuation in the string is ignored (it does not determine query operators). The resulting query matches phrases containing all non-stopwords in the text.

phraseto_tsquery('english', 'The Fat Rats')'fat' <-> 'rat'

phraseto_tsquery('english', 'The Cat and Rats')'cat' <2> 'rat'

websearch_to_tsquery ( [configregconfig,]querytext ) →tsquery

Converts text to atsquery, normalizing words according to the specified or default configuration. Quoted word sequences are converted to phrase tests. The wordor is understood as producing an OR operator, and a dash produces a NOT operator; other punctuation is ignored. This approximates the behavior of some common web search tools.

websearch_to_tsquery('english', '"fat rat" or cat dog')'fat' <-> 'rat' | 'cat' & 'dog'

querytree (tsquery ) →text

Produces a representation of the indexable portion of atsquery. A result that is empty or justT indicates a non-indexable query.

querytree('foo & ! bar'::tsquery)'foo'

setweight (vectortsvector,weight"char" ) →tsvector

Assigns the specifiedweight to each element of thevector.

setweight('fat:2,4 cat:3 rat:5B'::tsvector, 'A')'cat':3A 'fat':2A,4A 'rat':5A

setweight (vectortsvector,weight"char",lexemestext[] ) →tsvector

Assigns the specifiedweight to elements of thevector that are listed inlexemes. The strings inlexemes are taken as lexemes as-is, without further processing. Strings that do not match any lexeme invector are ignored.

setweight('fat:2,4 cat:3 rat:5,6B'::tsvector, 'A', '{cat,rat}')'cat':3A 'fat':2,4 'rat':5A,6A

strip (tsvector ) →tsvector

Removes positions and weights from thetsvector.

strip('fat:2,4 cat:3 rat:5A'::tsvector)'cat' 'fat' 'rat'

to_tsquery ( [configregconfig,]querytext ) →tsquery

Converts text to atsquery, normalizing words according to the specified or default configuration. The words must be combined by validtsquery operators.

to_tsquery('english', 'The & Fat & Rats')'fat' & 'rat'

to_tsvector ( [configregconfig,]documenttext ) →tsvector

Converts text to atsvector, normalizing words according to the specified or default configuration. Position information is included in the result.

to_tsvector('english', 'The Fat Rats')'fat':2 'rat':3

to_tsvector ( [configregconfig,]documentjson ) →tsvector

to_tsvector ( [configregconfig,]documentjsonb ) →tsvector

Converts each string value in the JSON document to atsvector, normalizing words according to the specified or default configuration. The results are then concatenated in document order to produce the output. Position information is generated as though one stopword exists between each pair of string values. (Beware thatdocument order of the fields of a JSON object is implementation-dependent when the input isjsonb; observe the difference in the examples.)

to_tsvector('english', '{"aa": "The Fat Rats", "b": "dog"}'::json)'dog':5 'fat':2 'rat':3

to_tsvector('english', '{"aa": "The Fat Rats", "b": "dog"}'::jsonb)'dog':1 'fat':4 'rat':5

json_to_tsvector ( [configregconfig,]documentjson,filterjsonb ) →tsvector

jsonb_to_tsvector ( [configregconfig,]documentjsonb,filterjsonb ) →tsvector

Selects each item in the JSON document that is requested by thefilter and converts each one to atsvector, normalizing words according to the specified or default configuration. The results are then concatenated in document order to produce the output. Position information is generated as though one stopword exists between each pair of selected items. (Beware thatdocument order of the fields of a JSON object is implementation-dependent when the input isjsonb.) Thefilter must be ajsonb array containing zero or more of these keywords:"string" (to include all string values),"numeric" (to include all numeric values),"boolean" (to include all boolean values),"key" (to include all keys), or"all" (to include all the above). As a special case, thefilter can also be a simple JSON value that is one of these keywords.

json_to_tsvector('english', '{"a": "The Fat Rats", "b": 123}'::json, '["string", "numeric"]')'123':5 'fat':2 'rat':3

json_to_tsvector('english', '{"cat": "The Fat Rats", "dog": 123}'::json, '"all"')'123':9 'cat':1 'dog':7 'fat':4 'rat':5

ts_delete (vectortsvector,lexemetext ) →tsvector

Removes any occurrence of the givenlexeme from thevector. Thelexeme string is treated as a lexeme as-is, without further processing.

ts_delete('fat:2,4 cat:3 rat:5A'::tsvector, 'fat')'cat':3 'rat':5A

ts_delete (vectortsvector,lexemestext[] ) →tsvector

Removes any occurrences of the lexemes inlexemes from thevector. The strings inlexemes are taken as lexemes as-is, without further processing. Strings that do not match any lexeme invector are ignored.

ts_delete('fat:2,4 cat:3 rat:5A'::tsvector, ARRAY['fat','rat'])'cat':3

ts_filter (vectortsvector,weights"char"[] ) →tsvector

Selects only elements with the givenweights from thevector.

ts_filter('fat:2,4 cat:3b,7c rat:5A'::tsvector, '{a,b}')'cat':3B 'rat':5A

ts_headline ( [configregconfig,]documenttext,querytsquery [,optionstext] ) →text

Displays, in an abbreviated form, the match(es) for thequery in thedocument, which must be raw text not atsvector. Words in the document are normalized according to the specified or default configuration before matching to the query. Use of this function is discussed inSection 12.3.4, which also describes the availableoptions.

ts_headline('The fat cat ate the rat.', 'cat')The fat <b>cat</b> ate the rat.

ts_headline ( [configregconfig,]documentjson,querytsquery [,optionstext] ) →text

ts_headline ( [configregconfig,]documentjsonb,querytsquery [,optionstext] ) →text

Displays, in an abbreviated form, match(es) for thequery that occur in string values within the JSONdocument. SeeSection 12.3.4 for more details.

ts_headline('{"cat":"raining cats and dogs"}'::jsonb, 'cat'){"cat": "raining <b>cats</b> and dogs"}

ts_rank ( [weightsreal[],]vectortsvector,querytsquery [,normalizationinteger] ) →real

Computes a score showing how well thevector matches thequery. SeeSection 12.3.3 for details.

ts_rank(to_tsvector('raining cats and dogs'), 'cat')0.06079271

ts_rank_cd ( [weightsreal[],]vectortsvector,querytsquery [,normalizationinteger] ) →real

Computes a score showing how well thevector matches thequery, using a cover density algorithm. SeeSection 12.3.3 for details.

ts_rank_cd(to_tsvector('raining cats and dogs'), 'cat')0.1

ts_rewrite (querytsquery,targettsquery,substitutetsquery ) →tsquery

Replaces occurrences oftarget withsubstitute within thequery. SeeSection 12.4.2.1 for details.

ts_rewrite('a & b'::tsquery, 'a'::tsquery, 'foo|bar'::tsquery)'b' & ( 'foo' | 'bar' )

ts_rewrite (querytsquery,selecttext ) →tsquery

Replaces portions of thequery according to target(s) and substitute(s) obtained by executing aSELECT command. SeeSection 12.4.2.1 for details.

SELECT ts_rewrite('a & b'::tsquery, 'SELECT t,s FROM aliases')'b' & ( 'foo' | 'bar' )

tsquery_phrase (query1tsquery,query2tsquery ) →tsquery

Constructs a phrase query that searches for matches ofquery1 andquery2 at successive lexemes (same as<-> operator).

tsquery_phrase(to_tsquery('fat'), to_tsquery('cat'))'fat' <-> 'cat'

tsquery_phrase (query1tsquery,query2tsquery,distanceinteger ) →tsquery

Constructs a phrase query that searches for matches ofquery1 andquery2 that occur exactlydistance lexemes apart.

tsquery_phrase(to_tsquery('fat'), to_tsquery('cat'), 10)'fat' <10> 'cat'

tsvector_to_array (tsvector ) →text[]

Converts atsvector to an array of lexemes.

tsvector_to_array('fat:2,4 cat:3 rat:5A'::tsvector){cat,fat,rat}

unnest (tsvector ) →setof record (lexemetext,positionssmallint[],weightstext )

Expands atsvector into a set of rows, one per lexeme.

select * from unnest('cat:3 fat:2,4 rat:5A'::tsvector)

 lexeme | positions | weights--------+-----------+--------- cat    | {3}       | {D} fat    | {2,4}     | {D,D} rat    | {5}       | {A}


Note

All the text search functions that accept an optionalregconfig argument will use the configuration specified bydefault_text_search_config when that argument is omitted.

The functions inTable 9.44 are listed separately because they are not usually used in everyday text searching operations. They are primarily helpful for development and debugging of new text search configurations.

Table 9.44. Text Search Debugging Functions

Function

Description

Example(s)

ts_debug ( [configregconfig,]documenttext ) →setof record (aliastext,descriptiontext,tokentext,dictionariesregdictionary[],dictionaryregdictionary,lexemestext[] )

Extracts and normalizes tokens from thedocument according to the specified or default text search configuration, and returns information about how each token was processed. SeeSection 12.8.1 for details.

ts_debug('english', 'The Brightest supernovaes')(asciiword,"Word, all ASCII",The,{english_stem},english_stem,{}) ...

ts_lexize (dictregdictionary,tokentext ) →text[]

Returns an array of replacement lexemes if the input token is known to the dictionary, or an empty array if the token is known to the dictionary but it is a stop word, or NULL if it is not a known word. SeeSection 12.8.3 for details.

ts_lexize('english_stem', 'stars'){star}

ts_parse (parser_nametext,documenttext ) →setof record (tokidinteger,tokentext )

Extracts tokens from thedocument using the named parser. SeeSection 12.8.2 for details.

ts_parse('default', 'foo - bar')(1,foo) ...

ts_parse (parser_oidoid,documenttext ) →setof record (tokidinteger,tokentext )

Extracts tokens from thedocument using a parser specified by OID. SeeSection 12.8.2 for details.

ts_parse(3722, 'foo - bar')(1,foo) ...

ts_token_type (parser_nametext ) →setof record (tokidinteger,aliastext,descriptiontext )

Returns a table that describes each type of token the named parser can recognize. SeeSection 12.8.2 for details.

ts_token_type('default')(1,asciiword,"Word, all ASCII") ...

ts_token_type (parser_oidoid ) →setof record (tokidinteger,aliastext,descriptiontext )

Returns a table that describes each type of token a parser specified by OID can recognize. SeeSection 12.8.2 for details.

ts_token_type(3722)(1,asciiword,"Word, all ASCII") ...

ts_stat (sqlquerytext [,weightstext] ) →setof record (wordtext,ndocinteger,nentryinteger )

Executes thesqlquery, which must return a singletsvector column, and returns statistics about each distinct lexeme contained in the data. SeeSection 12.4.4 for details.

ts_stat('SELECT vector FROM apod')(foo,10,15) ...



Prev Up Next
9.12. Network Address Functions and Operators Home 9.14. UUID Functions
pdfepub
Go to PostgreSQL 17
By continuing to browse this website, you agree to the use of cookies. Go toPrivacy Policy.

[8]ページ先頭

©2009-2025 Movatter.jp