> For the complete documentation index, see [llms.txt](https://docs.postgresql.tw/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.postgresql.tw/19/appendixes/contrib/pgtrgm/pgtrgm-funcs-ops.md). Agents can also search and read this documentation through its [MCP server](https://docs.postgresql.tw/~gitbook/mcp).

# F.35.2. 函式與運算子

## F.35.2. 函式與運算子 [#](#pgtrgm-funcs-ops) <a href="#pgtrgm-funcs-ops" id="pgtrgm-funcs-ops"></a>

`pg_trgm` 模組提供的函式列於[表 F.26](#pgtrgm-func-table)，運算子則列於[表 F.27](#pgtrgm-op-table)。

### 表 F.26. `pg_trgm` 函式 [#](#pgtrgm-func-table) <a href="#pgtrgm-func-table" id="pgtrgm-func-table"></a>

| <p>函式</p><p>說明</p>                                                                                                                                                                                                                  |
| ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| <p><code>similarity</code> ( <code>text</code>, <code>text</code> ) → <code>real</code></p><p>傳回一個表示兩個引數相似程度的數字。結果的範圍從 0（表示兩個字串完全不相似）到 1（表示兩個字串完全相同）。</p>                                                                           |
| <p><code>show\_trgm</code> ( <code>text</code> ) → <code>text\[]</code></p><p>傳回給定字串中所有三元組所組成的陣列。（實務上，除了除錯之外很少用到。）</p>                                                                                                              |
| <p><code>word\_similarity</code> ( <code>text</code>, <code>text</code> ) → <code>real</code></p><p>傳回一個數字，表示第一個字串的三元組集合，與第二個字串的有序三元組集合中任一連續範圍之間的最大相似度。詳細說明請參閱下文。</p>                                                               |
| <p><code>strict\_word\_similarity</code> ( <code>text</code>, <code>text</code> ) → <code>real</code></p><p>與 <code>word\_similarity</code> 相同，但強制範圍邊界必須與單字邊界一致。由於我們沒有跨單字的三元組，此函式實際上傳回的是第一個字串與第二個字串中任一連續單字範圍之間的最大相似度。</p>           |
| <p><code>show\_limit</code> () → <code>real</code></p><p>傳回 <code>%</code> 運算子目前使用的相似度門檻。此值設定兩個單字之間的最小相似度，達到此值才會被視為足夠相似，例如可視為彼此的拼寫錯誤。（<em>已棄用</em>；請改用 <code>SHOW</code> <code>pg\_trgm.similarity\_threshold</code>。）</p>          |
| <p><code>set\_limit</code> ( <code>real</code> ) → <code>real</code></p><p>設定 <code>%</code> 運算子目前使用的相似度門檻。門檻必須介於 0 與 1 之間（預設為 0.3）。傳回所傳入的同一個值。（<em>已棄用</em>；請改用 <code>SET</code> <code>pg\_trgm.similarity\_threshold</code>。）</p> |

<br>

請看以下範例：

```

# SELECT word_similarity('word', 'two words');
 word_similarity
-----------------
             0.8
(1 row)
```

在第一個字串中，三元組集合為 `{" w"," wo","wor","ord","rd "}`。 在第二個字串中，有序三元組集合為 `{" t"," tw","two","wo "," w"," wo","wor","ord","rds","ds "}`。 第二個字串中最相似的有序三元組集合範圍是 `{" w"," wo","wor","ord"}`，相似度為 `0.8`。

此函式傳回的值，可大致理解為第一個字串與第二個字串任一子字串之間的最大相似度。不過，此函式不會在範圍的邊界加上填補。因此，除了不相符的單字邊界之外，第二個字串中額外出現的字元數量不會納入考量。

另一方面，`strict_word_similarity` 會在第二個字串中選取一個單字範圍。在上例中，`strict_word_similarity` 會選取單一單字 `'words'` 的範圍，其三元組集合為 `{" w"," wo","wor","ord","rds","ds "}`。

```

# SELECT strict_word_similarity('word', 'two words'), similarity('word', 'words');
 strict_word_similarity | similarity
------------------------+------------
               0.571429 |   0.571429
(1 row)
```

因此，`strict_word_similarity` 函式適用於找出與完整單字的相似度，而 `word_similarity` 較適合找出與單字局部的相似度。

### 表 F.27. `pg_trgm` 運算子 [#](#pgtrgm-op-table) <a href="#pgtrgm-op-table" id="pgtrgm-op-table"></a>

| <p>運算子</p><p>說明</p>                                                                                                                                                                                                                                  |
| ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| <p><code>text</code> <code>%</code> <code>text</code> → <code>boolean</code></p><p>傳回 <code>true</code> 的條件是：兩個引數的相似度大於由 <code>pg\_trgm.similarity\_threshold</code> 所設定的目前相似度門檻。</p>                                                                |
| <p><code>text</code> <code><%</code> <code>text</code> → <code>boolean</code></p><p>傳回 <code>true</code> 的條件是：第一個引數中的三元組集合，與第二個引數中有序三元組集合的某個連續範圍之間的相似度，大於由 <code>pg\_trgm.word\_similarity\_threshold</code> 參數所設定的目前單字相似度門檻。</p>                    |
| <p><code>text</code> <code>%></code> <code>text</code> → <code>boolean</code></p><p><code><%</code> 運算子的交換運算子。</p>                                                                                                                                   |
| <p><code>text</code> <code><<%</code> <code>text</code> → <code>boolean</code></p><p>傳回 <code>true</code> 的條件是：第二個引數中有一個符合單字邊界的有序三元組集合連續範圍，且該範圍與第一個引數之三元組集合的相似度，大於由 <code>pg\_trgm.strict\_word\_similarity\_threshold</code> 參數所設定的目前嚴格單字相似度門檻。</p> |
| <p><code>text</code> <code>%>></code> <code>text</code> → <code>boolean</code></p><p><code><<%</code> 運算子的交換運算子。</p>                                                                                                                                 |
| <p><code>text</code> <code><-></code> <code>text</code> → <code>real</code></p><p>傳回兩個引數之間的“距離”，也就是 1 減去 <code>similarity()</code> 的值。</p>                                                                                                           |
| <p><code>text</code> <code><<-></code> <code>text</code> → <code>real</code></p><p>傳回兩個引數之間的“距離”，也就是 1 減去 <code>word\_similarity()</code> 的值。</p>                                                                                                    |
| <p><code>text</code> <code><->></code> <code>text</code> → <code>real</code></p><p><code><<-></code> 運算子的交換運算子。</p>                                                                                                                                  |
| <p><code>text</code> <code><<<-></code> <code>text</code> → <code>real</code></p><p>傳回兩個引數之間的“距離”，也就是 1 減去 <code>strict\_word\_similarity()</code> 的值。</p>                                                                                           |
| <p><code>text</code> <code><->>></code> <code>text</code> → <code>real</code></p><p><code><<<-></code> 運算子的交換運算子。</p>                                                                                                                                |

<br>

譯註：上表說明 %、<%、<<% 等運算子在相似度「大於」目前門檻時傳回 true；但依 PostgreSQL 18 原始碼（contrib/pg\_trgm/trgm\_op.c），實際判斷條件是相似度「大於或等於」門檻。

[返回 F.35. pg\_trgm — 支援以三元組比對文字相似度](https://docs.postgresql.tw/19/appendixes/contrib/pgtrgm.md#pgtrgm)

***

原文：[PostgreSQL 18.6 Documentation](https://www.postgresql.org/docs/18/pgtrgm.html)（原文版本：18.6；核對日期：2026-10-23）


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.postgresql.tw/19/appendixes/contrib/pgtrgm/pgtrgm-funcs-ops.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
