# 9.13.文本搜索函数和运算符

表9.41,表9.42表9.43总结全文搜索提供的函数和运算符。看见第12章有关PostgreSQL文本搜索功能的详细说明。

表9.41.文本搜索操作员

操作人员

描述

例子
tsvector @@ tsquery布尔值

tsquery @@ tsvector布尔值

tsvector火柴tsquery? (参数可以按任意顺序给出。)

to_tsvector(“肥猫吃老鼠”)@@to_tsquery(“猫和老鼠”)t
文本 @@ tsquery布尔值

在隐式调用to_tsvector()火柴tsquery?

“肥猫吃老鼠”@@to_tsquery(“猫和老鼠”)t
tsvector @@@ tsquery布尔值

tsquery @@@ tsvector布尔值

这是一个不推荐使用的同义词@@.

to_tsvector(“肥猫吃老鼠”)@@@to_tsquery(“猫和老鼠”)t
tsvector || tsvectortsvector

Concatenates twotsvectors. If both inputs contain lexeme positions, the second input's positions are adjusted accordingly.

'a:1 b:2'::tsvector || 'c:1 d:2 b:3'::tsvector'a':1 'b':2,5 'c':3 'd':4
tsquery && tsquerytsquery

ANDs twotsquerys together, producing a query that matches documents that match both input queries.

'fat | rat'::tsquery && 'cat'::tsquery( 'fat' | 'rat' ) & 'cat'
tsquery || tsquerytsquery

ORs twotsquerys together, producing a query that matches documents that match either input query.

'fat | rat'::tsquery || 'cat'::tsquery'fat' | 'rat' | 'cat'
!! tsquerytsquery

Negates atsquery, producing a query that matches documents that do not match the input query.

!! 'cat'::tsquery!'cat'
tsquery <-> tsquerytsquery

Constructs a phrase query, which matches if the two input queries match at successive lexemes.

to_tsquery('fat') <-> to_tsquery('rat')'fat' <-> 'rat'
tsquery @> tsqueryboolean

Does firsttsquerycontain the second? (This considers only whether all the lexemes appearing in one query appear in the other, ignoring the combining operators.)

'cat'::tsquery @> 'cat & rat'::tsqueryf
tsquery <@ tsqueryboolean

Is firsttsquerycontained in the second? (This considers only whether all the lexemes appearing in one query appear in the other, ignoring the combining operators.)

'cat'::tsquery <@ 'cat & rat'::tsqueryt

'cat'::tsquery <@ '!cat & rat'::tsqueryt

In addition to these specialized operators, the usual comparison operators shown inTable 9.1are available for typestsvectorandtsquery. These are not very useful for text searching but allow, for example, unique indexes to be built on columns of these types.

Table 9.42. Text Search Functions

Function

Description

Example(s)
array_to_tsvector(文本[] ) → tsvector

将词素数组转换为tsvector.给定的字符串按原样使用,无需进一步处理。

数组_to_tsvector(“{fat,cat,rat}”)::text[]“猫”“胖”“老鼠”
获取当前配置 ( ) → regconfig

返回当前默认文本搜索配置(由设置)的OID违约_文本_搜索_配置).

获取当前配置英语
( tsvector ) → 整数

返回列表中的词素数tsvector.

长度('脂肪:2,4猫:3大鼠:5A'::tsvector)3.
纽诺德 ( tsquery ) → 整数

返回列表中词素加运算符的数目tsquery.

numnode(“(脂肪和老鼠)|猫”::tsquery)5.
普莱托·尤茨基 ( [ 配置 regconfig, ] 查询 文本 ) → tsquery

将文本转换为tsquery,根据指定或默认配置规范化单词。忽略字符串中的任何标点符号(它不确定查询运算符)。结果查询匹配文本中包含所有非停止词的文档。

普兰托·尤茨基(‘英语’、‘胖老鼠’)“胖子”和“老鼠”
短语to_tsquery ( [ 配置 regconfig, ] 查询 文本 ) → tsquery

将文本转换为tsquery,根据指定或默认配置规范化单词。忽略字符串中的任何标点符号(它不确定查询运算符)。结果查询匹配包含文本中所有非停止词的短语。

短语to_tsquery(“英语”,“肥鼠”)“肥胖”<->“老鼠”

短语to_tsquery(‘英语’、‘猫和老鼠’)“猫”<2>“老鼠”
网络搜索 ( [ 配置 regconfig, ] 查询 文本 ) → tsquery

将文本转换为tsquery,根据指定或默认配置规范化单词。引用的单词序列被转换为短语测试。“or”一词被理解为产生or运算符,破折号产生NOT运算符;其他标点符号被忽略。这近似于一些常见的web搜索工具的行为。

