ThinkChat🤖让你学习和工作更高效,注册即送10W Token,即刻开启你的AI之旅 广告
[TOC] > [手册](http://www.postgres.cn/docs/9.3.4/functions-textsearch.html) ## 概述 基于 pg9.x ## 文本搜索函数和操作符 |操作符|描述|例子|结果| |---|---|---|---| |@@|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| ## 文本搜索函数 |函数|返回类型|描述|例子|结果| |---|---|---|---|---| |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|在查询内用 substitute 替换 target|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)||