Postgresql 中文操作指南
12.5. Parsers #
文本搜索解析器负责将原始文档文本拆分为 tokens 并识别每个令牌的类型,其中可能的类型是由解析器本身定义的。请注意,解析器不会完全修改文本,只识别可能的单词边界。由于其作用域有限,因此对特定于应用程序的自定义解析器的需求低于自定义词典。目前,PostgreSQL 只提供一个内置的解析器,该解析器已证明可用于各种应用程序。
Text search parsers are responsible for splitting raw document text into tokens and identifying each token’s type, where the set of possible types is defined by the parser itself. Note that a parser does not modify the text at all — it simply identifies plausible word boundaries. Because of this limited scope, there is less need for application-specific custom parsers than there is for custom dictionaries. At present PostgreSQL provides just one built-in parser, which has been found to be useful for a wide range of applications.
内置解析器名为 pg_catalog.default。它识别 23 种标记类型,如 Table 12.1 中所示。
The built-in parser is named pg_catalog.default. It recognizes 23 token types, shown in Table 12.1.
Table 12.1. Default Parser’s Token Types
Alias |
Description |
Example |
asciiword |
Word, all ASCII letters |
elephant |
word |
Word, all letters |
mañana |
numword |
Word, letters and digits |
beta1 |
asciihword |
Hyphenated word, all ASCII |
up-to-date |
hword |
Hyphenated word, all letters |
lógico-matemática |
numhword |
Hyphenated word, letters and digits |
postgresql-beta1 |
hword_asciipart |
Hyphenated word part, all ASCII |
postgresql in the context postgresql-beta1 |
hword_part |
Hyphenated word part, all letters |
lógico or matemática in the context lógico-matemática |
hword_numpart |
Hyphenated word part, letters and digits |
beta1 in the context postgresql-beta1 |
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, in the context of a URL |
file |
File or path name |
/usr/local/foo.txt, if not within a 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 |
& |
blank |
Space symbols |
(any whitespace or punctuation not otherwise recognized) |
Note
解析器对“字母”的概念取决于数据库的区域设置,具体为 lc_ctype。仅包含基本 ASCII 字母的单词会报告为一个单独的令牌类型,因为有时区分它们很有用。在大多数欧洲语言中,令牌类型 word 和 asciiword 应得到同等对待。
The parser’s notion of a “letter” is determined by the database’s locale setting, specifically lc_ctype. Words containing only the basic ASCII letters are reported as a separate token type, since it is sometimes useful to distinguish them. In most European languages, token types word and asciiword should be treated alike.
email 不支持 RFC 5322 中定义的所有有效电子邮件字符。具体来说,电子邮件用户名支持的唯一非字母数字字符是句点、连字符和下划线。
email does not support all valid email characters as defined by RFC 5322. Specifically, the only non-alphanumeric characters supported for email user names are period, dash, and underscore.
解析器有可能根据同一文本片段产生重叠的令牌。例如,连字符词将被报告为整个单词和每个组成部分:
It is possible for the parser to produce overlapping tokens from the same piece of text. As an example, a hyphenated word will be reported both as the entire word and as each component:
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
这种行为是理想的,因为它允许搜索对整个复合词和组成部分都起作用。这里有另一个有启发性的示例:
This behavior is desirable since it allows searches to work for both the whole compound word and for components. Here is another instructive example:
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