跳到主要内容
版本:下一版

文本搜索解析器(Text Search Parsers)

本文介绍 Apache Cloudberry 文本搜索解析器如何将原始文本拆分为不同类型的词元(token)。

文本搜索解析器的作用是将原始文档文本拆解成词元,并标注每个词元的类型,这些类型由解析器定义。解析器本身不会修改文本内容,只是识别可能的词边界。由于功能范围有限,通常不需要像字典那样定制解析器。目前 Apache Cloudberry 仅提供一个内建解析器,已经能满足多种应用场景。

这个内建解析器名为 pg_catalog.default,它识别 23 种词元类型,如下表所示:

别名(Alias)描述(Description)示例(Example)
asciiword仅由 ASCII 字母组成的单词elephant
word任意语言字母组成的单词mañana
numword字母和数字组成的单词beta1
asciihword由 ASCII 字符组成的连字符单词up-to-date
hword任意字母组成的连字符单词lógico-matemática
numhword字母和数字组成的连字符单词postgresql-beta1
hword_asciipart连字符单词的 ASCII 部分postgresql(出现在 postgresql-beta1 中)
hword_part连字符单词的普通字母部分lógicomatemática(出现在 lógico-matemática 中)
hword_numpart连字符单词的字母+数字部分beta1(出现在 postgresql-beta1 中)
email电子邮箱地址foo@example.com
protocol协议前缀http://
urlURL 地址example.com/stuff/index.html
host主机名example.com
url_pathURL 路径/stuff/index.html(作为 URL 的一部分)
file文件路径/usr/local/foo.txt(不作为 URL 时)
sfloat科学计数法-1.234e56
float小数表示-1.234
int带符号整数-1234
uint无符号整数1234
version版本号8.3.0
tagXML 标签<a href="dictionaries.html">
entityXML 实体&amp;
blank空格或未分类标点符号(如空格、标点)
注意

解析器对“字母”的定义受数据库的 locale 设置中的 lc_ctype 影响。完全由 ASCII 字母构成的词会被单独标为一种类型(asciiword),有时便于特殊处理。在多数欧洲语言中,wordasciiword 类型通常可以合并处理。

email 类型并不完全支持 RFC 5322 中所有合法的邮件地址字符。具体来说,只允许使用字母、数字、点号、短横线和下划线作为邮箱用户名中的字符。

同一段文本中,解析器有可能产生多个重叠的词元。例如,连字符连接的复合词会被同时拆解为整体和各个部分:

SELECT alias, description, token FROM ts_debug('foo-bar-beta1');
alias | description | token
-----------------+------------------------------------------+---------------
numhword | Hyphenated word, letters and digits | foo-bar-beta1
hword_asciipart | Hyphenated word part, all ASCII | foo
blank | Space symbols | -
hword_asciipart | Hyphenated word part, all ASCII | bar
blank | Space symbols | -
hword_numpart | Hyphenated word part, letters and digits | beta1

这种行为是有意设计的,它可以支持对复合词整体和部分的搜索。以下是另一个有参考价值的例子:

SELECT alias, description, token FROM ts_debug('http://example.com/stuff/index.html');
alias | description | token
----------+---------------+------------------------------
protocol | Protocol head | http://
url | URL | example.com/stuff/index.html
host | Host | example.com
url_path | URL path | /stuff/index.html