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

表9.41 テキスト検索演算子

演算子

説明

tsvector @@ tsqueryboolean

tsquery @@ tsvectorboolean

tsvectortsqueryの条件に合うか? (引数は任意の順で与えることができます。)

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

text @@ tsqueryboolean

テキスト文字列はto_tsvector()の暗黙的な呼び出し後にtsqueryの条件に合うか?

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

tsvector @@@ tsqueryboolean

tsquery @@@ tsvectorboolean

@@に対する廃止予定の同義語です。

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

tsvector || tsvectortsvector

2つのtsvectorを連結します。 両方の入力が語彙素の位置を含んでいるなら2番目の入力の位置はそれにしたがって調整されます。

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

tsquery && tsquerytsquery

2つのtsqueryの論理積を取り、両方の入力問い合わせにマッチする文書にマッチする問い合わせを生成します。

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

tsquery || tsquerytsquery

2つのtsqueryの論理和を取り、どちらかの入力問い合わせにマッチする文書にマッチする問い合わせを生成します。

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

!! tsquerytsquery

tsqueryの否定を取り、入力問い合わせにマッチしない文書にマッチする問い合わせを生成します。

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

tsquery <-> tsquerytsquery

2つの入力問い合わせが連続する語彙素にマッチする場合にマッチする語句問い合わせを作成します。

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

tsquery @> tsqueryboolean

最初のtsqueryは2番目を含んでいるか? (これは結合演算子を無視して、単に一方の問い合わせ中のすべての語彙素が他方に現れるかどうかだけを考慮します。)

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

tsquery <@ tsqueryboolean

最初のtsqueryは2番目に含まれているか? (これは結合演算子を無視して、単に一方の問い合わせ中のすべての語彙素が他方に現れるかどうかだけを考慮します。)

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

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


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

表9.42 テキスト検索関数

関数

説明

array_to_tsvector ( text[] ) → tsvector

語彙素の配列をtsvectorに変換します。 与えられた文字列は特に処理は施されずにそのまま利用されます。

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

get_current_ts_config ( ) → regconfig

default_text_search_configで設定された)現在のテキスト検索設定のOIDを返します。

get_current_ts_config()english

length ( tsvector ) → integer

tsvectorにある語彙素の数を返します。

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'

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

websearch_to_tsquery ( [ config regconfig, ] query text ) → tsquery

指定されたデフォルト設定にしたがって単語を正規化してテキストをtsqueryに変換します。 引用符で囲まれた一連の語は句の検査に変換されます。 orはOR演算子を生成するものとして扱われ、ダッシュはNOT演算子として扱われます。 それ以外の句読点は無視されます。 これにより通常のweb検索ツールに近い振る舞いをします。

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

querytree ( tsquery ) → text

tsqueryのインデックス付可能な部分の表現を生成します。 空あるいはTはインデックス付できる部分が無い問い合わせであることを意味します。

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

setweight ( vector tsvector, weight "char" ) → tsvector

vectorの各要素に指定した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[] ) → tsvector

vectorの各要素にlexemesで列挙したweightを割り当てます。

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

strip ( tsvector ) → tsvector

位置と重みをtsvectorから削除します。

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

to_tsquery ( [ config regconfig, ] query text ) → tsquery

指定されたデフォルト設定にしたがって単語を正規化してテキストを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 ) → tsvector

to_tsvector ( [ config regconfig, ] document jsonb ) → tsvector

指定されたデフォルト設定にしたがって正規化してJSON文書中の文字列値をtsvectorに変換します。 そして結果は文書中の順序にしたがって結合されます。 位置情報は、あたかも文字列値の各々の対の間にストップワードが存在するかのように生成されます。 (入力がjsonbの場合、JSONオブジェクトのフィールドのドキュメント順は実装依存であることに注意してください。例中の差異を見てください。)

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 ( [ config regconfig, ] document json, filter jsonb ) → tsvector

jsonb_to_tsvector ( [ config regconfig, ] document jsonb, filter jsonb ) → tsvector

filterによって要求された項目をJSON文書から検索し、指定されたデフォルト設定にしたがって正規化してtsvectorに変換します。 そして結果は文書中の順序にしたがって結合されます。 位置情報は、あたかも文字列値の各々の対の間にストップワードが存在するかのように生成されます。 (入力がjsonbの場合、JSONオブジェクトのフィールドのドキュメント順は実装依存であることに注意してください。例中の差異を見てください。) filterは0個以上の以下のキーワードを含むjsonbの配列でなければなりません: "string" (すべての文字列値を含めます)、"numeric" (すべての数値を含めます)、"boolean" (すべての論理値を含めます)、"key" (すべてのキーを含めます)、"all" (すべてを含めます)。 特別な場合として、filterはこれらのキーワードのどれかである単純なJSON値とすることもできます。

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 ( vector tsvector, lexeme text ) → tsvector

