ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
# 12.8\. 测试和调试文本搜索 一个自定义文本搜索配置的行为很容易变得混乱。在本节中描述的函数对测试文本搜索对象是有用的。 你可以测试一个完整的配置,或分别测试分析器和词典。 ## 12.8.1\. 配置测试 函数`ts_debug`允许简单测试文本搜索配置。 ``` ts_debug([ `_config_` `regconfig`, ] _document_ text, OUT _alias_ text, OUT _description_ text, OUT _token_ text, OUT _dictionaries_ regdictionary[], OUT _dictionary_ regdictionary, OUT _lexemes_ text[]) returns setof record ``` `ts_debug`显示关于通过解析器产生的和通过配置词典处理的`_document_`的每个标记的信息。 如果忽略参数,它使用通过`_config_`或者`default_text_search_config`指定的配置。 `ts_debug`返回通过文本解析器标识的每个标记的每一行,返回的列是: * `_alias_` `text` —标记类型的别名 * `_description_` `text` —标记类型描述 * `_token_` `text` —标记文本 * `_dictionaries_` `regdictionary[]` —通过配置为这个标记类型选定的词典 * `_dictionary_` `regdictionary` —词典公认的标记,如果不这样,则为`空`。 * `_lexemes_` `text[]` — 公认标记的词典产生的词(s),或者如果不做则为`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](#calibre_link-1118)),不会被索引。 空间也被丢弃,因为该配置根本没有为它们提供词典。 你可以通过明确指定你想要查看的列减少输出的宽度: ``` 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} ``` ## 12.8.2\. 解析器测试 下列函数允许直接测试文本搜索解析器。 ``` ts_parse(_parser_name_ text, _document_ text, OUT _tokid_ integer, OUT _token_ text) returns setof record ts_parse(_parser_oid_ oid, _document_ text, OUT _tokid_ integer, OUT _token_ text) returns setof 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_name_ text, OUT _tokid_ integer, OUT _alias_ text, OUT _description_ text) returns setof record ts_token_type(_parser_oid_ oid, OUT _tokid_ integer, OUT _alias_ text, OUT _description_ text) returns setof 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 ``` ## 12.8.3\. 词典测试 `ts_lexize`函数有易于进行词典测试。 ``` ts_lexize(_dict_ regdictionary, _token_ text) returns text[] ``` 如果输入`_token_`为词典已知的,那么`ts_lexize`返回词的数组,如果这个token对词典是已知的, 但它是一个屏蔽词,则返回空数组。如果它是一个未知的词则返回`NULL`。 比如: ``` SELECT ts_lexize('english_stem', 'stars'); ts_lexize ----------- {star} SELECT ts_lexize('english_stem', 'a'); ts_lexize ----------- {} ``` > **Note:** `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' > ```