すべてのプロダクト
Search
ドキュメントセンター

PolarDB:テキスト検索関数と演算子

最終更新日:May 31, 2024

このトピックでは、テキスト検索関数と演算子PolarDB for PostgreSQL (Compatible with Oracle) について説明します。

以下の表に、フルテキスト検索用の関数と演算子の概要を示します。

テキスト検索演算子

tsvector @ tsqueryブール

tsquery @ tsvectorブール

tsvectortsqueryと一致しますか? (引数はどちらの順序でも指定できます。)

to_tsvector (「太った猫はラットを食べた」) @ @ to_tsquery (「猫とラット」)t

text @ @ tsqueryブール値

to_tsvector() の暗黙的な呼び出しの後、テキスト文字列はtsqueryと一致しますか?

「太った猫はネズミを食べた」 @ @ to_tsquery('cat & rat')t

tsvector @ @ @ tsquery → ブール値

tsquery @ @ @ tsvector → ブール値

これは、@ @ の非推奨のシノニムです。

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をまとめてANDし、両方の入力クエリに一致するドキュメントに一致するクエリを生成します。

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

tsquery | | tsquerytsquery

2つのtsqueryをORして、どちらかの入力クエリに一致するドキュメントに一致するクエリを生成します。

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

!!tsquerytsquery

tsqueryを否定し、入力クエリに一致しないドキュメントに一致するクエリを生成します。

!! 'cat'::tsquery! 「猫」

tsquery <-> tsquerytsquery

2つの入力クエリが連続する語彙素で一致する場合に一致するフレーズクエリを構築します。

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

tsquery @> tsqueryブール

最初のtsqueryには2番目のものが含まれていますか? (これは、結合演算子を無視して、一方のクエリに表示されるすべての語彙素が他方に表示されるかどうかのみを考慮します。)

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

tsquery <@ tsqueryブール

最初のtsqueryは2番目に含まれていますか? (これは、結合演算子を無視して、一方のクエリに表示されるすべての語彙素が他方に表示されるかどうかのみを考慮します。)

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

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

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

テキスト検索機能

array_to_tsvector (text[]) → tsvector

語彙の配列をtsvectorに変換します。 所与の文字列は、さらなる処理を行わずにそのまま使用される。

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

get_current_ts_config ( ) → regconfig

現在のデフォルトのテキスト検索設定のOIDを返します (default_text_search_configで設定) 。

get_current_ts_config()英語

length (tsvector) → integer

tsvector内の語彙の数を返します。

长さ ('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" またはcat dog')'fat' <-> 'rat' | 'cat' & 'dog'

querytree (tsquery) → text

tsqueryのインデックス可能な部分の表現を生成します。 空またはTだけの結果は、インデックス不可能なクエリを示します。

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

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

指定した重みvectorの各要素に割り当てます。

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

seitweight (vector tsvectorweight "char"lexemes text[]) → tsvector

指定されたウェイトを、語彙素にリストされたベクトルの要素に割り当てます。

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

ストリップ (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に変換し、指定された設定またはデフォルトの設定に従って単語を正規化します。 次に、結果をドキュメント順に連結して出力を生成します。 位置情報は、ストリング値の各対の間に1つのストップワードが存在するかのように生成される。 (入力が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に変換します。 次に、結果をドキュメント順に連結して出力を生成します。 選択されたアイテムの各ペア間に1つのストップワードが存在するかのように位置情報が生成される。 (入力がjsonbの場合、JSONオブジェクトのフィールドの「ドキュメント順序」は実装に依存することに注意してください。) フィルターは、"string" (すべての文字列値を含める) 、"numeric" (すべての数値を含める) 、"boolean" (すべてのブール値を含める) 、"key" (すべてのキーを含める) 、または "all" (上記のすべてを含む) 。 特殊なケースとして、フィルターはこれらのキーワードの1つである単純な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

指定された語彙素の出現をベクトルから削除します。

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

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

ベクトルから語彙素内の語彙素の出現を削除します。

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

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

ベクトルから指定された重みを持つ要素のみを選択します。

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内のクエリの一致を省略形式で表示します。これはtsvectorではなく生のテキストでなければなりません。 ドキュメント内の単語は、クエリに一致する前に、指定された構成またはデフォルトの構成に従って正規化されます。

ts_headline('The fat cat atate the rat.'、'cat')The fat <b>cat</b> tate 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ドキュメント内の文字列値で発生するクエリの一致を省略形式で表示します。

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

ベクトルクエリにどれだけ一致するかを示すスコアを計算します。

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

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

カバー密度アルゴリズムを使用して、ベクトルクエリにどれだけよく一致するかを示すスコアを計算します。

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

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

targetの出現をクエリ内の代替に置き換えます。

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

ts_rewrite (query tsquery, select text) → tsquery

SELECTコマンドを実行して取得したターゲットと代替物に従って、クエリの一部を置き換えます。

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

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)

tsvectorを行のセットに展開します。

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

lexeme | 位置 | 重み
------- ----------- ----------------
 cat | {3} | {D}
 fat | {2,4} | {D、D}
 rat | {5} | {A} 
重要

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

次の表の関数は、通常、日常のテキスト検索操作では使用されないため、個別にリストされています。 これらは主に、新しいテキスト検索構成の開発とデバッグに役立ちます。

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

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

指定されたテキスト検索設定または既定のテキスト検索設定に従って、ドキュメントからトークンを抽出および正規化し、各トークンの処理方法に関する情報を返します。

ts_debug('english' 、'The Brightest supernove')(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) → setof record (tokid integer, token text)

名前付きパーサーを使用してドキュメントからトークンを抽出します。

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

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

OIDで指定されたパーサーを使用してドキュメントからトークンを抽出します。

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

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

名前付きパーサーが認識できるトークンの各タイプを記述するテーブルを返します。

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

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

OIDで指定されたパーサーが認識できるトークンの各タイプを記述するテーブルを返します。

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

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

sqlqueryを実行します。これは、単一のtsvector列を返す必要があり、データに含まれる個別の語彙に関する統計を返します。

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