vectorから与えられたlexemeを削除します。

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

ts_delete ( vector tsvector, lexemes text[] ) → tsvector

vectorからlexemes中のすべての語彙素を削除します。

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

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

vectorからweightsを持つ要素だけを検索します。

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

ts_headline ( [ config regconfig, ] document text, query tsquery [, options text ] ) → text

document中のqueryにマッチするものを省略形で表示します。 documenttsvectorではなくて生のテキストでなければなりません。 問い合わせのマッチ処理を行う前に、指定した、あるいはデフォルトの設定にしたがって単語が正規化されます。 12.3.4にこの関数の使い方が記述されています。可能なoptionsについても言及されています。

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

ts_headline ( [ config regconfig, ] document json, query tsquery [, options text ] ) → text

ts_headline ( [ config regconfig, ] document jsonb, query tsquery [, options text ] ) → text

JSON document中に出現する文字列値にqueryがマッチしたものを省略形で表示します。 詳細は12.3.4を見てください。

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

ts_rank ( [ weights real[], ] vector tsvector, query tsquery [, normalization integer ] ) → real

vectorqueryにどれほどマッチするかのスコアを計算します。 詳細は12.3.3を見てください。

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

ts_rank_cd ( [ weights real[], ] vector tsvector, query tsquery [, normalization integer ] ) → real

被覆密度アルゴリズムを用いてvectorqueryにどれほどマッチするかのスコアを計算します。 詳細は12.3.3を見てください。

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

ts_rewrite ( query tsquery, target tsquery, substitute tsquery ) → tsquery

query中に出現するtargetsubstituteに置き換えます。 詳細は12.4.2.1を見てください。

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

ts_rewrite ( query tsquery, select text ) → tsquery

SELECTを実行して取得したターゲットと代替を使用してqueryの一部を置き換えます。 詳細は12.4.2.1を見てください。

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

tsquery_phrase ( query1 tsquery, query2 tsquery ) → tsquery

連続する語彙素でquery1query2のマッチを検索する語句問い合わせを作成します。 (<->演算子と同じです。)

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

tsquery_phrase ( query1 tsquery, query2 tsquery, distance integer ) → tsquery

語彙素が正確にdistanceだけ離れているquery1query2へのマッチを検索する語句問い合わせを作成します。

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}

unnest ( tsvector ) → setof record ( lexeme text, positions smallint[], weights text )

1行につき1語彙素でtsvectorを行の集合に変換します。

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}


注記

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

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

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

関数

説明

ts_debug ( [ config regconfig, ] document text ) → setof record ( alias text, description text, token text, dictionaries regdictionary[], dictionary regdictionary, lexemes text[] )

指定した、あるいはデフォルトの設定にしたがってdocumentから正規化されたトークンを取り出し、各トークンがどのように処理されたかの情報を返します。 詳細は12.8.1を見てください。

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

ts_lexize ( dict regdictionary, token text ) → text[]

入力トークンが辞書にあれば代替の語彙素の配列、辞書にあるがストップワードである場合には空の配列、未知の単語ならNULLを返します。 詳細は12.8.3を見てください。

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

ts_parse ( parser_name text, document text ) → setof record ( tokid integer, token text )

名前で指定したパーサを使ってdocumentからトークンを取り出します。 詳細は12.8.2を見てください。

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

ts_parse ( parser_oid oid, document text ) → setof record ( tokid integer, token text )

OIDで指定されたパーサを使ってdocumentからトークンを取り出します。 詳細は12.8.2を見てください。

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

ts_token_type ( parser_name text ) → setof record ( tokid integer, alias text, description text )

名前で指定したパーサが認識できるトークンの型を記述するテーブルを返します。 詳細は12.8.2を見てください。

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

ts_token_type ( parser_oid oid ) → setof record ( tokid integer, alias text, description text )

OIDで指定したパーサが認識できるトークンの型を記述するテーブルを返します。 詳細は12.8.2を見てください。

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

ts_stat ( sqlquery text [, weights text ] ) → setof record ( word text, ndoc integer, nentry integer )

単一のtsvector列を返さなければならないsqlqueryを実行し、データに含まれる別個の語彙素に関する統計情報を返します。 詳細は12.4.4をご覧ください。

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