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.40,Table 9.41 andTable 9.42 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.40. Text Search Operators

OperatorReturn TypeDescriptionExampleResult
@@booleantsvector matchestsquery ?to_tsvector('fat cats ate rats') @@ to_tsquery('cat & rat')t
@@@booleandeprecated synonym for@@to_tsvector('fat cats ate rats') @@@ to_tsquery('cat & rat')t
||tsvectorconcatenatetsvectors'a:1 b:2'::tsvector || 'c:1 d:2 b:3'::tsvector'a':1 'b':2,5 'c':3 'd':4
&&tsqueryANDtsquerys together'fat | rat'::tsquery && 'cat'::tsquery( 'fat' | 'rat' ) & 'cat'
||tsqueryORtsquerys together'fat | rat'::tsquery || 'cat'::tsquery( 'fat' | 'rat' ) | 'cat'
!!tsquerynegate atsquery!! 'cat'::tsquery!'cat'
<->tsquerytsquery followed bytsqueryto_tsquery('fat') <-> to_tsquery('rat')'fat' <-> 'rat'
@>booleantsquery contains another ?'cat'::tsquery @> 'cat & rat'::tsqueryf
<@booleantsquery is contained in ?'cat'::tsquery <@ 'cat & rat'::tsqueryt

Note

Thetsquery containment operators consider only the lexemes listed in the two queries, ignoring the combining operators.

In addition to the operators shown in the table, the ordinary B-tree comparison operators (=,<, etc) are defined 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.41. Text Search Functions

FunctionReturn TypeDescriptionExampleResult
array_to_tsvector(text[])tsvectorconvert array of lexemes totsvectorarray_to_tsvector('{fat,cat,rat}'::text[])'cat' 'fat' 'rat'
get_current_ts_config()regconfigget default text search configurationget_current_ts_config()english
length(tsvector)integernumber of lexemes intsvectorlength('fat:2,4 cat:3 rat:5A'::tsvector)3
numnode(tsquery)integernumber of lexemes plus operators intsquery numnode('(fat & rat) | cat'::tsquery)5
plainto_tsquery([configregconfig ,]querytext)tsqueryproducetsquery ignoring punctuationplainto_tsquery('english', 'The Fat Rats')'fat' & 'rat'
phraseto_tsquery([configregconfig ,]querytext)tsqueryproducetsquery that searches for a phrase, ignoring punctuationphraseto_tsquery('english', 'The Fat Rats')'fat' <-> 'rat'
websearch_to_tsquery([configregconfig ,]querytext)tsqueryproducetsquery from a web search style querywebsearch_to_tsquery('english', '"fat rat" or rat')'fat' <-> 'rat' | 'rat'
querytree(querytsquery)textget indexable part of atsqueryquerytree('foo & ! bar'::tsquery)'foo'
setweight(vectortsvector,weight"char")tsvectorassignweight to each element ofvectorsetweight('fat:2,4 cat:3 rat:5B'::tsvector, 'A')'cat':3A 'fat':2A,4A 'rat':5A
setweight(vectortsvector,weight"char",lexemestext[])tsvectorassignweight to elements ofvector that are listed inlexemessetweight('fat:2,4 cat:3 rat:5B'::tsvector, 'A', '{cat,rat}')'cat':3A 'fat':2,4 'rat':5A
strip(tsvector)tsvectorremove positions and weights fromtsvectorstrip('fat:2,4 cat:3 rat:5A'::tsvector)'cat' 'fat' 'rat'
to_tsquery([configregconfig ,]querytext)tsquerynormalize words and convert totsqueryto_tsquery('english', 'The & Fat & Rats')'fat' & 'rat'
to_tsvector([configregconfig ,]documenttext)tsvectorreduce document text totsvectorto_tsvector('english', 'The Fat Rats')'fat':2 'rat':3
to_tsvector([configregconfig ,]documentjson(b))tsvector reduce each string value in the document to atsvector, and then concatenate those in document order to produce a singletsvectorto_tsvector('english', '{"a": "The Fat Rats"}'::json)'fat':2 'rat':3
json(b)_to_tsvector([configregconfig,]documentjson(b),filterjson(b))tsvector reduce each value in the document, specified byfilter to atsvector, and then concatenate those in document order to produce a singletsvector.filter is ajsonb array, that enumerates what kind of elements need to be included into the resultingtsvector. Possible values forfilter are"string" (to include all string values),"numeric" (to include all numeric values in the string format),"boolean" (to include all Boolean values in the string format"true"/"false"),"key" (to include all keys) or"all" (to include all above). These values can be combined together to include, e.g., all string and numeric values.json_to_tsvector('english', '{"a": "The Fat Rats", "b": 123}'::json, '["string", "numeric"]')'123':5 'fat':2 'rat':3
ts_delete(vectortsvector,lexemetext)tsvectorremove givenlexeme fromvectorts_delete('fat:2,4 cat:3 rat:5A'::tsvector, 'fat')'cat':3 'rat':5A
ts_delete(vectortsvector,lexemestext[])tsvectorremove any occurrence of lexemes inlexemes fromvectorts_delete('fat:2,4 cat:3 rat:5A'::tsvector, ARRAY['fat','rat'])'cat':3
ts_filter(vectortsvector,weights"char"[])tsvectorselect only elements with givenweights fromvectorts_filter('fat:2,4 cat:3b rat:5A'::tsvector, '{a,b}')'cat':3B 'rat':5A
ts_headline([configregconfig,]documenttext,querytsquery [,optionstext])textdisplay a query matchts_headline('x y z', 'z'::tsquery)x y <b>z</b>
ts_headline([configregconfig,]documentjson(b),querytsquery [,optionstext])textdisplay a query matchts_headline('{"a":"x y z"}'::json, 'z'::tsquery){"a":"x y <b>z</b>"}
ts_rank([weightsfloat4[],]vectortsvector,querytsquery [,normalizationinteger])float4rank document for queryts_rank(textsearch, query)0.818
ts_rank_cd([weightsfloat4[],]vectortsvector,querytsquery [,normalizationinteger])float4rank document for query using cover densityts_rank_cd('{0.1, 0.2, 0.4, 1.0}', textsearch, query)2.01317
ts_rewrite(querytsquery,targettsquery,substitutetsquery)tsqueryreplacetarget withsubstitute within queryts_rewrite('a & b'::tsquery, 'a'::tsquery, 'foo|bar'::tsquery)'b' & ( 'foo' | 'bar' )
ts_rewrite(querytsquery,selecttext)tsqueryreplace using targets and substitutes from aSELECT commandSELECT ts_rewrite('a & b'::tsquery, 'SELECT t,s FROM aliases')'b' & ( 'foo' | 'bar' )
tsquery_phrase(query1tsquery,query2tsquery)tsquerymake query that searches forquery1 followed byquery2 (same as<-> operator)tsquery_phrase(to_tsquery('fat'), to_tsquery('cat'))'fat' <-> 'cat'
tsquery_phrase(query1tsquery,query2tsquery,distanceinteger)tsquerymake query that searches forquery1 followed byquery2 at distancedistancetsquery_phrase(to_tsquery('fat'), to_tsquery('cat'), 10)'fat' <10> 'cat'
tsvector_to_array(tsvector)text[]converttsvector to array of lexemestsvector_to_array('fat:2,4 cat:3 rat:5A'::tsvector){cat,fat,rat}
tsvector_update_trigger()triggertrigger function for automatictsvector column updateCREATE TRIGGER ... tsvector_update_trigger(tsvcol, 'pg_catalog.swedish', title, body)
tsvector_update_trigger_column()triggertrigger function for automatictsvector column updateCREATE TRIGGER ... tsvector_update_trigger_column(tsvcol, configcol, title, body)
unnest(tsvector, OUTlexemetext, OUTpositionssmallint[], OUTweightstext)setof recordexpand atsvector to a set of rowsunnest('fat:2,4 cat:3 rat:5A'::tsvector)(cat,{3},{D}) ...

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.42 are listed separately because they are not usually used in everyday text searching operations. They are helpful for development and debugging of new text search configurations.

