选择 打开 改范围 完整检索页
受支持版本: 当前版本 (18) / 17 / 16 / 15 / 14
开发版本: 19 / devel
不受支持的版本: 13 / 12 / 11 / 10
当前 PostgreSQL 版本不在支持生命周期内。
您可以参阅当前版本的对应页面,或其他在上面列出的活跃大版本。

9.13. 文本搜索函数和操作符 #

Table 9.40Table 9.41以及 Table 9.42 总结了为全文搜索提供的函数和操作符。PostgreSQL的文本搜索功能的详细解释可参考Chapter 12

Table 9.40. 文本搜索操作符

操作符 返回类型 描述 示例 结果
@@ boolean tsvector 是否匹配 tsquery to_tsvector('fat cats ate rats') @@ to_tsquery('cat & rat') t
@@@ boolean @@ 的已弃用同义写法 to_tsvector('fat cats ate rats') @@@ to_tsquery('cat & rat') t
|| tsvector 串接 tsvector 'a:1 b:2'::tsvector || 'c:1 d:2 b:3'::tsvector 'a':1 'b':2,5 'c':3 'd':4
&& tsquery tsquery 进行 AND 组合 'fat | rat'::tsquery && 'cat'::tsquery ( 'fat' | 'rat' ) & 'cat'
|| tsquery tsquery 进行 OR 组合 'fat | rat'::tsquery || 'cat'::tsquery ( 'fat' | 'rat' ) | 'cat'
!! tsquery tsquery 取反 !! 'cat'::tsquery !'cat'
<-> tsquery tsquery 后跟 tsquery to_tsquery('fat') <-> to_tsquery('rat') 'fat' <-> 'rat'
@> boolean tsquery 是否包含另一个查询? 'cat'::tsquery @> 'cat & rat'::tsquery f
<@ boolean tsquery 是否被包含于另一个查询? 'cat'::tsquery <@ 'cat & rat'::tsquery t

Note

tsquery 包含操作符只考虑两个查询中列出的词元,忽略组合操作符。

除了表中列出的操作符,tsvectortsquery 类型还定义了普通的 B-tree 比较操作符(=< 等)。这些操作符对文本搜索用处不大,但可以用于其他用途,例如在这些类型的列上建立唯一索引。

Table 9.41. 文本搜索函数

函数 返回类型 描述 示例 结果
array_to_tsvector(text[]) tsvector 将词元数组转换为 tsvector array_to_tsvector('{fat,cat,rat}'::text[]) 'cat' 'fat' 'rat'
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'
phraseto_tsquery([ config regconfig , ] query text) tsquery 生成搜索短语的 tsquery,忽略标点符号 phraseto_tsquery('english', 'The Fat Rats') 'fat' <-> 'rat'
websearch_to_tsquery([ config regconfig , ] query text) tsquery 从网络搜索风格的查询生成 tsquery websearch_to_tsquery('english', '"fat rat" or rat') 'fat' <-> 'rat' | 'rat'
querytree(query tsquery) text 获取 tsquery 中可索引的部分 querytree('foo & ! bar'::tsquery) 'foo'
setweight(vector tsvector, weight "char") tsvector vector 中的每个元素赋予 weight setweight('fat:2,4 cat:3 rat:5B'::tsvector, 'A') 'cat':3A 'fat':2A,4A 'rat':5A
setweight(vector tsvector, weight "char", lexemes text[]) tsvector vector 中列在 lexemes 内的元素赋予 weight setweight('fat:2,4 cat:3 rat:5B'::tsvector, 'A', '{cat,rat}') 'cat':3A 'fat':2,4 '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
to_tsvector([ config regconfig , ] document json(b)) tsvector 将文档中的每个字符串值转换为 tsvector,再按文档顺序串接,生成单个 tsvector to_tsvector('english', '{"a": "The Fat Rats"}'::json) 'fat':2 'rat':3
json(b)_to_tsvector([ config regconfig, ] document json(b), filter json(b)) tsvector 将文档中由 filter 指定的每个值转换为 tsvector,再按文档顺序串接,生成单个 tsvectorfilter 是一个 jsonb 数组,列出需要包含在结果 tsvector 中的元素种类。filter 的可用值为 "string"(包含所有字符串值)、"numeric"(以字符串形式包含所有数值)、"boolean"(以字符串 "true"/"false" 的形式包含所有布尔值)、"key"(包含所有键)或 "all"(包含上述全部内容)。可以组合这些值,例如包含所有字符串值和数值。 json_to_tsvector('english', '{"a": "The Fat Rats", "b": 123}'::json, '["string", "numeric"]') '123':5 'fat':2 'rat':3
ts_delete(vector tsvector, lexeme text) tsvector vector 中移除给定的 lexeme ts_delete('fat:2,4 cat:3 rat:5A'::tsvector, 'fat') 'cat':3 'rat':5A
ts_delete(vector tsvector, lexemes text[]) tsvector vector 中移除 lexemes 所列词元的所有出现 ts_delete('fat:2,4 cat:3 rat:5A'::tsvector, ARRAY['fat','rat']) 'cat':3
ts_filter(vector tsvector, weights "char"[]) tsvector 仅从 vector 中选出具有给定 weights 的元素 ts_filter('fat:2,4 cat:3b rat:5A'::tsvector, '{a,b}') 'cat':3B 'rat':5A
ts_headline([ config regconfig, ] document text, query tsquery [, options text ]) text 显示查询匹配内容 ts_headline('x y z', 'z'::tsquery) x y <b>z</b>
ts_headline([ config regconfig, ] document json(b), query tsquery [, options text ]) text 显示查询匹配内容 ts_headline('{"a":"x y z"}'::json, 'z'::tsquery) {"a":"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 将查询中的 target 替换为 substitute 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' )
tsquery_phrase(query1 tsquery, query2 tsquery) tsquery 构造搜索 query1 后跟 query2 的查询(与 <-> 操作符相同) tsquery_phrase(to_tsquery('fat'), to_tsquery('cat')) 'fat' <-> 'cat'
tsquery_phrase(query1 tsquery, query2 tsquery, distance integer) tsquery 构造搜索 query1 后跟 query2、间距为 distance 的查询 tsquery_phrase(to_tsquery('fat'), to_tsquery('cat'), 10) 'fat' <10> 'cat'
tsvector_to_array(tsvector) text[] tsvector 转换为词元数组 tsvector_to_array('fat:2,4 cat:3 rat:5A'::tsvector) {cat,fat,rat}
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)
unnest(tsvector, OUT lexeme text, OUT positions smallint[], OUT weights text) setof record tsvector 展开为一组行 unnest('fat:2,4 cat:3 rat:5A'::tsvector) (cat,{3},{D}) ...

Note

所有接受一个可选的regconfig参数的文本搜索函数在该参数被忽略时,使用由default_text_search_config指定的配置。

Table 9.42中的函数被单独列出,因为它们通常不被用于日常的文本搜索操作。 它们主要有助于开发和调试新的文本搜索配置。

Table 9.42. 文本搜索调试函数

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