PostgreSQL 10.1 手册_部分 II. SQL 语言_第 9 章 函数和操作符_9.13. 文本搜索函数和操作符

本文涉及的产品
云原生数据库 PolarDB MySQL 版,Serverless 5000PCU 100GB
简介: 9.13. 文本搜索函数和操作符 表 9.40、 表 9.41和 表 9.42总结了为全文搜索提供的函数和操作符。PostgreSQL的文本搜索功能的详细解释可参考第 12 章。 表 9.40.

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

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

表 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

注意

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

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

表 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'
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
setweight(vector tsvectorweight "char"lexemestext[]) tsvector lexemes中列出的 vector元素分配 权重 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 , documentjson(b)) tsvector 将文档中的每个字符串值减少到tsvector, 然后按文档顺序连接以生成一个tsvector to_tsvector('english', '{"a": "The Fat Rats"}'::json) 'fat':2 'rat':3
ts_delete(vector tsvectorlexeme text) tsvector vector中移除给定的 lexeme ts_delete('fat:2,4 cat:3 rat:5A'::tsvector, 'fat') 'cat':3 'rat':5A
ts_delete(vector tsvectorlexemes text[]) tsvector vector中移除 lexemes中词位的任何出现 ts_delete('fat:2,4 cat:3 rat:5A'::tsvector, ARRAY['fat','rat']) 'cat':3
ts_filter(vector tsvectorweights "char"[]) tsvector vector 中只选择带有给定权重的元素 ts_filter('fat:2,4 cat:3b rat:5A'::tsvector, '{a,b}') 'cat':3B 'rat':5A
ts_headline([ config regconfigdocument textquery tsquery [options text ]) text 显示一个查询匹配 ts_headline('x y z', 'z'::tsquery) x y <b>z</b>
ts_headline([ config regconfigdocument 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 tsvectorquery tsquery [normalization integer ]) float4 为查询排名文档 ts_rank(textsearch, query) 0.818
ts_rank_cd([ weights float4[]vector tsvectorquery tsquery [normalization integer ]) float4 使用覆盖密度为查询排名文档 ts_rank_cd('{0.1, 0.2, 0.4, 1.0}', textsearch, query) 2.01317
ts_rewrite(query tsquerytarget tsquerysubstitute tsquery) tsquery 在查询内用 substitute 替换 target ts_rewrite('a & b'::tsquery, 'a'::tsquery, 'foo|bar'::tsquery) 'b' & ( 'foo' | 'bar' )
ts_rewrite(query tsqueryselect text) tsquery 使用来自一个SELECT的目标和替换者进行替换 SELECT ts_rewrite('a & b'::tsquery, 'SELECT t,s FROM aliases') 'b' & ( 'foo' | 'bar' )
tsquery_phrase(query1 tsqueryquery2 tsquery) tsquery 制造搜索后面跟着query2query1 的查询(和<->操作符相同) tsquery_phrase(to_tsquery('fat'), to_tsquery('cat')) 'fat' <-> 'cat'
tsquery_phrase(query1 tsqueryquery2 tsquerydistance integer) tsquery 制造查询来搜索在query1后面距离 distance上跟着query2的情况 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 positionssmallint[], OUT weights text) setof record 把一个tsvector扩展成一组行 unnest('fat:2,4 cat:3 rat:5A'::tsvector) (cat,{3},{D}) ...

注意

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

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

表 9.42. 文本搜索调试函数

函数 返回类型 描述 例子 结果
ts_debug([ config regconfigdocument text, OUT alias text, OUT description text, OUT token text, OUT dictionaries regdictionary[], OUT dictionary regdictionary, OUT lexemestext[]) setof record 测试一个配置 ts_debug('english', 'The Brightest supernovaes') (asciiword,"Word, all ASCII",The,{english_stem},english_stem,{}) ...
ts_lexize(dict regdictionarytoken text) text[] 测试一个字典 ts_lexize('english_stem', 'stars') {star}
ts_parse(parser_name textdocument text, OUT tokid integer, OUT token text) setof record 测试一个解析器 ts_parse('default', 'foo - bar') (1,foo) ...
ts_parse(parser_oid oiddocument 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 nentryinteger) setof record 获得一个tsvector列的统计 ts_stat('SELECT vector from apod') (foo,10,15) ...

本文转自PostgreSQL中文社区,原文链接:9.13. 文本搜索函数和操作符

相关实践学习
使用PolarDB和ECS搭建门户网站
本场景主要介绍基于PolarDB和ECS实现搭建门户网站。
阿里云数据库产品家族及特性
阿里云智能数据库产品团队一直致力于不断健全产品体系,提升产品性能,打磨产品功能,从而帮助客户实现更加极致的弹性能力、具备更强的扩展能力、并利用云设施进一步降低企业成本。以云原生+分布式为核心技术抓手,打造以自研的在线事务型(OLTP)数据库Polar DB和在线分析型(OLAP)数据库Analytic DB为代表的新一代企业级云原生数据库产品体系, 结合NoSQL数据库、数据库生态工具、云原生智能化数据库管控平台,为阿里巴巴经济体以及各个行业的企业客户和开发者提供从公共云到混合云再到私有云的完整解决方案,提供基于云基础设施进行数据从处理、到存储、再到计算与分析的一体化解决方案。本节课带你了解阿里云数据库产品家族及特性。
相关文章
|
2月前
|
SQL 存储 BI
【软件设计师备考 专题 】数据库语言(SQL)
【软件设计师备考 专题 】数据库语言(SQL)
91 0
|
23天前
|
SQL 数据库 数据库管理
【SQL server】玩转SQL server数据库:第三章 关系数据库标准语言SQL(一)模式、表、索引与视图
【SQL server】玩转SQL server数据库:第三章 关系数据库标准语言SQL(一)模式、表、索引与视图
55 11
|
23天前
|
SQL 算法 数据库
【SQL server】玩转SQL server数据库:第三章 关系数据库标准语言SQL(二)数据查询
【SQL server】玩转SQL server数据库:第三章 关系数据库标准语言SQL(二)数据查询
97 6
|
4天前
|
SQL 关系型数据库 MySQL
【MySQL】:探秘主流关系型数据库管理系统及SQL语言
【MySQL】:探秘主流关系型数据库管理系统及SQL语言
11 0
|
10天前
|
SQL Java 数据库连接
Java从入门到精通:2.3.2数据库编程——了解SQL语言,编写基本查询语句
Java从入门到精通:2.3.2数据库编程——了解SQL语言,编写基本查询语句
|
19天前
|
SQL 数据库
数据库SQL语言实战(一)
数据库SQL语言实战(一)
|
19天前
|
SQL 数据库
数据库SQL语言实战(二)
数据库SQL语言实战(二)
|
23天前
|
SQL Oracle 数据可视化
【Oracle】玩转Oracle数据库(四):SQL语言
【Oracle】玩转Oracle数据库(四):SQL语言
35 8

热门文章

最新文章