9.13. 文本检索函数和操作符
Table 9-36,Table 9-37 和Table 9-38概述了提供全文检索函数和操作符。 详见Chapter 12 PostgreSQL文本检索功能的说明。
Table 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 |
|| |
连接tsvectors |
'a:1 b:2'::tsvector || 'c:1 d:2 b:3'::tsvector |
'a':1 'b':2,5 'c':3 'd':4 |
&& |
tsquery与 |
'fat | rat'::tsquery && 'cat'::tsquery |
( 'fat' | 'rat' ) & 'cat' |
|| |
tsquery或 |
'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 |
Note:
tsquery的操作符只考虑两个查询列出的项的情况,忽略组合的操作符。
除了显示在表中的操作符,还为类型tsvector和tsquery 定义了普通的B-tree比较操作符(=, <等)。 它们对于文本检索不是很有用,但是允许使用。例如,建在这些类型列上的唯一索引。
Table 9-37. 文本检索函数
| 函数 | 返回类型 | 描述 | 示例 | 结果 |
|---|---|---|---|---|
get_current_ts_config() |
regconfig |
获取文本检索的默认配置 | get_current_ts_config() |
english |
`length(```tsvector) |
integer |
tsvector的单词数 |
length('fat:2,4 cat:3 rat:5A'::tsvector) |
3 |
`numnode(```tsquery) |
integer |
tsquery中的单词加上操作符的数量 |
numnode('(fat & rat) | cat'::tsquery) |
5 |
`plainto_tsquery([ ```_config_ regconfig , ] _query_ text) |
tsquery |
产生tsquery忽略标点 |
plainto_tsquery('english', 'The Fat Rats') |
'fat' & 'rat' |
`querytree(```_query_ tsquery) |
text |
获取tsquery可索引的部分 |
querytree('foo & ! bar'::tsquery) |
'foo' |
`setweight(```tsvector, "char") |
tsvector |
给tsvector的每个元素赋予权值 |
setweight('fat:2,4 cat:3 rat:5B'::tsvector, 'A') |
'cat':3A 'fat':2A,4A 'rat':5A |
`strip(```tsvector) |
tsvector |
删除tsvector中的位置和权值 |
strip('fat:2,4 cat:3 rat:5A'::tsvector) |
'cat' 'fat' 'rat' |
`to_tsquery([ ```_config_ regconfig , ] _query_ text) |
tsquery |
标准化单词并转换为tsquery |
to_tsquery('english', 'The & Fat & Rats') |
'fat' & 'rat' |
`to_tsvector([ ```_config_ regconfig , ] _document_ text) |
tsvector |
减少文档中的文本到 tsvector |
to_tsvector('english', 'The Fat Rats') |
'fat':2 'rat':3 |
`ts_headline([ ```_config_ regconfig, ] _document_ text, _query_ tsquery [, _options_ text ]) |
text |
显示一个查询的匹配项 | ts_headline('x y z', 'z'::tsquery) |
x y <b>z</b> |
`ts_rank([ ```_weights_ float4[], ] _vector_ tsvector, _query_ tsquery [, _normalization_ integer ]) |
float4 |
文档查询排名 | ts_rank(textsearch, query) |
0.818 |
`ts_rank_cd([ ```_weights_ float4[], ] _vector_ tsvector, _query_ tsquery [, _normalization_ integer ]) |
float4 |
排序文件查询使用覆盖密度 | ts_rank_cd('{0.1, 0.2, 0.4, 1.0}', textsearch, query) |
2.01317 |
`ts_rewrite(```_query_ tsquery, _target_ tsquery, _substitute_ tsquery) |
tsquery |
替换带有查询的替代目标 | ts_rewrite('a & b'::tsquery, 'a'::tsquery, 'foo|bar'::tsquery) |
'b' & ( 'foo' | 'bar' ) |
`ts_rewrite(```_query_ tsquery, _select_ text) |
tsquery |
从一条SELECT命令的替代目标做替换 |
SELECT ts_rewrite('a & b'::tsquery, 'SELECT t,s FROM aliases') |
'b' & ( 'foo' | 'bar' ) |
tsvector_update_trigger() |
trigger |
自动更新tsvector列的触发器函数 |
CREATE TRIGGER ... tsvector_update_trigger(tsvcol, 'pg_catalog.swedish', title, body) |
|
tsvector_update_trigger_column() |
trigger |
自动更新tsvector列的触发器函数 |
CREATE TRIGGER ... tsvector_update_trigger_column(tsvcol, configcol, title, body) |
Note: 所有的文本检索函数,接受一个选项
regconfig参数。当这个参数忽略, 使用由default_text_search_config指定的配置。
Table 9-38单独列出的函数, 因为他们通常不用于每天的文本检索操作。它们有助于开发调试新文本检索配置。
Table 9-38. 文本检索调试函数
| 函数 | 返回类型 | 描述 | 示例 | 结果 |
|---|---|---|---|---|
`ts_debug([ ```_config_ regconfig, ] _document_ text, OUT _alias_ text, OUT _description_ text, OUT _token_ text, OUT _dictionaries_ regdictionary[], OUT _dictionary_ regdictionary, OUT _lexemes_ text[]) |
setof record |
测试一个配置 | ts_debug('english', 'The Brightest supernovaes') |
(asciiword,"Word, all ASCII",The,{english_stem},english_stem,{}) ... |
`ts_lexize(```_dict_ regdictionary, _token_ text) |
text[] |
测试一个数据字典 | ts_lexize('english_stem', 'stars') |
{star} |
`ts_parse(```_parser_name_ text, _document_ text, OUT _tokid_ integer, OUT _token_ text) |
setof record |
测试一个解析 | ts_parse('default', 'foo - bar') |
(1,foo) ... |
`ts_parse(```_parser_oid_ oid, _document_ text, OUT _tokid_ integer, OUT _token_ text) |
setof record |
测试一个解析 | ts_parse(3722, 'foo - bar') |
(1,foo) ... |
`ts_token_type(```_parser_name_ text, OUT _tokid_ integer, OUT _alias_ text, OUT _description_ text) |
setof record |
由解析器获取分词类型的定义 | ts_token_type('default') |
(1,asciiword,"Word, all ASCII") ... |
`ts_token_type(```_parser_oid_ oid, OUT _tokid_ integer, OUT _alias_ text, OUT _description_ text) |
setof record |
由解析器获取分词类型的定义 | ts_token_type(3722) |
(1,asciiword,"Word, all ASCII") ... |
`ts_stat(```_sqlquery_ text, [ _weights_ text, ] OUT _word_ text, OUT _ndoc_ integer, OUT _nentry_ integer) |
setof record |
获取tsvector列的统计数据 |
ts_stat('SELECT vector from apod') |
(foo,10,15) ... |