バージョンごとのドキュメント一覧

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

<title>Text Search Functions and Operators</title>

<xref linkend="textsearch-operators-table"/>, <xref linkend="textsearch-functions-table"/> and <xref linkend="textsearch-functions-debug-table"/> summarize the functions and operators that are provided for full text searching. See <xref linkend="textsearch"/> for a detailed explanation of <productname>PostgreSQL</productname>'s text search facility. 表 9.42表 9.43および表 9.44は全文検索用に提供されている関数と演算子を要約しています。PostgreSQLのテキスト検索機能の詳細は第12章を参照してください。

表9.42 テキスト検索演算子

<title>Text Search Operators</title>

Operator 演算子

Description 説明

Example(s)

tsvector @@ tsqueryboolean

tsquery @@ tsvectorboolean

Does <type>tsvector</type> match <type>tsquery</type>? (The arguments can be given in either order.) tsvectortsqueryの条件に合うか? (引数は任意の順で与えることができます。)

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

text @@ tsqueryboolean

Does text string, after implicit invocation of <function>to_tsvector()</function>, match <type>tsquery</type>? テキスト文字列はto_tsvector()の暗黙的な呼び出し後にtsqueryの条件に合うか?

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

tsvector @@@ tsqueryboolean

tsquery @@@ tsvectorboolean

This is a deprecated synonym for <literal>@@</literal>. @@に対する廃止予定の同義語です。

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

tsvector || tsvectortsvector

Concatenates two <type>tsvector</type>s. If both inputs contain lexeme positions, the second input's positions are adjusted accordingly. 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

ANDs two <type>tsquery</type>s together, producing a query that matches documents that match both input queries. 2つのtsqueryの論理積を取り、両方の入力問い合わせにマッチする文書にマッチする問い合わせを生成します。

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

tsquery || tsquerytsquery

ORs two <type>tsquery</type>s together, producing a query that matches documents that match either input query. 2つのtsqueryの論理和を取り、どちらかの入力問い合わせにマッチする文書にマッチする問い合わせを生成します。

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

!! tsquerytsquery

Negates a <type>tsquery</type>, producing a query that matches documents that do not match the input query. tsqueryの否定を取り、入力問い合わせにマッチしない文書にマッチする問い合わせを生成します。

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

tsquery <-> tsquerytsquery

Constructs a phrase query, which matches if the two input queries match at successive lexemes. 2つの入力問い合わせが連続する語彙素にマッチする場合にマッチする語句問い合わせを作成します。

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

tsquery @> tsqueryboolean

Does first <type>tsquery</type> contain the second? (This considers only whether all the lexemes appearing in one query appear in the other, ignoring the combining operators.) 最初のtsqueryは2番目を含んでいるか? (これは結合演算子を無視して、単に一方の問い合わせ中のすべての語彙素が他方に現れるかどうかだけを考慮します。)

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

tsquery <@ tsqueryboolean

Is first <type>tsquery</type> contained in the second? (This considers only whether all the lexemes appearing in one query appear in the other, ignoring the combining operators.) 最初のtsqueryは2番目に含まれているか? (これは結合演算子を無視して、単に一方の問い合わせ中のすべての語彙素が他方に現れるかどうかだけを考慮します。)

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

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


In addition to these specialized operators, the usual comparison operators shown in <xref linkend="functions-comparison-op-table"/> are available for types <type>tsvector</type> and <type>tsquery</type>. These are not very useful for text searching but allow, for example, unique indexes to be built on columns of these types. 表に示された演算子に加え、表 9.1で示す通常の比較演算子型tsvectorおよびtsqueryに対して利用できます。 これらはテキスト検索に対してそれほど有用ではありませんが、例えばこれらの型の列に一意インデックスを作成することを可能にします。

表9.43 テキスト検索関数

<title>Text Search Functions</title>

Function 関数

Description 説明

Example(s)

array_to_tsvector ( text[] ) → tsvector

Converts an array of text strings to a <type>tsvector</type>. The given strings are used as lexemes as-is, without further processing. Array elements must not be empty strings or <literal>NULL</literal>. 文字列の配列をtsvectorに変換します。 与えられた文字列はそれ以上処理せずに語彙素としてそのまま使用されます。 配列要素は空文字列やNULLであってはなりません。

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

get_current_ts_config ( ) → regconfig

Returns the OID of the current default text search configuration (as set by <xref linkend="guc-default-text-search-config"/>). default_text_search_configで設定された)現在のテキスト検索設定のOIDを返します。

get_current_ts_config()english

length ( tsvector ) → integer

Returns the number of lexemes in the <type>tsvector</type>. tsvectorにある語彙素の数を返します。

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

numnode ( tsquery ) → integer

Returns the number of lexemes plus operators in the <type>tsquery</type>. tsqueryにある語彙素の数と演算子の数の和を返します。

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

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

