Redrock Postgres 搜索 英文
版本: 9.3 / 9.4 / 9.5 / 9.6 / 10 / 11 / 12 / 13 / 14 / 15 / 16 / 17

12.5. 解析器 #

文本搜索解析器负责将原始文档文本拆分到令牌中,并识别每种令牌的类型,可能的类型集由解析器本身定义。请注意,解析器根本不会修改文本 — 它只是识别合理的单词边界。由于此范围有限,因此与自定义词典相比,对特定于应用程序的自定义解析器的需求较少。目前PostgreSQL只提供一个内置解析器,该解析器已被证明对广泛的应用程序很有用。

内置分析器名为 pg_catalog.default。它识别 23 种令牌类型,显示在 表 12.1 中。

表 12.1。默认分析器的令牌类型

别名 说明 示例
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 电子邮件地址 [email protected]
protocol 协议头 http://
url URL example.com/stuff/index.html
host 主机 example.com
url_path URL 路径 /stuff/index.html,在 URL 上下文中
file 文件或路径名称 /usr/local/foo.txt,如果不在 URL 内
sfloat 科学记数法 -1.234e56
float 十进制 -1.234
int 有符号整数 -1234
uint 无符号整数 1234
version 版本号 8.3.0
tag XML 标签 <a href="dictionaries.html">
entity XML 实体 &amp;
blank 空格符号 (任何未被识别为其他类型的空格或标点符号)

注意

分析器对 letter 的概念由数据库的语言环境设置决定,特别是 lc_ctype。只包含基本 ASCII 字母的单词以单独的令牌类型报告,因为有时将它们区分开来很有用。在大多数欧洲语言中,令牌类型 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