9.3 9.4 9.5 9.6 10 11 12 13
阿里云PostgreSQL 问题报告 纠错本页面

9.13. 文本检索函数和操作符

表 9-37表 9-38表 9-39概述了提供全文检索函数和操作符。 详见第 12 章 PostgreSQL文本检索功能的说明。

表 9-37. 文本检索操作符

操作符描述示例结果
@@ 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'::tsqueryf
<@ tsquery 包含于 ?'cat'::tsquery <@ 'cat & rat'::tsqueryt

注意: tsquery的操作符只考虑两个查询列出的项的情况,忽略组合的操作符。

除了显示在表中的操作符,还为类型tsvectortsquery 定义了普通的B-tree比较操作符(=, <等)。 它们对于文本检索不是很有用,但是允许使用。例如,建在这些类型列上的唯一索引。

表 9-38. 文本检索函数

函数返回类型描述示例结果
get_current_ts_config() regconfig获取文本检索的默认配置get_current_ts_config()english
length(tsvector) integertsvector的单词数length('fat:2,4 cat:3 rat:5A'::tsvector)3
numnode(tsquery) integertsquery中的单词加上操作符的数量 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") tsvectortsvector的每个元素赋予权值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标准化单词并转换为tsqueryto_tsquery('english', 'The & Fat & Rats')'fat' & 'rat'
to_tsvector([ config regconfig , ] document text) tsvector减少文档中的文本到 tsvectorto_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)

注意: 所有的文本检索函数,接受一个选项regconfig参数。当这个参数忽略, 使用由default_text_search_config指定的配置。

表 9-39单独列出的函数, 因为他们通常不用于每天的文本检索操作。它们有助于开发调试新文本检索配置。

表 9-39. 文本检索调试函数

函数返回类型描述示例结果
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) ...
<
/BODY >