Converts text to a <type>tsquery</type>, normalizing words according to the specified or default configuration. Any punctuation in the string is ignored (it does not determine query operators). The resulting query matches documents containing all non-stopwords in the text. 指定されたデフォルト設定にしたがって単語を正規化してテキストをtsqueryに変換します。 文字列中の句読点はすべて無視されます。(句読点は問い合わせ演算子を決定しません。) 結果の問い合わせはテキスト中の非ストップワードをすべて含む文書にマッチします。

plainto_tsquery('english', 'The Fat Rats')'fat' & 'rat'

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

Converts text to a <type>tsquery</type>, normalizing words according to the specified or default configuration. Any punctuation in the string is ignored (it does not determine query operators). The resulting query matches phrases containing all non-stopwords in the text. 指定されたデフォルト設定にしたがって単語を正規化してテキストを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

Converts text to a <type>tsquery</type>, normalizing words according to the specified or default configuration. Quoted word sequences are converted to phrase tests. The word <quote>or</quote> is understood as producing an OR operator, and a dash produces a NOT operator; other punctuation is ignored. This approximates the behavior of some common web search tools. 指定されたデフォルト設定にしたがって単語を正規化してテキストをtsqueryに変換します。 引用符で囲まれた一連の語は句の検査に変換されます。 orはOR演算子を生成するものとして扱われ、ダッシュはNOT演算子として扱われます。 それ以外の句読点は無視されます。 これにより通常のweb検索ツールに近い振る舞いをします。

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

querytree ( tsquery ) → text

Produces a representation of the indexable portion of a <type>tsquery</type>. A result that is empty or just <literal>T</literal> indicates a non-indexable query. tsqueryのインデックス付可能な部分の表現を生成します。 空あるいはTはインデックス付できる部分が無い問い合わせであることを意味します。

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

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

Assigns the specified <parameter>weight</parameter> to each element of the <parameter>vector</parameter>. 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

Assigns the specified <parameter>weight</parameter> to elements of the <parameter>vector</parameter> that are listed in <parameter>lexemes</parameter>. The strings in <parameter>lexemes</parameter> are taken as lexemes as-is, without further processing. Strings that do not match any lexeme in <parameter>vector</parameter> are ignored. 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

Removes positions and weights from the <type>tsvector</type>. 位置と重みをtsvectorから削除します。

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

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

Converts text to a <type>tsquery</type>, normalizing words according to the specified or default configuration. The words must be combined by valid <type>tsquery</type> operators. 指定されたデフォルト設定にしたがって単語を正規化してテキストをtsqueryに変換します。 単語は有効なtsquery演算子と組み合わされていなければなりません。

to_tsquery('english', 'The & Fat & Rats')'fat' & 'rat'

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

Converts text to a <type>tsvector</type>, normalizing words according to the specified or default configuration. Position information is included in the result. 指定されたデフォルト設定にしたがって単語を正規化してテキストを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

Converts each string value in the JSON document to a <type>tsvector</type>, normalizing words according to the specified or default configuration. The results are then concatenated in document order to produce the output. Position information is generated as though one stopword exists between each pair of string values. (Beware that <quote>document order</quote> of the fields of a JSON object is implementation-dependent when the input is <type>jsonb</type>; observe the difference in the examples.) 指定されたデフォルト設定にしたがって正規化して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

Selects each item in the JSON document that is requested by the <parameter>filter</parameter> and converts each one to a <type>tsvector</type>, normalizing words according to the specified or default configuration. The results are then concatenated in document order to produce the output. Position information is generated as though one stopword exists between each pair of selected items. (Beware that <quote>document order</quote> of the fields of a JSON object is implementation-dependent when the input is <type>jsonb</type>.) The <parameter>filter</parameter> must be a <type>jsonb</type> array containing zero or more of these keywords: <literal>"string"</literal> (to include all string values), <literal>"numeric"</literal> (to include all numeric values), <literal>"boolean"</literal> (to include all boolean values), <literal>"key"</literal> (to include all keys), or <literal>"all"</literal> (to include all the above). As a special case, the <parameter>filter</parameter> can also be a simple JSON value that is one of these keywords. 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

Removes any occurrence of the given <parameter>lexeme</parameter> from the <parameter>vector</parameter>. The <parameter>lexeme</parameter> string is treated as a lexeme as-is, without further processing. vectorから与えられたlexemeを削除します。 lexeme文字列は、それ以上の処理を行わずに、現状のままのlexemeとして扱われます。

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

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

Removes any occurrences of the lexemes in <parameter>lexemes</parameter> from the <parameter>vector</parameter>. The strings in <parameter>lexemes</parameter> are taken as lexemes as-is, without further processing. Strings that do not match any lexeme in <parameter>vector</parameter> are ignored. vectorからlexemes中のすべての語彙素を削除します。 lexemes内の文字列は、それ以上の処理を行わずに、そのまま語彙素として扱われます。 vector内のどの語彙素とも一致しない文字列は無視されます。

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

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

