# F.33.pg_trgm

F.33.1.三角图(或三角图)概念F.33.2.函数和运算符F.33.3.GUC参数F.33.4.索引支持F.33.5.文本搜索集成F.33.6.工具书类F.33.7.作者

这个pg_trgm该模块提供了基于三元匹配确定字母数字文本相似性的函数和运算符,以及支持快速搜索相似字符串的索引运算符类。

该模块被认为是“受信任的”,也就是说,它可以由拥有创造当前数据库的权限。

# F.33.1.三角图(或三角图)概念

三元图是从字符串中提取的三个连续字符组成的一组。我们可以通过计算两个字符串共享的三元数来衡量它们的相似性。事实证明,这个简单的想法对于衡量许多自然语言中单词的相似性非常有效。

# 笔记

pg_trgm从字符串中提取三角形时忽略非单词字符(非字母数字)。在确定字符串中包含的一组三角形时,每个单词被认为有两个空格前缀和一个空格后缀。例如,字符串中的一组三角形““是”c”, “ca”, ““和””. “字符串中的一组三角形”富吧“是”f”, “法罗群岛”, “”, “面向对象”, “b”, “文学士”, “酒吧“和”应收账”.

# F.33.2.函数和运算符

pg_trgm模块如所示表F.25中的操作员表F.26.

表F.25. pg_trgm功能

作用

描述
相似性 ( 文本, 文本 ) → 真实的

返回一个数字,该数字指示这两个参数的相似程度。结果的范围是0(表示两个字符串完全不同)到1(表示两个字符串相同)。
show_trgm ( 文本 ) → 文本[]

返回给定字符串中所有三角形的数组。(实际上,除了调试之外,这很少有用。)
单词相似性 ( 文本, 文本 ) → 真实的

返回一个数字,该数字指示第一个字符串中的一组三角形与第二个字符串中有序三角形的任何连续范围之间的最大相似性。有关详细信息,请参见下面的说明。
严格的单词相似性 ( 文本, 文本 ) → 真实的

等同于单词相似性,但强制范围边界与单词边界匹配。由于我们没有交叉单词三角形,这个函数实际上返回第一个字符串和第二个字符串中任何连续的单词范围之间的最大相似性。
显示限制 () → 真实的

返回用户使用的当前相似性阈值%操作人员例如,这设置了两个单词之间的最小相似性,以使它们被视为相似到足以构成拼写错误。(反对; 而是使用显示 pg_trgm。相似性阈值.)
设定上限 ( 真实的 ) → 真实的

设置应用程序使用的当前相似性阈值%操作人员阈值必须介于0和1之间(默认值为0.3)。返回传入的相同值。(反对; 而是使用设置 pg_trgm。相似性阈值.)

考虑下面的例子:

# SELECT word_similarity('word', 'two words');
 word_similarity
# SELECT strict_word_similarity('word', 'two words'), similarity('word', 'words');
 strict_word_similarity | similarity
### F.33.3. GUC Parameters

`pg_trgm.similarity_threshold` (`real`) []()

 Sets the current similarity threshold that is used by the `%` operator. The threshold must be between 0 and 1 (default is 0.3).

`pg_trgm.word_similarity_threshold` (`real`) []()

 Sets the current word similarity threshold that is used by the `<%` and `%>` operators. The threshold must be between 0 and 1 (default is 0.6).

`pg_trgm.strict_word_similarity_threshold` (`real`) []()

 Sets the current strict word similarity threshold that is used by the `<<%` and `%>>` operators. The threshold must be between 0 and 1 (default is 0.5).

### F.33.4. Index Support

 The `pg_trgm` module provides GiST and GIN index operator classes that allow you to create an index over a text column for the purpose of very fast similarity searches. These index types support the above-described similarity operators, and additionally support trigram-based index searches for `LIKE`, `ILIKE`, `~`, `~*` and `=` queries. Inequality operators are not supported. Note that those indexes may not be as efficient as regular B-tree indexes for equality operator.

 Example:

创建表格test_trgm(t text);使用GIST(t GIST_trgm_ops)在测试_trgm上创建索引trgm_idx;

or

使用GIN(t GIN_trgm_ops)在test_trgm上创建索引trgm_idx;

`gist_trgm_ops` GiST opclass approximates a set of trigrams as a bitmap signature. Its optional integer parameter `siglen` determines the signature length in bytes. The default length is 12 bytes. Valid values of signature length are between 1 and 2024 bytes. Longer signatures lead to a more precise search (scanning a smaller fraction of the index and fewer heap pages), at the cost of a larger index.

 Example of creating such an index with a signature length of 32 bytes:

