8.3 12.5 分析器(分词程序)
From PostgreSQL 中文维基, PostgreSQL 中文站, PostgreSQL 中国社区, PostgreSQL Chinese community
[编辑] 分析器(分词程序)
文本搜索分析器(分词程序)负责把裸的文档文本分解成记号并且标识每个记号的类型,这里可能的类型集由分析器(分词程序)自身定义。请注意分析器(分词程序)并不修改文本 -- 它只是标识可能的词的边界。因为这个有限的范围,所以应用相关的客户化分析器没有客户化的字典需求那么大。目前,PostgreSQL 只提供了一个内置的分析器,我们发现它可以适用于很广的应用范围。
内置的分析器(分词程序)叫 pg_catalog.default。它包含 23 种记号类型:
| 别名 | 描述 | 例子 |
| 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-beta1 里的 postgresql |
| hword_part | 连接成的词的部分,所有字母 | lógico-matemática 里的 lógico 或 matemática |
| hword_numpart | 连接成的词的部分,字母和数字 | postgresql-beta1 里的 beta1 |
| Email 地址 | foo@example.com | |
| protocol | 协议头 | http:// |
| url | URL | example.com/stuff/index.html |
| host | 主机 | example.com |
| url_path | URL 路径 | /stuff/index.html, in the context of a URL |
| file | 文件或路径名 | /usr/local/foo.txt, if not within a URL |
| sfloat | 科学计数法 | -1.234e56 |
| float | 小数表示法 | -1.234 |
| int | 符号整数 | -1234 |
| uint | 无符号整数 | 1234 |
| version | 版本号 | 8.3.0 |
| tag | XML 标签 | <a href="dictionaries.html"> |
| entity | XML entity | & |
| blank | 空白符号 | (任意空白或者其它会被识别的标点) |
- 注意: 分析器(分词程序)的“字母”的概念是由服务器的区域设置决定的,准确说是 lc_type。只包含基本 ASCII 字母的单词会被报告为独立的记号类型,因为有时候对它们区别对待还是有用的。在大多数欧洲语言里,记号类型的单词和 ascii 类型的单词应该总是一致对待的。
我们可以用分析器(分词程序)从同一段文本里生成重叠的记号。比如,一个连接词就会被当做整个词报告,也会被分隔成几个部分报告出来:
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