Selects only elements with the given <parameter>weights</parameter> from the <parameter>vector</parameter>. 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

Displays, in an abbreviated form, the match(es) for the <parameter>query</parameter> in the <parameter>document</parameter>, which must be raw text not a <type>tsvector</type>. Words in the document are normalized according to the specified or default configuration before matching to the query. Use of this function is discussed in <xref linkend="textsearch-headline"/>, which also describes the available <parameter>options</parameter>. 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

Displays, in an abbreviated form, match(es) for the <parameter>query</parameter> that occur in string values within the JSON <parameter>document</parameter>. See <xref linkend="textsearch-headline"/> for more details. 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

Computes a score showing how well the <parameter>vector</parameter> matches the <parameter>query</parameter>. See <xref linkend="textsearch-ranking"/> for details. 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

Computes a score showing how well the <parameter>vector</parameter> matches the <parameter>query</parameter>, using a cover density algorithm. See <xref linkend="textsearch-ranking"/> for details. 被覆密度アルゴリズムを用いて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

Replaces occurrences of <parameter>target</parameter> with <parameter>substitute</parameter> within the <parameter>query</parameter>. See <xref linkend="textsearch-query-rewriting"/> for details. 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

Replaces portions of the <parameter>query</parameter> according to target(s) and substitute(s) obtained by executing a <command>SELECT</command> command. See <xref linkend="textsearch-query-rewriting"/> for details. 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

Constructs a phrase query that searches for matches of <parameter>query1</parameter> and <parameter>query2</parameter> at successive lexemes (same as <literal>&lt;-&gt;</literal> operator). 連続する語彙素でquery1query2のマッチを検索する語句問い合わせを作成します。 (<->演算子と同じです。)

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

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

Constructs a phrase query that searches for matches of <parameter>query1</parameter> and <parameter>query2</parameter> that occur exactly <parameter>distance</parameter> lexemes apart. 語彙素が正確にdistanceだけ離れているquery1query2へのマッチを検索する語句問い合わせを作成します。

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

tsvector_to_array ( tsvector ) → text[]

Converts a <type>tsvector</type> to an array of lexemes. 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 )

Expands a <type>tsvector</type> into a set of rows, one per lexeme. 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}


注記

All the text search functions that accept an optional <type>regconfig</type> argument will use the configuration specified by <xref linkend="guc-default-text-search-config"/> when that argument is omitted. オプションのregconfig引数を受け付ける全てのテキスト検索関数は、その引数が省略された場合default_text_search_configで指定された設定を使用します。

The functions in <xref linkend="textsearch-functions-debug-table"/> are listed separately because they are not usually used in everyday text searching operations. They are primarily helpful for development and debugging of new text search configurations. 表 9.44の関数は、日常のテキスト検索操作では通常使用されないので、別の表にしました。 これらは主に新しいテキスト検索設定の開発およびデバッグに役立ちます。

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

<title>Text Search Debugging Functions</title>

Function 関数

Description 説明

Example(s)

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

Extracts and normalizes tokens from the <parameter>document</parameter> according to the specified or default text search configuration, and returns information about how each token was processed. See <xref linkend="textsearch-configuration-testing"/> for details. 指定した、あるいはデフォルトの設定にしたがって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[]

Returns an array of replacement lexemes if the input token is known to the dictionary, or an empty array if the token is known to the dictionary but it is a stop word, or NULL if it is not a known word. See <xref linkend="textsearch-dictionary-testing"/> for details. 入力トークンが辞書にあれば代替の語彙素の配列、辞書にあるがストップワードである場合には空の配列、未知の単語ならNULLを返します。 詳細は12.8.3を見てください。

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

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

Extracts tokens from the <parameter>document</parameter> using the named parser. See <xref linkend="textsearch-parser-testing"/> for details. 名前で指定したパーサを使ってdocumentからトークンを取り出します。 詳細は12.8.2を見てください。

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

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

Extracts tokens from the <parameter>document</parameter> using a parser specified by OID. See <xref linkend="textsearch-parser-testing"/> for details. 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 )

Returns a table that describes each type of token the named parser can recognize. See <xref linkend="textsearch-parser-testing"/> for details. 名前で指定したパーサが認識できるトークンの型を記述するテーブルを返します。 詳細は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 )

Returns a table that describes each type of token a parser specified by OID can recognize. See <xref linkend="textsearch-parser-testing"/> for details. 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 )

Executes the <parameter>sqlquery</parameter>, which must return a single <type>tsvector</type> column, and returns statistics about each distinct lexeme contained in the data. See <xref linkend="textsearch-statistics"/> for details. 単一のtsvector列を返さなければならないsqlqueryを実行し、データに含まれる別個の語彙素に関する統計情報を返します。 詳細は12.4.4をご覧ください。

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