websearch_to_tsquery('english'、'fat rat'或cat dog')“胖”<->“老鼠”|“猫”和“狗”
栎树 ( tsquery ) → 文本

生成可转位部分的表示形式tsquery.结果是空的或只是T指示不可索引的查询。

querytree('foo&!bar':tsquery)“福”
设定重量 ( 矢量 tsvector, 重量 “char” ) → tsvector

分配指定的*重量每个元素的矢量*.

设定体重('脂肪:2,4猫:3大鼠:5B'::tsvector'A')“猫”:3A“脂肪”:2A,4A“老鼠”:5A
设定重量 ( 矢量 tsvector, 重量 “char”, 词位 文本[] ) → tsvector

分配指定的*重量矢量列在词位*.

设定体重('脂肪:2,4猫:3鼠:5,6B'::tsvector'A','{cat,rat}')“猫”:3A“脂肪”:2,4“老鼠”:5A、6A
( tsvector ) → tsvector

从中删除位置和权重tsvector.

条状('脂肪:2,4猫:3鼠:5A'::tsvector)“猫”“胖”“老鼠”
质疑 ( [ 配置 regconfig, ] 查询 文本 ) → tsquery

将文本转换为tsquery,根据指定或默认配置规范化单词。这些单词必须用有效的字母组合tsquery接线员。

to_tsquery('english'、'The&Fat&Rats')“胖子”和“老鼠”
到_tsvector ( [ 配置 regconfig, ] 文件 文本 ) → tsvector

将文本转换为tsvector,根据指定或默认配置规范化单词。结果中包含位置信息。

to_tsvector(“英语”,“肥鼠”)“肥胖”:2“老鼠”:3
到_tsvector ( [ 配置 regconfig, ] 文件 json ) → tsvector

到_tsvector ( [ 配置 regconfig, ] 文件 jsonb ) → tsvector

将JSON文档中的每个字符串值转换为tsvector,根据指定或默认配置规范化单词。然后将结果连接到文档中以生成输出。位置信息的生成就像每对字符串值之间存在一个停止字一样。(注意,当输入为空时,JSON对象字段的“文档顺序”取决于实现。)jsonb; 观察示例中的差异。)

to_tsvector('english','{“aa”:“肥鼠”,“b”:“狗”}'::json)“狗”:5“脂肪”:2“老鼠”:3

to_tsvector('english','{“aa”:“肥鼠”,“b”:“狗”}'::jsonb)“狗”:1“脂肪”:4“老鼠”:5
json_to_tsvector ( [ 配置 regconfig, ] 文件 json, 滤器 jsonb ) → tsvector

jsonb_to_tsvector ( [ 配置 regconfig, ] 文件 jsonb, 滤器 jsonb ) → tsvector

选择JSON文档中由*滤器把每一个都变成一个tsvector,根据指定或默认配置规范化单词。然后将结果连接到文档中以生成输出。位置信息的生成就像每对选定项目之间存在一个停止字一样。(注意,当输入为空时,JSON对象字段的“文档顺序”取决于实现。)jsonb)The滤器一定是个jsonb包含零个或多个以下关键字的数组:“字符串”(包括所有字符串值),“数字”(包括所有数值),“布尔”(包括所有布尔值),“钥匙”(包括所有钥匙),或“全部”(包括上述所有内容)。作为特例滤器*也可以是这些关键字之一的简单JSON值。

json_to_tsvector('english','{a:'The Fat Rats',b:'123}'::json,[“string”,“numeric”])“123”:5“脂肪”:2“老鼠”:3

json_to_tsvector('english','{cat:“肥鼠”,“狗”:123}'::json,'all')“123”:9“猫”:1“狗”:7“肥”:4“鼠”:5
删除 ( 矢量 tsvector, 词素 文本 ) → tsvector

删除给定事件的任何出现*词素矢量*.

ts_delete('fat:2,4 cat:3 rat:5A'::tsvector'fat')“猫”:3“老鼠”:5A
删除 ( 矢量 tsvector, 词位 文本[] ) → tsvector

删除中出现的所有词素*词位矢量*.

ts_delete('fat:2,4 cat:3 rat:5A'::tsvector,数组['fat','rat']))“猫”:3
ts_过滤器 ( 矢量 tsvector, 砝码 “char”[] ) → tsvector

仅选择具有给定属性的元素*砝码矢量*.

ts_过滤器('fat:2,4 cat:3b,7c rat:5A'::tsvector,{a,b}')“猫”:3B“老鼠”:5A
标题 ( [ 配置 regconfig, ] 文件 文本, 查询 tsquery [, 选项 文本 ] ) → 文本

以缩写形式显示*查询文件,必须是原始文本,而不是tsvector.在匹配到查询之前,文档中的单词将根据指定或默认配置进行规范化。中讨论了此函数的使用第12.3.4节,它还描述了可用的选项*.

标题(‘肥猫吃老鼠’,‘猫’)肥猫吃掉了老鼠。
标题 ( [ 配置 regconfig, ] 文件 json, 查询 tsquery [, 选项 文本 ] ) → 文本

标题 ( [ 配置 regconfig, ] 文件 jsonb, 查询 tsquery [, 选项 文本 ] ) → 文本

以缩写形式显示与*查询出现在JSON中的字符串值中文件*看见第12.3.4节更多细节。

ts_标题(“{”cat:“倾盆大雨的猫和狗“}”)::jsonb,'cat'){“猫”:“猫和狗”}
T_秩 ( [ 砝码 真实的, ] 矢量 tsvector, 查询 tsquery [, 规范化 整数 ] ) → 真实的

