一个自定义文本搜索配置的行为很容易变得混乱。本节中描述的函数对于测试文本搜索对象有用。你可以测试一个完整的配置,或者独立测试解析器和词典。
函数ts_debug允许简单地测试一个文本搜索配置。
ts_debug([configregconfig, ]documenttext, OUTaliastext, OUTdescriptiontext, OUTtokentext, OUTdictionariesregdictionary[], OUTdictionaryregdictionary, OUTlexemestext[]) returns setof record
ts_debug显示document的每一个词元的信息,词元由解析器产生并由配置的词典处理过。该函数使用由config指定的配置,如果该参数被忽略则使用default_text_search_config指定的配置。
ts_debug为解析器在文本中标识的每一个词元返回一行。被返回的列是:
alias text — 词元类型的短名称
description text — 词元类型的描述
token text — 词元的文本
dictionaries regdictionary[] — 配置为这种词元类型选择的词典
dictionary regdictionary — 识别该词元的词典,如果没有词典能识别则为NULL
lexemes text[] — 识别该词元的词典产生的词位,如果没有词典能识别则为NULL;一个空数组({})表示该词元被识别为一个停用词
以下是一个简单的示例:
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}
为了一个更广泛的示范,我们先为英语语言创建一个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}
在这个示例中,词Brightest被解析器识别为一个ASCII 词(别名asciiword)。对于这种词元类型,词典列表是english_ispell和english_stem。该词被english_ispell识别,并被这个词典归约为词位bright。词supernovaes对english_ispell词典来说是未知的,因此它会被传递给下一个词典;幸运的是,它随后被识别了。(实际上,english_stem是一个 Snowball 词典,它能够识别所有输入;这也是为什么它被放在词典列表末尾。)
词The被english_ispell词典识别为一个停用词(Section 12.6.1)并且将不会被索引。空格也被丢弃,因为该配置没有为它们提供词典。
你可以通过明确指定要查看的列来减少输出宽度:
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}
下列函数允许直接测试一个文本搜索解析器。
ts_parse(parser_nametext,documenttext, OUTtokidinteger, OUTtokentext) returnssetof recordts_parse(parser_oidoid,documenttext, OUTtokidinteger, OUTtokentext) returnssetof record
ts_parse解析给定的document,并返回一组记录,每个由解析过程产生的词元对应一条记录。每条记录都包含一个 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
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
ts_lexize函数帮助词典测试。
ts_lexize(dictregdictionary,tokentext) returnstext[]
如果输入的token是该词典已知的,则ts_lexize返回一个词位数组;如果词元是词典已知的但是它是一个停用词,则返回一个空数组;或者如果它对词典是未知词,则返回NULL。
示例:
SELECT ts_lexize('english_stem', 'stars');
ts_lexize
-----------
{star}
SELECT ts_lexize('english_stem', 'a');
ts_lexize
-----------
{}
ts_lexize函数期望的是单个词元,而不是文本。下面这种情况可能会让人困惑:
SELECT ts_lexize('thesaurus_astro', 'supernovae stars') is null;
?column?
----------
t
分类词典 thesaurus_astro 确实知道短语 supernovae stars,但 ts_lexize 会失败,因为它不会解析输入文本,而是把它当作一个单独的词元。测试分类词典时应使用 plainto_tsquery 或 to_tsvector,例如:
SELECT plainto_tsquery('supernovae stars');
plainto_tsquery
-----------------
'sn'
如果您发现文档中有不正确的内容、与您使用特定功能的经验不符或需要进一步说明,请使用此表单来报告文档问题。