自定义文本搜索配置的行为很容易变得令人困惑。本节介绍的函数对于测试文本搜索对象很有用。您可以测试一个完整的配置,也可以单独测试解析器和词典。
函数 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[] — 识别该词元的词典生成的词位(lexeme)列表,如果没有任何词典识别则为 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 word(别名 asciiword)。对于这种词元类型,词典列表是 english_ispell 和 english_stem。该单词被 english_ispell 识别,并将其缩减为名词 bright。单词 supernovaes 是 english_ispell 词典未知的,因此被传递给下一个词典,并且幸运的是,它被识别了(事实上,english_stem 是一个 Snowball 词典,它能识别所有词;这就是它被放在词典列表末尾的原因)。
单词 The 被 english_ispell 词典识别为停用词(第 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[]
ts_lexize 如果输入 token 是词典已知的,则返回一个词位数组;如果词典已知该词元但它是一个停用词,则返回一个空数组;如果它是一个未知词,则返回 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'
如果您在文档中看到任何不正确的内容、与您对特定功能的体验不符的内容,或者需要进一步说明的内容,请使用 此表格 报告文档问题。