On this page
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
-识别令牌的字典,如果没有识别符,则返回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 word
(别名asciiword
)。对于此令牌类型,字典列表为english_ispell
和english_stem
。该单词被english_ispell
识别,从而将其简化为名词bright
。 english_ispell
字典中不知道单词supernovaes
,因此将其传递到下一个字典,而且很幸运,它被识别了(实际上english_stem
是可识别所有内容的 Snowball 字典;这就是为什么将其放在字典列表末尾的原因) )。
english_ispell
字典将单词The
识别为停用词(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}
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[]
如果 Importing* token
*对于字典是已知的,则ts_lexize
返回一个词素数组;如果字典对令牌是已知的但它是一个停用词,则返回一个空数组;如果它是一个停用词,则返回NULL
。
Examples:
SELECT ts_lexize('english_stem', 'stars');
ts_lexize
-----------
{star}
SELECT ts_lexize('english_stem', 'a');
ts_lexize
-----------
{}
Note
ts_lexize
函数需要一个* token *,而不是文本。在这种情况下,这可能会造成混淆:
SELECT ts_lexize('thesaurus_astro','supernovae stars') is null;
?column?
----------
t
同义词词典thesaurus_astro
确实知道短语supernovae stars
,但是ts_lexize
失败,因为它不解析 Importing 文本,而是将其视为单个标记。使用plainto_tsquery
或to_tsvector
测试同义词库字典,例如:
SELECT plainto_tsquery('supernovae stars');
plainto_tsquery
-----------------
'sn'