历史版本。 PostgreSQL 8.4 已结束支持。 请参阅 当前版本手册.
9.13. 文本搜索函数和操作符 #
表 9.36、表 9.37以及表 9.38总结了为全文检索提供的函数和操作符。PostgreSQL的文本检索功能的详细解释可参考第 12 章。
表 9.36. 文本搜索操作符
| 操作符 | 描述 | 示例 | 结果 |
|---|---|---|---|
@@ | tsvector 是否匹配 tsquery? | to_tsvector('fat cats ate rats') @@ to_tsquery('cat & rat') | t |
@@@ | @@ 的已弃用同义写法 | to_tsvector('fat cats ate rats') @@@ to_tsquery('cat & rat') | t |
|| | 串接 tsvector | 'a:1 b:2'::tsvector || 'c:1 d:2 b:3'::tsvector | 'a':1 'b':2,5 'c':3 'd':4 |
&& | 将 tsquery 进行 AND 组合 | 'fat | rat'::tsquery && 'cat'::tsquery | ( 'fat' | 'rat' ) & 'cat' |
|| | 把多个tsquery进行 OR 组合 | 'fat | rat'::tsquery || 'cat'::tsquery | ( 'fat' | 'rat' ) | 'cat' |
!! | 求 tsquery 的反 | !! 'cat'::tsquery | !'cat' |
@> | tsquery 是否包含另一个查询? | 'cat'::tsquery @> 'cat & rat'::tsquery | f |
<@ | tsquery 是否被包含于另一个查询? | 'cat'::tsquery <@ 'cat & rat'::tsquery | t |
注意
tsquery 包含操作符只考虑两个查询中列出的词位,忽略组合操作符。
除了表中列出的操作符,tsvector 和 tsquery 类型还定义了普通的 B-树比较操作符(=、< 等)。这些操作符对文本检索用处不大,但可以用于其他用途,例如在这些类型的列上建立唯一索引。
表 9.37. 文本搜索函数
| 函数 | 返回类型 | 描述 | 示例 | 结果 |
|---|---|---|---|---|
| tsvector | 把文档文本归约为 tsvector | to_tsvector('english', 'The Fat Rats') | 'fat':2 'rat':3 |
| integer | tsvector 中词位的数量 | length('fat:2,4 cat:3 rat:5A'::tsvector) | 3 |
| tsvector | 为tsvector的每个元素指派权重 | setweight('fat:2,4 cat:3 rat:5B'::tsvector, 'A') | 'cat':3A 'fat':2A,4A 'rat':5A |
| tsvector | 移除tsvector中的位置和权重 | strip('fat:2,4 cat:3 rat:5A'::tsvector) | 'cat' 'fat' 'rat' |
| tsquery | 规范化单词并转换为tsquery | to_tsquery('english', 'The & Fat & Rats') | 'fat' & 'rat' |
| tsquery | 忽略标点生成tsquery | plainto_tsquery('english', 'The Fat Rats') | 'fat' & 'rat' |
| integer | tsquery中词素加操作符的数量 | numnode('(fat & rat) | cat'::tsquery) | 5 |
| text | 获取tsquery的可索引部分 | querytree('foo & ! bar'::tsquery) | 'foo' |
| float4 | 为查询对文档排名 | ts_rank(textsearch, query) | 0.818 |
| float4 | 使用覆盖密度为查询对文档排名 | ts_rank_cd('{0.1, 0.2, 0.4, 1.0}', textsearch, query) | 2.01317 |
| text | 显示查询匹配 | ts_headline('x y z', 'z'::tsquery) | x y <b>z</b> |
| tsquery | 在查询中用替代子查询替换目标子查询 | ts_rewrite('a & b'::tsquery, 'a'::tsquery, 'foo|bar'::tsquery) | 'b' & ( 'foo' | 'bar' ) |
| tsquery | 使用 SELECT 命令提供的目标和替代内容进行替换 | SELECT ts_rewrite('a & b'::tsquery, 'SELECT t,s FROM aliases') | 'b' & ( 'foo' | 'bar' ) |
| regconfig | 获取默认文本搜索配置 | get_current_ts_config() | english |
| trigger | 自动更新 tsvector 列的触发器函数 | CREATE TRIGGER ... tsvector_update_trigger(tsvcol, 'pg_catalog.swedish', title, body) | |
| trigger | 自动更新 tsvector 列的触发器函数 | CREATE TRIGGER ... tsvector_update_trigger_column(tsvcol, configcol, title, body) | |
注意
所有接受一个可选的regconfig参数的文本检索函数在省略该参数时,会使用由default_text_search_config指定的配置。
表 9.38中的函数被单独列出,因为它们通常不被用于日常的文本检索操作。它们有助于开发和调试新的文本检索配置。
表 9.38. 文本搜索调试函数
| 函数 | 返回类型 | 描述 | 示例 | 结果 |
|---|---|---|---|---|
| setof record | 测试配置 | ts_debug('english', 'The Brightest supernovaes') | (asciiword,"Word, all ASCII",The,{english_stem},english_stem,{}) ... |
| text[] | 测试词典 | ts_lexize('english_stem', 'stars') | {star} |
| setof record | 测试解析器 | ts_parse('default', 'foo - bar') | (1,foo) ... |
| setof record | 测试解析器 | ts_parse(3722, 'foo - bar') | (1,foo) ... |
| setof record | 获取解析器定义的词元类型 | ts_token_type('default') | (1,asciiword,"Word, all ASCII") ... |
| setof record | 获取解析器定义的词元类型 | ts_token_type(3722) | (1,asciiword,"Word, all ASCII") ... |
| setof record | 获取 tsvector 列的统计信息 | ts_stat('SELECT vector from apod') | (foo,10,15) ... |