使用GIST(t GIST_trgm_ops(siglen=32))在test_trgm上创建索引trgm_idx;

 At this point, you will have an index on the `t` column that you can use for similarity searching. A typical query is

从测试中选择t,相似性(t,'单词')作为sml,其中t%的“单词”顺序由sml DESC,t决定;

 This will return all values in the text column that are sufficiently similar to *`word`*, sorted from best match to worst. The index will be used to make this a fast operation even over very large data sets.

 A variant of the above query is

按距离限制10从测试顺序中选择t,t<->“word”作为距离;

 This can be implemented quite efficiently by GiST indexes, but not by GIN indexes. It will usually beat the first formulation when only a small number of the closest matches is wanted.

 Also you can use an index on the `t` column for word similarity or strict word similarity. Typical queries are:

从测试中选择t,单词相似性('word',t)作为sml,其中'word'<%t按sml描述,t排序;

 and

从测试中选择t,严格的单词相似性('word',t)作为sml,其中'word'<<%t按sml描述,t排序;

 This will return all values in the text column for which there is a continuous extent in the corresponding ordered trigram set that is sufficiently similar to the trigram set of *`word`*, sorted from best match to worst. The index will be used to make this a fast operation even over very large data sets.

 Possible variants of the above queries are:

按距离限制10从测试顺序中选择t,‘word’<<->t作为距离;

 and

按距离限制10从测试顺序中选择t,‘word’作为距离;

 This can be implemented quite efficiently by GiST indexes, but not by GIN indexes.

 Beginning in PostgreSQL 9.1, these index types also support index searches for `LIKE` and `ILIKE`, for example

从test_trgm中选择*,其中不喜欢“%foo%bar”;

 The index search works by extracting trigrams from the search string and then looking these up in the index. The more trigrams in the search string, the more effective the index search is. Unlike B-tree based searches, the search string need not be left-anchored.

 Beginning in PostgreSQL 9.3, these index types also support index searches for regular-expression matches (`~` and `~*` operators), for example

从测试_trgm中选择*,其中t~'(foo | bar)';

 The index search works by extracting trigrams from the regular expression and then looking these up in the index. The more trigrams that can be extracted from the regular expression, the more effective the index search is. Unlike B-tree based searches, the search string need not be left-anchored.

 For both `LIKE` and regular-expression searches, keep in mind that a pattern with no extractable trigrams will degenerate to a full-index scan.

 The choice between GiST and GIN indexing depends on the relative performance characteristics of GiST and GIN, which are discussed elsewhere.

### F.33.5. Text Search Integration

 Trigram matching is a very useful tool when used in conjunction with a full text index. In particular it can help to recognize misspelled input words that will not be matched directly by the full text search mechanism.

 The first step is to generate an auxiliary table containing all the unique words in the documents:

创建表格单词作为从ts_stat中选择单词(“从文档中选择到_tsvector”(“简单”,bodytext));

 where `documents` is a table that has a text field `bodytext` that we wish to search. The reason for using the `simple` configuration with the `to_tsvector` function, instead of using a language-specific configuration, is that we want a list of the original (unstemmed) words.

 Next, create a trigram index on the word column:

使用GIN(word GIN\u trgm\u ops)在单词上创建索引单词;

 Now, a `SELECT` query similar to the previous example can be used to suggest spellings for misspelled words in user search terms. A useful extra test is to require that the selected words are also of similar length to the misspelled word.

### Note

 Since the `words` table has been generated as a separate, static table, it will need to be periodically regenerated so that it remains reasonably up-to-date with the document collection. Keeping it exactly current is usually unnecessary.

### F.33.6. References

 GiST Development Site [http://www.sai.msu.su/\~megera/postgres/gist/](http://www.sai.msu.su/~megera/postgres/gist/)

 Tsearch2 Development Site [http://www.sai.msu.su/\~megera/postgres/gist/tsearch/V2/](http://www.sai.msu.su/~megera/postgres/gist/tsearch/V2/)

### F.33.7. Authors

 Oleg Bartunov `<[oleg@sai.msu.su](mailto:oleg@sai.msu.su)>`, Moscow, Moscow University, Russia

 Teodor Sigaev `<[teodor@sigaev.ru](mailto:teodor@sigaev.ru)>`, Moscow, Delta-Soft Ltd.,Russia

 Alexander Korotkov `<[a.korotkov@postgrespro.ru](mailto:a.korotkov@postgrespro.ru)>`, Moscow, Postgres Professional, Russia

 Documentation: Christopher Kings-Lynne

 This module is sponsored by Delta-Soft Ltd., Moscow, Russia.