pgsql学习笔记:函数和操作符(四)

10、文本搜索函数和操作符

文本搜索操作符


操作符
描述
例子


tsvector @@ tsquery → boolean
tsquery @@ tsvector → boolean
tsvector匹配tsquery吗?(参数可以按任意顺序给出。)
to_tsvector(‘fat cats ate rats’) @@ to_tsquery(‘cat & rat’) → t


text @@ tsquery → boolean
隐式调用to_tsvector()后的文本字符串匹配tsquery么 ?
‘fat cats ate rats’ @@ to_tsquery(‘cat & rat’) → t


tsvector || tsvector → 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 → tsquery
ANDs两个tsquery一起,生成一个匹配两个输入查询的匹配文档的查询。
‘fat | rat’::tsquery && ‘cat’::tsquery → ( ‘fat’ | ‘rat’ ) & ‘cat’


tsquery || tsquery → tsquery
ORs两个tsquery一起,生成一个匹配两个输入查询的匹配文档的查询。
‘fat | rat’::tsquery || ‘cat’::tsquery → ‘fat’ | ‘rat’ | ‘cat’


!! tsquery → tsquery
否定tsquery,生成一个与输入查询不匹配的匹配文档的查询。

!! ‘cat’::tsquery → !‘cat’


tsquery <-> tsquery → tsquery
构造一个短语查询,如果两个输入查询在连续的词素上匹配,该查询将进行匹配。
to_tsquery(‘fat’) <-> to_tsquery(‘rat’) → ‘fat’ <-> ‘rat’


tsquery @> tsquery → boolean
第一个tsquery包含了第二个吗?(这只考虑出现在一个查询中的所有词素是否出现在另一个查询中,忽略了组合操作符。)
‘cat’::tsquery @> ‘cat & rat’::tsquery → f


tsquery <@ tsquery → boolean
第一个tsquery包含在第二个中吗?(这只考虑出现在一个查询中的所有词素是否出现在另一个查询中,而忽略了组合操作符。)
‘cat’::tsquery <@ ‘cat & rat’::tsquery → t
‘cat’::tsquery <@ ‘!cat & rat’::tsquery → t


文本搜索函数


函数
描述
例子


array_to_tsvector ( text[] ) → tsvector
将词素数组转换为tsvector。给定的字符串按原样使用,不做进一步处理。
array_to_tsvector(‘{fat,cat,rat}’::text[]) → ‘cat’ ‘fat’ ‘rat’


get_current_ts_config ( ) → regconfig
返回当前默认文本搜索配置的OID(由 default_text_search_config 所设定的).
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’
phraseto_tsquery(‘english’, ‘The Cat and Rats’) → ‘cat’ <2> ‘rat’


websearch_to_tsquery ( [ config regconfig, ] query text ) → tsquery
将文本转换为tsquery,根据指定的或默认配置对单词进行标准化。引用的单词序列被转换为短语测试。 “or”一词被理解为产生OR操作符,而破折号产生NOT操作符;其他标点符号被忽略。这类似于一些常见的网络搜索工具的行为。
websearch_to_tsquery(‘english’, ‘“fat rat” or cat dog’) → ‘fat’ <-> ‘rat’ | ‘cat’ & ‘dog’


querytree ( tsquery ) → text
生成tsquery的可转位部分的表示。结果为空或仅为T表示不可索引查询。
querytree(‘foo & ! bar’::tsquery) → ‘foo’


setweight ( vector tsvector, weight “char” ) → tsvector
将指定的weight赋给vector的每个元素。
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
将指定的weight赋给列在lexemes中的vector元素。
setweight(‘fat:2,4 cat:3 rat:5,6B’::tsvector, ‘A’, ‘{cat,rat}’) → ‘cat’:3A ‘fat’:2,4 ‘rat’:5A,6A


strip ( tsvector ) → tsvector
从tsvector中移除位置和权重。
strip(‘fat:2,4 cat:3 rat:5A’::tsvector) → ‘cat’ ‘fat’ ‘rat’