Table 9.42. Text Search Debugging Functions

FunctionReturn TypeDescriptionExampleResult
ts_debug([configregconfig,]documenttext, OUTaliastext, OUTdescriptiontext, OUTtokentext, OUTdictionariesregdictionary[], OUTdictionaryregdictionary, OUTlexemestext[])setof recordtest a configurationts_debug('english', 'The Brightest supernovaes')(asciiword,"Word, all ASCII",The,{english_stem},english_stem,{}) ...
ts_lexize(dictregdictionary,tokentext)text[]test a dictionaryts_lexize('english_stem', 'stars'){star}
ts_parse(parser_nametext,documenttext, OUTtokidinteger, OUTtokentext)setof recordtest a parserts_parse('default', 'foo - bar')(1,foo) ...
ts_parse(parser_oidoid,documenttext, OUTtokidinteger, OUTtokentext)setof recordtest a parserts_parse(3722, 'foo - bar')(1,foo) ...
ts_token_type(parser_nametext, OUTtokidinteger, OUTaliastext, OUTdescriptiontext)setof recordget token types defined by parserts_token_type('default')(1,asciiword,"Word, all ASCII") ...
ts_token_type(parser_oidoid, OUTtokidinteger, OUTaliastext, OUTdescriptiontext)setof recordget token types defined by parserts_token_type(3722)(1,asciiword,"Word, all ASCII") ...
ts_stat(sqlquerytext, [weightstext,] OUTwordtext, OUTndocinteger, OUTnentryinteger)setof recordget statistics of atsvector columnts_stat('SELECT vector from apod')(foo,10,15) ...


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

[8]ページ先頭

©2009-2025 Movatter.jp