他のバージョンの文書 15 | 14 | 13 | 12 | 11 | 10 | 9.6 | 9.5 | 9.4 | 9.3 | 9.2 | 9.1 | 9.0 | 8.4 | 8.3 | 8.2 | 8.1 | 8.0 | 7.4 | 7.3 | 7.2

9.13. テキスト検索関数と演算子

表9.37「テキスト検索演算子」表9.38「テキスト検索関数」および 表9.39「テキスト検索デバッグ関数」は全文検索用に提供されている関数と演算子を要約しています。PostgreSQLのテキスト検索機能の詳細は12章全文検索を参照してください。

表9.37 テキスト検索演算子

演算子説明結果
@@ tsvectortsqueryの条件に合うか?to_tsvector('fat cats ate rats') @@ to_tsquery('cat & rat')t
@@@ @@に対する廃止予定の同義語to_tsvector('fat cats ate rats') @@@ to_tsquery('cat & rat')t
|| tsvectorを連結'a:1 b:2'::tsvector || 'c:1 d:2 b:3'::tsvector'a':1 'b':2,5 'c':3 'd':4
&& tsqueryにAND演算を行う'fat | rat'::tsquery && 'cat'::tsquery( 'fat' | 'rat' ) & 'cat'
|| tsqueryにOR演算を行う'fat | rat'::tsquery || 'cat'::tsquery( 'fat' | 'rat' ) | 'cat'
!! tsqueryを否定!! 'cat'::tsquery!'cat'
@> tsqueryは他方を包含するか?'cat'::tsquery @> 'cat & rat'::tsqueryf
<@ tsqueryは他方に包含されるか?'cat'::tsquery <@ 'cat & rat'::tsqueryt

注記

tsqueryの包含演算子は2つの問い合わせで列挙された語彙素のみを対象とし、結合演算子を無視します。

表に示された演算子に加え、通常のB-tree比較演算子(=<など)が、型tsvectorおよびtsqueryに対して定義されます。 これらはテキスト検索に対してそれほど有用ではありませんが、例えばこれらの型の列に一意インデックスを作成することを可能にします。

表9.38 テキスト検索関数

関数戻り値型説明結果
get_current_ts_config() regconfig デフォルトのテキスト検索設定の取得 get_current_ts_config()english
length(tsvector) integertsvectorにある語彙素の数length('fat:2,4 cat:3 rat:5A'::tsvector)3
numnode(tsquery) integer tsqueryにある語彙素の数と演算子の数の和 numnode('(fat & rat) | cat'::tsquery)5
plainto_tsquery([ config regconfig , ] query text) tsquery句読点を無視して、tsqueryを作成plainto_tsquery('english', 'The Fat Rats')'fat' & 'rat'
querytree(query tsquery) texttsqueryのインデックス付け可能部分の取得querytree('foo & ! bar'::tsquery)'foo'
setweight(tsvector, "char") tsvectortsvectorのそれぞれの要素に重みを割り当てsetweight('fat:2,4 cat:3 rat:5B'::tsvector, 'A')'cat':3A 'fat':2A,4A 'rat':5A
strip(tsvector) tsvectortsvectorから位置と重みを削除strip('fat:2,4 cat:3 rat:5A'::tsvector)'cat' 'fat' 'rat'
to_tsquery([ config regconfig , ] query text) tsquery単語(複数)を正規化しtsqueryに変換to_tsquery('english', 'The & Fat & Rats')'fat' & 'rat'
to_tsvector([ config regconfig , ] document text) tsvectorドキュメントテキストをtsvectorに縮小to_tsvector('english', 'The Fat Rats')'fat':2 'rat':3
ts_headline([ config regconfig, ] document text, query tsquery [, options text ]) text問い合わせによるマッチを表示ts_headline('x y z', 'z'::tsquery)x y <b>z</b>
ts_rank([ weights float4[], ] vector tsvector, query tsquery [, normalization integer ]) float4問い合わせのためのドキュメント順位付けts_rank(textsearch, query)0.818
ts_rank_cd([ weights float4[], ] vector tsvector, query tsquery [, normalization integer ]) float4被覆密度を用いた問い合わせのためのドキュメント順位付けts_rank_cd('{0.1, 0.2, 0.4, 1.0}', textsearch, query)2.01317
ts_rewrite(query tsquery, target tsquery, substitute tsquery) tsquery問い合わせ内で対象を代替と置換ts_rewrite('a & b'::tsquery, 'a'::tsquery, 'foo|bar'::tsquery)'b' & ( 'foo' | 'bar' )
ts_rewrite(query tsquery, select text)tsquerySELECTから対象と代替を使用して置換SELECT ts_rewrite('a & b'::tsquery, 'SELECT t,s FROM aliases')'b' & ( 'foo' | 'bar' )
tsvector_update_trigger() triggertsvector列の自動更新のためのトリガ関数CREATE TRIGGER ... tsvector_update_trigger(tsvcol, 'pg_catalog.swedish', title, body)
tsvector_update_trigger_column() triggertsvector列の自動更新のためのトリガ関数CREATE TRIGGER ... tsvector_update_trigger_column(tsvcol, configcol, title, body)

注記

オプションのregconfig引数を受け付ける全てのテキスト検索関数は、その引数が省略された場合default_text_search_configで指定された構成を使用します。

表9.39「テキスト検索デバッグ関数」の関数は、日常のテキスト検索操作では通常使用されないので、別の表にしました。 これらは新しいテキスト検索設定の開発およびデバッグに役立ちます。

表9.39 テキスト検索デバッグ関数

関数戻り値型説明結果
ts_debug([ config regconfig, ] document text, OUT alias text, OUT description text, OUT token text, OUT dictionaries regdictionary[], OUT dictionary regdictionary, OUT lexemes text[]) setof record構成を検査ts_debug('english', 'The Brightest supernovaes')(asciiword,"Word, all ASCII",The,{english_stem},english_stem,{}) ...
ts_lexize(dict regdictionary, token text) text[]辞書を検査ts_lexize('english_stem', 'stars'){star}
ts_parse(parser_name text, document text, OUT tokid integer, OUT token text) setof recordパーサを検査ts_parse('default', 'foo - bar')(1,foo) ...
ts_parse(parser_oid oid, document text, OUT tokid integer, OUT token text)setof recordパーサを検査ts_parse(3722, 'foo - bar')(1,foo) ...
ts_token_type(parser_name text, OUT tokid integer, OUT alias text, OUT description text) setof recordパーサで定義されたトークンの型を入手ts_token_type('default')(1,asciiword,"Word, all ASCII") ...
ts_token_type(parser_oid oid, OUT tokid integer, OUT alias text, OUT description text)setof recordパーサで定義されたトークンの型を入手ts_token_type(3722)(1,asciiword,"Word, all ASCII") ...
ts_stat(sqlquery text, [ weights text, ] OUT word text, OUT ndoc integer, OUT nentry integer) setof recordtsvector列の統計情報を入手ts_stat('SELECT vector from apod')(foo,10,15) ...