to_tsquery ( [ config regconfig, ] query text ) → tsquery
将文本转换为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 ) → tsvector
to_tsvector ( [ config regconfig, ] document jsonb ) → tsvector
将JSON文档中的每个字符串值转换为tsvector,根据指定的或默认配置对单词进行标准化。 然后将结果按文档顺序连接起来以产生输出。位置信息就像在每对字符串值之间存在一个停止词一样生成。 (注意,当输入为jsonb时,JSON对象的字段的“document order”取决于实现;请观察这些例子中的差异。)
to_tsvector(‘english’, ‘{“aa”: “The Fat Rats”, “b”: “dog”}’::json) → ‘dog’:5 ‘fat’:2 ‘rat’:3
to_tsvector(‘english’, ‘{“aa”: “The Fat Rats”, “b”: “dog”}’::jsonb) → ‘dog’:1 ‘fat’:4 ‘rat’:5


json_to_tsvector ( [ config regconfig, ] document json, filter jsonb ) → tsvector
jsonb_to_tsvector ( [ config regconfig, ] document jsonb, filter jsonb ) → tsvector
选择filter请求的JSON文档中的每个项,并将每个项转换为tsvector,根据指定的或默认配置对单词进行标准化。 然后将结果按文档顺序连接起来以产生输出。位置信息就像在每对选定的项目之间存在一个停止词一样生成。 (注意,当输入为jsonb时,JSON对象字段的“document order”取决于实现。) filter必须是一个jsonb数组,其中包含0个或多个关键字: “string”(包括所有字符串值), “numeric”(包括所有数值), “boolean”(包括所有布尔值), “key”(包括所有键),或 “all”(包括以上所有关键字)。 作为一种特殊情况,该filter也可以是这些关键字之一的简单JSON值。
json_to_tsvector(‘english’, ‘{“a”: “The Fat Rats”, “b”: 123}’::json, ‘[“string”, “numeric”]’) → ‘123’:5 ‘fat’:2 ‘rat’:3
json_to_tsvector(‘english’, ‘{“cat”: “The Fat Rats”, “dog”: 123}’::json, ‘“all”’) → ‘123’:9 ‘cat’:1 ‘dog’:7 ‘fat’:4 ‘rat’:5


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,7c rat:5A’::tsvector, ‘{a,b}’) → ‘cat’:3B ‘rat’:5A


ts_headline ( [ config regconfig, ] document text, query tsquery [, options text ] ) → text
以缩写形式显示document中query的匹配项,该匹配项必须是原始文本,而不是tsvector。 在匹配查询之前,文档中的单词将根据指定的或默认的配置进行规范化。
ts_headline(‘The fat cat ate the rat.’, ‘cat’) → The fat cat ate the rat.


ts_headline ( [ config regconfig, ] document json, query tsquery [, options text ] ) → text
ts_headline ( [ config regconfig, ] document jsonb, query tsquery [, options text ] ) → text
以缩写形式显示匹配JSONdocument中字符串值中的query。
ts_headline(‘{“cat”:“raining cats and dogs”}’::jsonb, ‘cat’) → {“cat”: “raining cats and dogs”}


ts_rank ( [ weights real[], ] vector tsvector, query tsquery [, normalization integer ] ) → real
计算一个分数,显示vector与query的匹配程度。
ts_rank(to_tsvector(‘raining cats and dogs’), ‘cat’) → 0.06079271


ts_rank_cd ( [ weights real[], ] vector tsvector, query tsquery [, normalization integer ] ) → real
使用覆盖密度算法计算一个分数,显示vector与query的匹配程度。
ts_rank_cd(to_tsvector(‘raining cats and dogs’), ‘cat’) → 0.1


ts_rewrite ( query tsquery, target tsquery, substitute tsquery ) → tsquery
在query中使用 substitute替换出现的target。
ts_rewrite(‘a & b’::tsquery, ‘a’::tsquery, ‘foo|bar’::tsquery) → ‘b’ & ( ‘foo’ | ‘bar’ )


ts_rewrite ( query tsquery, select text ) → tsquery
根据目标替换部分query,并替换通过执行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}


unnest ( tsvector ) → setof record ( lexeme text, positions smallint[], weights text )
将tsvector展开为一组行,每个行对应一个词位。

select * from unnest('cat:3 fat:2,4 rat:5A'::tsvector) →

 lexeme | positions | weights
--------+-----------+---------
 cat    | {3}       | {D}
 fat    | {2,4}     | {D,D}
 rat    | {5}       | {A}

11、 UUID 函数

PostgreSQL 包含一个函数来生成UUID:

gen_random_uuid () → uuid
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值