12.5. Parsers

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

内置的解析器名为pg_catalog.default。它可以识别 23 种令牌类型,如Table 12.1所示。

表 12.1 默认解析器的令牌类型

Alias Description Example
asciiword Word,所有 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ógico-matemática中为lógicomatemática
hword_numpart 带连字符的单词部分,字母和数字 beta1在上下文postgresql-beta1
email Email address foo@example.com
protocol Protocol head http://
url URL example.com/stuff/index.html
host Host example.com
url_path URL path /stuff/index.html,在 URL 的上下文中
file 文件或路径名 /usr/local/foo.txt(如果不在 URL 中)
sfloat Scientific notation -1.234e56
float Decimal notation -1.234
int Signed integer -1234
uint Unsigned integer 1234
version Version number 8.3.0
tag XML tag <a href="dictionaries.html">
entity XML entity &amp;
blank Space symbols (任何空白或标点符号都无法识别)

Note

解析器的“字母”概念由数据库的语言环境设置(特别是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
上一章 首页 下一章