# 12.4.附加功能
本节介绍与文本搜索相关的其他功能和运算符。
# 12.4.1.操纵文件
第12.3.1节演示了如何将原始文本文档转换为tsvector
价值观PostgreSQL还提供了一些函数和运算符,可以用来操作已经存在的文档tsvector
类型
这个tsvector
串联运算符返回一个向量,该向量结合了作为参数给出的两个向量的词素和位置信息。位置和权重标签在连接过程中保留。出现在右向量中的位置被左向量中提到的最大位置偏移,因此结果几乎等同于执行的结果到_tsvector
两个原始文档字符串的串联。(等价性并不精确,因为从左手论证结尾移除的任何停止词都不会影响结果,而如果使用文本连接,它们会影响右手论证中词素的位置。)
在向量形式中使用串联的一个优点,而不是在应用之前串联文本到_tsvector
,即您可以使用不同的配置来解析文档的不同部分。还有,因为设定重量
函数以相同的方式标记给定向量的所有词素,需要解析文本并执行设定重量
如果要使用不同的权重标记文档的不同部分,则在连接之前。
设定重量(*
矢量*
tsvector, *
重量*
“char”)返回
tsvector``
设定重量
返回输入向量的副本,其中每个位置都用给定的*重量
*任何一个A.
, B
, C
或D
. (D
是新向量的默认值,因此不会显示在输出上。)当向量被连接时,这些标签会被保留,从而允许来自文档不同部分的单词通过排序函数进行不同的加权。
请注意,重量标签适用于位置不词位.如果输入向量已去除位置,则设定重量
什么都不做。
返回向量中存储的词素数。
返回一个向量,该向量列出与给定向量相同的词素,但缺少任何位置或权重信息。结果通常比未压缩向量小得多,但也不太有用。相关性排序在剥离向量上的效果不如在非剥离向量上好。还有<->
(紧跟其后)tsquery
运算符永远不会匹配剥离输入,因为它无法确定词素出现之间的距离。
完整的清单tsvector
-相关功能可在表9.42.
# 12.4.2.操纵查询
第12.3.2节演示了如何将原始文本查询转换为tsquery
价值观PostgreSQL还提供了一些函数和运算符,可用于操作已在中的查询tsquery
类型
tsquery`&&`tsquery
返回两个给定查询的和组合。
tsquery`| |`tsquery
返回两个给定查询的或组合。
!!
tsquery``
返回给定查询的否定(NOT)。
tsquery`<->`tsquery
返回一个查询,该查询使用<->
(紧跟其后)tsquery
操作人员例如:
SELECT to_tsquery('fat') <-> to_tsquery('cat | rat');
?column?
#### 12.4.2.1. Query Rewriting
[]()
The `ts_rewrite` family of functions search a given `tsquery` for occurrences of a target subquery, and replace each occurrence with a substitute subquery. In essence this operation is a `tsquery`-specific version of substring replacement. A target and substitute combination can be thought of as a *query rewrite rule*. A collection of such rewrite rules can be a powerful search aid. For example, you can expand the search using synonyms (e.g., `new york`, `big apple`, `nyc`, `gotham`) or narrow the search to direct the user to some hot topic. There is some overlap in functionality between this feature and thesaurus dictionaries ([Section 12.6.4](textsearch-dictionaries.html#TEXTSEARCH-THESAURUS)). However, you can modify a set of rewrite rules on-the-fly without reindexing, whereas updating a thesaurus requires reindexing to be effective.
`ts_rewrite (*`query`* `tsquery`, *`target`* `tsquery`, *`substitute`* `tsquery`) returns `tsquery``
This form of `ts_rewrite` simply applies a single rewrite rule: *`target`* is replaced by *`substitute`* wherever it appears in *`query`*. For example:
选择ts_rewrite('a&b':tsquery,'a'::tsquery,'c'::tsquery);重写
# 12.4.3.自动更新的触发器
# 笔记
本节中描述的方法已因使用存储的生成列而过时,如中所述第12.2.2节.
当使用单独的列存储tsvector
表示文档时,有必要创建一个触发器来更新tsvector
当文档内容列发生更改时。有两个内置的触发函数可用于此操作,也可以自己编写。
tsvector_update_trigger(tsvector_column_name, config_name, text_column_name [, ... ])
tsvector_update_trigger_column(tsvector_column_name, config_column_name, text_column_name [, ... ])
这些触发函数会自动计算tsvector
一个或多个文本列中的列,在创建触发器
命令使用它们的一个例子是:
CREATE TABLE messages (
title text,
body text,
tsv tsvector
);
CREATE TRIGGER tsvectorupdate BEFORE INSERT OR UPDATE
ON messages FOR EACH ROW EXECUTE FUNCTION
tsvector_update_trigger(tsv, 'pg_catalog.english', title, body);
INSERT INTO messages VALUES('title here', 'the body text is here');
SELECT * FROM messages;
title | body | tsv
### 12.4.4. Gathering Document Statistics
[]()
The function `ts_stat` is useful for checking your configuration and for finding stop-word candidates.
ts_stat(sqlquery文本,[权重文本,,]OUT word text、OUT ndoc integer、OUT ENTERY integer)返回记录集
*`sqlquery`* is a text value containing an SQL query which must return a single `tsvector` column. `ts_stat` executes the query and returns statistics about each distinct lexeme (word) contained in the `tsvector` data. The columns returned are
* *`word`* `text` — the value of a lexeme
* *`ndoc`* `integer` — number of documents (`tsvector`s) the word occurred in
* *`nentry`* `integer` — total number of occurrences of the word
If *`weights`* is supplied, only occurrences having one of those weights are counted.
For example, to find the ten most frequent words in a document collection:
按ENTRY DESC、ndoc DESC、字数限制10从ts_stat(“从apod中选择向量”)顺序选择*;
The same, but counting only word occurrences with weight `A` or `B`:
按ENTRY DESC、ndoc DESC、字数限制10的顺序从ts_stat中选择*('SELECT vector FROM apod'、'ab');