The behavior of a custom text search configuration can easily become confusing. The functions described in this section are useful for testing text search objects. You can test a complete configuration, or test parsers and dictionaries separately. カスタムテキスト検索設定の挙動は複雑になりがちで、結果として混乱を招くことになります。 この節では、テキスト検索オブジェクトのテストの際に役に立つ関数を説明します。 完全な設定でテストすることも、パーサと辞書を別々にテストすることも可能です。
The function <function>ts_debug</function> allows easy testing of a
text search configuration.
ts_debug関数により、テキスト検索設定の容易なテストができます。
ts_debug([configregconfig, ]documenttext, OUTaliastext, OUTdescriptiontext, OUTtokentext, OUTdictionariesregdictionary[], OUTdictionaryregdictionary, OUTlexemestext[]) returns setof record
<function>ts_debug</function> displays information about every token of
<replaceable class="parameter">document</replaceable> as produced by the
parser and processed by the configured dictionaries. It uses the
configuration specified by <replaceable
class="parameter">config</replaceable>,
or <varname>default_text_search_config</varname> if that argument is
omitted.
ts_debugは、パーサが生成し、設定された辞書が処理したdocumentのすべてのトークンの情報を表示します。その際、configで指定した設定が使われます。引数が省略されるとdefault_text_search_configが使われます。
<function>ts_debug</function> returns one row for each token identified in the text
by the parser. The columns returned are
ts_debugは、パーサが認識したテキスト中のトークンを1行につき一つ返します。
返却される列は以下です。
<replaceable>alias</replaceable> <type>text</type> — short name of the token type
alias text — トークン型の短縮名
<replaceable>description</replaceable> <type>text</type> — description of the
token type
description text — トークン型の説明
<replaceable>token</replaceable> <type>text</type> — text of the token
token text — トークンテキスト
<replaceable>dictionaries</replaceable> <type>regdictionary[]</type> — the
dictionaries selected by the configuration for this token type
dictionaries regdictionary[] — 設定によってこのトークン型用に選択された辞書
<replaceable>dictionary</replaceable> <type>regdictionary</type> — the dictionary
that recognized the token, or <literal>NULL</literal> if none did
dictionary regdictionary — トークンを認識した辞書。もし認識した辞書がなければ NULL
<replaceable>lexemes</replaceable> <type>text[]</type> — the lexeme(s) produced
by the dictionary that recognized the token, or <literal>NULL</literal> if
none did; an empty array (<literal>{}</literal>) means it was recognized as a
stop word
lexemes text[] — トークンを認識した辞書が生成した語彙素。もしどの辞書も認識しなければNULL。空の配列({})が返った場合は、ストップワードとして認識されたことを示す
Here is a simple example: 簡単な例を示します。
SELECT * FROM ts_debug('english', 'a fat cat sat on a mat - it ate a fat rats');
alias | description | token | dictionaries | dictionary | lexemes
-----------+-----------------+-------+----------------+--------------+---------
asciiword | Word, all ASCII | a | {english_stem} | english_stem | {}
blank | Space symbols | | {} | |
asciiword | Word, all ASCII | fat | {english_stem} | english_stem | {fat}
blank | Space symbols | | {} | |
asciiword | Word, all ASCII | cat | {english_stem} | english_stem | {cat}
blank | Space symbols | | {} | |
asciiword | Word, all ASCII | sat | {english_stem} | english_stem | {sat}
blank | Space symbols | | {} | |
asciiword | Word, all ASCII | on | {english_stem} | english_stem | {}
blank | Space symbols | | {} | |
asciiword | Word, all ASCII | a | {english_stem} | english_stem | {}
blank | Space symbols | | {} | |
asciiword | Word, all ASCII | mat | {english_stem} | english_stem | {mat}
blank | Space symbols | | {} | |
blank | Space symbols | - | {} | |
asciiword | Word, all ASCII | it | {english_stem} | english_stem | {}
blank | Space symbols | | {} | |
asciiword | Word, all ASCII | ate | {english_stem} | english_stem | {ate}
blank | Space symbols | | {} | |
asciiword | Word, all ASCII | a | {english_stem} | english_stem | {}
blank | Space symbols | | {} | |
asciiword | Word, all ASCII | fat | {english_stem} | english_stem | {fat}
blank | Space symbols | | {} | |
asciiword | Word, all ASCII | rats | {english_stem} | english_stem | {rat}
For a more extensive demonstration, we
first create a <literal>public.english</literal> configuration and
Ispell dictionary for the English language:
もう少し高度なデモをお見せするために、まず英語用のpublic.english設定と、Ispell辞書を作ります。
CREATE TEXT SEARCH CONFIGURATION public.english ( COPY = pg_catalog.english );
CREATE TEXT SEARCH DICTIONARY english_ispell (
TEMPLATE = ispell,
DictFile = english,
AffFile = english,
StopWords = english
);
ALTER TEXT SEARCH CONFIGURATION public.english
ALTER MAPPING FOR asciiword WITH english_ispell, english_stem;
SELECT * FROM ts_debug('public.english', 'The Brightest supernovaes');
alias | description | token | dictionaries | dictionary | lexemes
-----------+-----------------+-------------+-------------------------------+----------------+-------------
asciiword | Word, all ASCII | The | {english_ispell,english_stem} | english_ispell | {}
blank | Space symbols | | {} | |
asciiword | Word, all ASCII | Brightest | {english_ispell,english_stem} | english_ispell | {bright}
blank | Space symbols | | {} | |
asciiword | Word, all ASCII | supernovaes | {english_ispell,english_stem} | english_stem | {supernova}
In this example, the word <literal>Brightest</literal> was recognized by the
parser as an <literal>ASCII word</literal> (alias <literal>asciiword</literal>).
For this token type the dictionary list is
<literal>english_ispell</literal> and
<literal>english_stem</literal>. The word was recognized by
<literal>english_ispell</literal>, which reduced it to the noun
<literal>bright</literal>. The word <literal>supernovaes</literal> is
unknown to the <literal>english_ispell</literal> dictionary so it
was passed to the next dictionary, and, fortunately, was recognized (in
fact, <literal>english_stem</literal> is a Snowball dictionary which
recognizes everything; that is why it was placed at the end of the
dictionary list).
この例では、単語Brightestは、ASCII word (別名はasciiword)として認識されています。
このトークン型のための辞書リストはenglish_ispell とenglish_stemです。この単語はenglish_ispellに認識され、名詞brightへと縮退されています。
単語supernovaesはenglish_ispell辞書には認識されず、次の辞書に渡され、幸い認識されました(実際には、english_stemはSnowball辞書で、何でも認識します。それで、この辞書は辞書リストの最後に置かれているわけです)。
The word <literal>The</literal> was recognized by the
<literal>english_ispell</literal> dictionary as a stop word (<xref
linkend="textsearch-stopwords"/>) and will not be indexed.
The spaces are discarded too, since the configuration provides no
dictionaries at all for them.
単語Theは、english_ispell辞書によってストップワード(12.6.1)として認識されており、インデックス付けされません。空白も捨てられます。この設定では空白に関する辞書が提供されていないからです。
You can reduce the width of the output by explicitly specifying which columns you want to see: 明示的に見たい列を指定することにより、出力の幅を減らすことができます。
SELECT alias, token, dictionary, lexemes
FROM ts_debug('public.english', 'The Brightest supernovaes');
alias | token | dictionary | lexemes
-----------+-------------+----------------+-------------
asciiword | The | english_ispell | {}
blank | | |
asciiword | Brightest | english_ispell | {bright}
blank | | |
asciiword | supernovaes | english_stem | {supernova}
The following functions allow direct testing of a text search parser. 次にあげた関数により、テキスト検索パーサを直接テストすることができます。
ts_parse(parser_nametext,documenttext, OUTtokidinteger, OUTtokentext) returnssetof recordts_parse(parser_oidoid,documenttext, OUTtokidinteger, OUTtokentext) returnssetof record
<function>ts_parse</function> parses the given <replaceable>document</replaceable>
and returns a series of records, one for each token produced by
parsing. Each record includes a <varname>tokid</varname> showing the
assigned token type and a <varname>token</varname> which is the text of the
token. For example:
ts_parseは与えられたdocumentをパースし、パーサが生成したトークンを1行に1個もつ一連のレコードを返します。それぞれのレコードには、割り当てられたトークン型を示すtokidと、テキストのトークンであるtokenが含まれます。
例を示します。
SELECT * FROM ts_parse('default', '123 - a number');
tokid | token
-------+--------
22 | 123
12 |
12 | -
1 | a
12 |
1 | number
ts_token_type(parser_nametext, OUTtokidinteger, OUTaliastext, OUTdescriptiontext) returnssetof recordts_token_type(parser_oidoid, OUTtokidinteger, OUTaliastext, OUTdescriptiontext) returnssetof record
<function>ts_token_type</function> returns a table which describes each type of
token the specified parser can recognize. For each token type, the table
gives the integer <varname>tokid</varname> that the parser uses to label a
token of that type, the <varname>alias</varname> that names the token type
in configuration commands, and a short <varname>description</varname>. For
example:
ts_token_typeは、指定したパーサが認識できるトークン型を記述したテーブルを返します。各々のトークン型に対し、パーサがトークン型をラベル付けするのに使用する整数tokid、設定コマンド中のトークンの名前であるalias、簡単な説明であるdescriptionが含まれます。
例を示します。
SELECT * FROM ts_token_type('default');
tokid | alias | description
-------+-----------------+------------------------------------------
1 | asciiword | Word, all ASCII
2 | word | Word, all letters
3 | numword | Word, letters and digits
4 | email | Email address
5 | url | URL
6 | host | Host
7 | sfloat | Scientific notation
8 | version | Version number
9 | hword_numpart | Hyphenated word part, letters and digits
10 | hword_part | Hyphenated word part, all letters
11 | hword_asciipart | Hyphenated word part, all ASCII
12 | blank | Space symbols
13 | tag | XML tag
14 | protocol | Protocol head
15 | numhword | Hyphenated word, letters and digits
16 | asciihword | Hyphenated word, all ASCII
17 | hword | Hyphenated word, all letters
18 | url_path | URL path
19 | file | File or path name
20 | float | Decimal notation
21 | int | Signed integer
22 | uint | Unsigned integer
23 | entity | XML entity
The <function>ts_lexize</function> function facilitates dictionary testing.
ts_lexize関数は辞書のテストを支援します。
ts_lexize(dictregdictionary,tokentext) returnstext[]
<function>ts_lexize</function> returns an array of lexemes if the input
<replaceable>token</replaceable> is known to the dictionary,
or an empty array if the token
is known to the dictionary but it is a stop word, or
<literal>NULL</literal> if it is an unknown word.
ts_lexizeは、入力tokenが辞書に認識されれば語彙素の配列を返します。辞書に認識され、それがストップワードである場合には空の配列を返します。認識されなければNULLを返します。
Examples: 例:
SELECT ts_lexize('english_stem', 'stars');
ts_lexize
-----------
{star}
SELECT ts_lexize('english_stem', 'a');
ts_lexize
-----------
{}
The <function>ts_lexize</function> function expects a single
<emphasis>token</emphasis>, not text. Here is a case
where this can be confusing:
ts_lexize関数には、テキストではなく単一のトークンを与えます。これを間違えると次のようになります。
SELECT ts_lexize('thesaurus_astro', 'supernovae stars') is null;
?column?
----------
t
The thesaurus dictionary <literal>thesaurus_astro</literal> does know the
phrase <literal>supernovae stars</literal>, but <function>ts_lexize</function>
fails since it does not parse the input text but treats it as a single
token. Use <function>plainto_tsquery</function> or <function>to_tsvector</function> to
test thesaurus dictionaries, for example:
類語辞書thesaurus_astroは語句supernovae starsを認識しますが、ts_lexizeはしません。なぜなら、入力をテキストではなく、単一のトークンとして扱うからです。
類語辞書をテストするには、plainto_tsqueryまたはto_tsvectorを使ってください。例を示します。
SELECT plainto_tsquery('supernovae stars');
plainto_tsquery
-----------------
'sn'