测试和调试文本搜索
本主题介绍可用于测试和调试搜索配置或配置中指定的单个解析器和词典的Greenplum数据库函数。
自定义文本搜索配置的行为很容易变得混乱。 本节中描述的功能对于测试文本搜索对象非常有用。 您可以单独测试完整配置或测试解析器和词典。
本节包含以下子主题:* 配置测试
配置测试
函数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
显示有关解析器生成并由配置的字典处理的每个文档token的信息。
它使用config
指定的配置,如果省略该参数,则使用default_text_search_config
。
ts_debug
为解析器在文本中标识的每个token返回一行。
返回的列是
alias text
— token类型的简称description text
— token类型的描述token text
— token文本dictionaries regdictionary[]
— 由此token类型的配置选择的词典dictionary regdictionary
— 识别token的字典,如果没有,则为NULL
lexemes text[]
— 识别token的字典产生的词位,如果没有,则为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
)。
对于此token类型,字典列表是english_ispell
和english_stem
。
这个词被english_ispell
识别,将其缩小为名词bright
。
english_ispell
词典中不知道supernovaes
这个词,所以它被传递到下一个词典,
幸运的是,它被识别出来(事实上,english_stem
是一个可以识别所有内容的Snowball词典;这就是为什么它放在词典列表的末尾)。
单词The
被english_ispell
字典识别为停止词(停止词),不会被编入索引。
这些空间也被丢弃,因为配置根本不为它们提供字典。
您可以通过显式指定要查看的列来减小输出的宽度:
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_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
解析给定文档并返回一系列记录,每个记录用于解析生成的每个token。
每条记录都包含一个显示已分配token类型的tokid
和一个token
,是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
返回一个表,该表描述指定解析器可识别的每种类型的token。
对于每个token类型,该表给出了解析器用于标记该token类型的整数tokid
,
在配置命令中命名token类型的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(dictreg dictionary, 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
函数需要单个token,而不是文本。
这是一个令人困惑的案例:
SELECT ts_lexize('thesaurus_astro','supernovae stars') is null;
?column?
----------
t
同义词词典thesaurus_astro
确实知道短语supernovae stars
,但ts_lexize
失败,因为它不解析输入文本但将其视为单个token。
使用plainto_tsquery
或to_tsvector
来测试同义词词典,例如:
SELECT plainto_tsquery('supernovae stars');
plainto_tsquery
-----------------
'sn'
Parent topic: 使用全文搜索