他のバージョンの文書 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.40表 9.41および表 9.42は全文検索用に提供されている関数と演算子を要約しています。PostgreSQLのテキスト検索機能の詳細は第12章を参照してください。

表9.40 テキスト検索演算子

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

注記

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

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

表9.41 テキスト検索関数

関数戻り値型説明結果
array_to_tsvector(text[]) tsvector語彙素の配列をtsvectorに変換array_to_tsvector('{fat,cat,rat}'::text[])'cat' 'fat' 'rat'
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'
phraseto_tsquery([ config regconfig , ] query text) tsquery句読点を無視して、語句を検索するtsqueryを生成phraseto_tsquery('english', 'The Fat Rats')'fat' <-> 'rat'
websearch_to_tsquery([ config regconfig , ] query text) tsqueryweb検索形式の問い合わせからtsqueryを生成websearch_to_tsquery('english', '"fat rat" or rat')'fat' <-> 'rat' | 'rat'
querytree(query tsquery) texttsqueryのインデックス付け可能部分の取得querytree('foo & ! bar'::tsquery)'foo'
setweight(vector tsvector, weight "char") tsvectorvectorの各要素にweightを割り当てるsetweight('fat:2,4 cat:3 rat:5B'::tsvector, 'A')'cat':3A 'fat':2A,4A 'rat':5A
setweight(vector tsvector, weight "char", lexemes text[]) tsvectorlexemesに列挙されたvectorの要素にweightを割り当てるsetweight('fat:2,4 cat:3 rat:5B'::tsvector, 'A', '{cat,rat}')'cat':3A 'fat':2,4 '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
to_tsvector([ config regconfig , ] document json(b)) tsvector ドキュメント内の各文字列の値をtsvectorに縮小し、それを繋げて一つのtsvectorにする to_tsvector('english', '{"a": "The Fat Rats"}'::json)'fat':2 'rat':3
json(b)_to_tsvector([ config regconfig, ] document json(b), filter json(b)) tsvector filterによって指定された文書中の各々の値をtsvectorにまとめ、次に文書にあらわれる順にそれらを結合して単一のtsvectorを生成します。 filterjsonbの配列で、結果のtsvectorにどの種類の要素を含める必要があるのかを列挙します。 filterに指定可能な値は、"string" (すべての文字列値を含める)、"numeric" (すべての文字列形式の数値を含める)、"key" (すべてのキーを含める)、"all" (それらすべてを含める)です。 これらの値は、たとえばすべての文字列と数値、のように組み合わせることができます。 json_to_tsvector('english', '{"a": "The Fat Rats", "b": 123}'::json, '["string", "numeric"]')'123':5 'fat':2 'rat':3
ts_delete(vector tsvector, lexeme text) tsvectorvectorから指定のlexemeを削除するts_delete('fat:2,4 cat:3 rat:5A'::tsvector, 'fat')'cat':3 'rat':5A
ts_delete(vector tsvector, lexemes text[]) tsvectorvectorからlexemes内にある語彙素の出現のすべてを削除するts_delete('fat:2,4 cat:3 rat:5A'::tsvector, ARRAY['fat','rat'])'cat':3
ts_filter(vector tsvector, weights "char"[]) tsvectorvectorから指定のweightsの要素のみを選択するts_filter('fat:2,4 cat:3b rat:5A'::tsvector, '{a,b}')'cat':3B 'rat':5A
ts_headline([ config regconfig, ] document text, query tsquery [, options text ]) text問い合わせによるマッチを表示ts_headline('x y z', 'z'::tsquery)x y <b>z</b>
ts_headline([ config regconfig, ] document json(b), query tsquery [, options text ]) text問い合わせによるマッチを表示ts_headline('{"a":"x y z"}'::json, 'z'::tsquery){"a":"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問い合わせ内のtargetsubstituteで置換する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' )
tsquery_phrase(query1 tsquery, query2 tsquery) tsqueryquery1の後にquery2が続くものを検索する問い合わせを作成する(<->演算子と同じ)tsquery_phrase(to_tsquery('fat'), to_tsquery('cat'))'fat' <-> 'cat'
tsquery_phrase(query1 tsquery, query2 tsquery, distance integer) tsqueryquery1の後にdistanceの距離でquery2があるものを検索する問い合わせを作成するtsquery_phrase(to_tsquery('fat'), to_tsquery('cat'), 10)'fat' <10> 'cat'
tsvector_to_array(tsvector) text[]tsvectorを語彙素の配列に変換するtsvector_to_array('fat:2,4 cat:3 rat:5A'::tsvector){cat,fat,rat}
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)
unnest(tsvector, OUT lexeme text, OUT positions smallint[], OUT weights text) setof recordtsvectorを行の集合に展開するunnest('fat:2,4 cat:3 rat:5A'::tsvector)(cat,{3},{D}) ...

注記

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

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

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

関数戻り値型説明結果
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) ...