12.5. Parsers

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

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

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

AliasDescriptionExample
asciiwordWord,所有 ASCII 字母elephant
word单词,所有字母mañana
numword单词,字母和数字beta1
asciihword连字,全 ASCIIup-to-date
hword连字,全字母lógico-matemática
numhword连字,字母和数字postgresql-beta1
hword_asciipart连字符部分,全为 ASCIIpostgresql在上下文postgresql-beta1
hword_part连字符部分,所有字母在上下文lógico-matemática中为lógicomatemática
hword_numpart带连字符的单词部分,字母和数字beta1在上下文postgresql-beta1
emailEmail address[email protected]
protocolProtocol headhttp://
urlURLexample.com/stuff/index.html
hostHostexample.com
url_pathURL path/stuff/index.html,在 URL 的上下文中
file文件或路径名/usr/local/foo.txt(如果不在 URL 中)
sfloatScientific notation-1.234e56
floatDecimal notation-1.234
intSigned integer-1234
uintUnsigned integer1234
versionVersion number8.3.0
tagXML tag<a href="dictionaries.html">
entityXML entity&amp;
blankSpace 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