计算一个分数,显示*矢量匹配查询*看见第12.3.3节详细信息。

T_rank(to_tsvector(“雨中的猫和狗”),“猫”)0.06079271
T_rank_cd ( [ 砝码 真实的, ] 矢量 tsvector, 查询 tsquery [, 规范化 整数 ] ) → 真实的

计算一个分数,显示*矢量匹配查询*,使用覆盖密度算法。看见第12.3.3节详细信息。

ts_rank_cd(to_tsvector(‘雨中的猫和狗’),‘猫’)0.1
重写 ( 查询 tsquery, 目标 tsquery, 代替 tsquery ) → tsquery

替换出现的*目标具有代替查询*看见第12.4.2.1节详细信息。

t|u rewrite('a&b'::tsquery,'a'::tsquery,'foo | bar'::tsquery)“b”和(“foo”|“bar”)
重写 ( 查询 tsquery, 选择 文本 ) → tsquery

替换部分*查询*根据通过执行选择命令看见第12.4.2.1节详细信息。

选择t_rewrite('a&b'::tsquery,'SELECT t,s FROM alias')“b”和(“foo”|“bar”)
Tsu_短语 ( 问题1 tsquery, 问题2 tsquery ) → tsquery

构造一个短语查询,用于搜索*问题1问题2*在连续的词素中(与<->接线员)。

短语(to_tsquery('fat')、to_tsquery('cat'))“胖”<->“猫”
Tsu_短语 ( 问题1 tsquery, 问题2 tsquery, 距离 整数 ) → tsquery

构造一个短语查询,用于搜索*问题1问题2这种情况确实发生了距离*词素分离。

短语(to_tsquery('fat')、to_tsquery('cat')、10)“肥猫”
TSU-to-U阵列 ( tsvector ) → 文本[]

皈依tsvector到一系列词汇表。

tsvector_to_数组('fat:2,4 cat:3 rat:5A'::tsvector){猫、肥、鼠}
不安 ( tsvector ) → 一套记录 ( 词素 文本, 位置 smallint[], 砝码 文本 )

扩展tsvector分成一组行,每个词素一行。

从unnest中选择*('cat:3脂肪:2,4大鼠:5A'::tsvector) → ``

```
词素
位置 砝码
--------+-----------+---------
{3} {D}
脂肪
{2,4} {D,D}
老鼠
{5} {A}

```

# 笔记

所有接受可选文本的文本搜索功能regconfig参数将使用违约_文本_搜索_配置当这个论点被省略时。

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

表9.43.文本搜索调试功能

作用

描述

例子
调试 ( [ 配置 regconfig, ] 文件 文本 ) → 一套记录 ( 别名 文本,描述 文本,令牌 文本,字典 字典[],字典 规范的,词位 文本[])

从*文档*根据指定的或默认的文本搜索配置,并返回有关如何处理每个令牌的信息。看第 12.8.1 节详情。

ts_debug('english', '最亮的超新星')(asciiword,"Word, all ASCII",The,{english_stem},english_stem,{}) ...
ts_lexize(听写 规范的,令牌 文本) →文本[]

如果字典已知输入标记,则返回替换词位数组;如果字典已知标记但它是停用词,则返回空数组;如果不是已知词,则返回 NULL。看第 12.8.3 节详情。

ts_lexize('english_stem', '星星'){星星}
ts_parse(解析器名称 文本,文档 文本) →记录集(小孩子 整数,令牌 文本)

从*文档*使用命名解析器。看第 12.8.2 节详情。

ts_parse('default', 'foo - bar')(1,富) ...
ts_parse(parser_oid 样的,文档 文本) →记录集(小孩子 整数,令牌 文本)

从*文档*使用由 OID 指定的解析器。看第 12.8.2 节详情。

ts_parse(3722, 'foo - bar')(1,富) ...
ts_token_type(解析器名称 文本) →记录集(小孩子 整数,别名 文本,描述 文本)

返回一个表,该表描述了命名解析器可以识别的每种类型的标记。看第 12.8.2 节详情。

ts_token_type('默认')(1,asciiword,"Word, all ASCII") ...
ts_token_type(parser_oid 样的) →记录集(小孩子 整数,别名 文本,描述 文本)

返回一个表,该表描述了由 OID 指定的解析器可以识别的每种类型的标记。看第 12.8.2 节详情。

ts_token_type(3722)(1,asciiword,"Word, all ASCII") ...
ts_stat(查询 文本 [,权重*文本]) →记录集(单词* 文本,ndoc 整数,入门 整数)

执行*查询*,它必须返回一个向量列,并返回有关数据中包含的每个不同词位的统计信息。看第 12.4.4 节详情。

ts_stat('从 apod 中选择向量')(foo,10,15) ...