> 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/the-sql-language/functions/functions-textsearch.md). Agents can also search and read this documentation through its [MCP server](https://docs.postgresql.tw/~gitbook/mcp).

# 9.13. 文字搜尋函式與運算子 \#

### 9.13. 文字搜尋函式與運算子 [#](#functions-textsearch) <a href="#functions-textsearch" id="functions-textsearch"></a>

[表 9.42](#textsearch-operators-table)、[表 9.43](#textsearch-functions-table) 與[表 9.44](#textsearch-functions-debug-table) 彙整了為全文檢索所提供的函式與運算子。關於 PostgreSQL 文字搜尋功能的詳細說明，請參閱[第 12 章](https://docs.postgresql.tw/19/the-sql-language/textsearch.md)。

#### 表 9.42. 文字搜尋運算子 [#](#textsearch-operators-table) <a href="#textsearch-operators-table" id="textsearch-operators-table"></a>

| <p>運算子</p><p>說明</p><p>範例</p>                                                                                                                                                                                                                                                                                                                              |                                                        |                                                                                                                                                      |               |                                                                             |                                     |       |                  |
| --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------ | ---------------------------------------------------------------------------------------------------------------------------------------------------- | ------------- | --------------------------------------------------------------------------- | ----------------------------------- | ----- | ---------------- |
| <p><code>tsvector</code> <code>@@</code> <code>tsquery</code> → <code>boolean</code></p><p><code>tsquery</code> <code>@@</code> <code>tsvector</code> → <code>boolean</code></p><p><code>tsvector</code> 是否與 <code>tsquery</code> 相符？（引數可以用任何順序給定。）</p><p><code>to\_tsvector('fat cats ate rats') @@ to\_tsquery('cat & rat')</code> → <code>t</code></p> |                                                        |                                                                                                                                                      |               |                                                                             |                                     |       |                  |
| <p><code>text</code> <code>@@</code> <code>tsquery</code> → <code>boolean</code></p><p>文字字串在隱含地呼叫 <code>to\_tsvector()</code> 之後，是否與 <code>tsquery</code> 相符？</p><p><code>'fat cats ate rats' @@ to\_tsquery('cat & rat')</code> → <code>t</code></p>                                                                                                     |                                                        |                                                                                                                                                      |               |                                                                             |                                     |       |                  |
| <p><code>tsvector</code> <code>                                                                                                                                                                                                                                                                                                                           |                                                        | </code> <code>tsvector</code> → <code>tsvector</code></p><p>串接兩個 <code>tsvector</code>。如果兩個輸入都包含詞素位置，第二個輸入的位置會相應地調整。</p><p><code>'a:1 b:2'::tsvector |               | 'c:1 d:2 b:3'::tsvector</code> → <code>'a':1 'b':2,5 'c':3 'd':4</code></p> |                                     |       |                  |
| <p><code>tsquery</code> <code>&&</code> <code>tsquery</code> → <code>tsquery</code></p><p>將兩個 <code>tsquery</code> 以 AND 結合，產生一個與同時符合兩個輸入查詢之文件相符的查詢。</p><p><code>'fat                                                                                                                                                                                     | rat'::tsquery && 'cat'::tsquery</code> → <code>( 'fat' | 'rat' ) & 'cat'</code></p>                                                                                                                           |               |                                                                             |                                     |       |                  |
| <p><code>tsquery</code> <code>                                                                                                                                                                                                                                                                                                                            |                                                        | </code> <code>tsquery</code> → <code>tsquery</code></p><p>將兩個 <code>tsquery</code> 以 OR 結合，產生一個與符合任一輸入查詢之文件相符的查詢。</p><p><code>'fat                   | rat'::tsquery |                                                                             | 'cat'::tsquery</code> → <code>'fat' | 'rat' | 'cat'</code></p> |
| <p><code>!!</code> <code>tsquery</code> → <code>tsquery</code></p><p>否定一個 <code>tsquery</code>，產生一個與不符合輸入查詢之文件相符的查詢。</p><p><code>!! 'cat'::tsquery</code> → <code>!'cat'</code></p>                                                                                                                                                                       |                                                        |                                                                                                                                                      |               |                                                                             |                                     |       |                  |
| <p><code>tsquery</code> <code><-></code> <code>tsquery</code> → <code>tsquery</code></p><p>建構一個片語查詢，當兩個輸入查詢在連續的詞素上相符時，它就相符。</p><p><code>to\_tsquery('fat') <-> to\_tsquery('rat')</code> → <code>'fat' <-> 'rat'</code></p>                                                                                                                               |                                                        |                                                                                                                                                      |               |                                                                             |                                     |       |                  |
| <p><code>tsquery</code> <code>@></code> <code>tsquery</code> → <code>boolean</code></p><p>第一個 <code>tsquery</code> 是否包含第二個？（這只考慮出現在一個查詢中的所有詞素是否都出現在另一個查詢中，而忽略組合運算子。）</p><p><code>'cat'::tsquery @> 'cat & rat'::tsquery</code> → <code>f</code></p>                                                                                                       |                                                        |                                                                                                                                                      |               |                                                                             |                                     |       |                  |
| <p><code>tsquery</code> <code><@</code> <code>tsquery</code> → <code>boolean</code></p><p>第一個 <code>tsquery</code> 是否被第二個包含？（這只考慮出現在一個查詢中的所有詞素是否都出現在另一個查詢中，而忽略組合運算子。）</p><p><code>'cat'::tsquery <@ 'cat & rat'::tsquery</code> → <code>t</code></p><p><code>'cat'::tsquery <@ '!cat & rat'::tsquery</code> → <code>t</code></p>                          |                                                        |                                                                                                                                                      |               |                                                                             |                                     |       |                  |

<br>

除了這些特殊運算子之外，`tsvector` 與 `tsquery` 型別也可以使用[表 9.1](https://docs.postgresql.tw/19/the-sql-language/functions/functions-comparison.md#functions-comparison-table) 所列的一般比較運算子。這些運算子對文字搜尋不是很有用，但它們讓你可以在這些型別的欄位上建立唯一索引等等。

#### 表 9.43. 文字搜尋函式 [#](#textsearch-functions-table) <a href="#textsearch-functions-table" id="textsearch-functions-table"></a>

<table data-header-hidden><thead><tr><th></th></tr></thead><tbody><tr><td><p>函式</p><p>說明</p><p>範例</p></td></tr><tr><td><p><code>array_to_tsvector</code> ( <code>text[]</code> ) → <code>tsvector</code></p><p>將文字字串陣列轉換為 <code>tsvector</code>。給定的字串會原樣用作詞素，不做進一步處理。陣列元素不得為空字串或 <code>NULL</code>。</p><p><code>array_to_tsvector('{fat,cat,rat}'::text[])</code> → <code>'cat' 'fat' 'rat'</code></p></td></tr><tr><td><p><code>get_current_ts_config</code> ( ) → <code>regconfig</code></p><p>回傳目前預設文字搜尋組態（由 <a href="https://docs.postgresql.tw/19/server-administration/runtime-config/runtime-config-client.md#GUC-DEFAULT-TEXT-SEARCH-CONFIG">default_text_search_config</a> 設定）的 OID。</p><p><code>get_current_ts_config()</code> → <code>english</code></p></td></tr><tr><td><p><code>length</code> ( <code>tsvector</code> ) → <code>integer</code></p><p>回傳 <code>tsvector</code> 中的詞素數量。</p><p><code>length('fat:2,4 cat:3 rat:5A'::tsvector)</code> → <code>3</code></p></td></tr><tr><td><p><code>numnode</code> ( <code>tsquery</code> ) → <code>integer</code></p><p>回傳 <code>tsquery</code> 中的詞素數量加上運算子數量。</p><p><code>numnode('(fat &#x26; rat) | cat'::tsquery)</code> → <code>5</code></p></td></tr><tr><td><p><code>plainto_tsquery</code> ( [ <em><code>config</code></em> <code>regconfig</code>, ] <em><code>query</code></em> <code>text</code> ) → <code>tsquery</code></p><p>將文字轉換為 <code>tsquery</code>，並依照指定的或預設的組態將單字正規化。字串中的任何標點符號都會被忽略（它不會決定查詢運算子）。產生的查詢會與包含該文字中所有非停用詞的文件相符。</p><p><code>plainto_tsquery('english', 'The Fat Rats')</code> → <code>'fat' &#x26; 'rat'</code></p></td></tr><tr><td><p><code>phraseto_tsquery</code> ( [ <em><code>config</code></em> <code>regconfig</code>, ] <em><code>query</code></em> <code>text</code> ) → <code>tsquery</code></p><p>將文字轉換為 <code>tsquery</code>，並依照指定的或預設的組態將單字正規化。字串中的任何標點符號都會被忽略（它不會決定查詢運算子）。產生的查詢會與包含該文字中所有非停用詞的片語相符。</p><p><code>phraseto_tsquery('english', 'The Fat Rats')</code> → <code>'fat' &#x3C;-> 'rat'</code></p><p><code>phraseto_tsquery('english', 'The Cat and Rats')</code> → <code>'cat' &#x3C;2> 'rat'</code></p></td></tr><tr><td><p><code>websearch_to_tsquery</code> ( [ <em><code>config</code></em> <code>regconfig</code>, ] <em><code>query</code></em> <code>text</code> ) → <code>tsquery</code></p><p>將文字轉換為 <code>tsquery</code>，並依照指定的或預設的組態將單字正規化。加上引號的單字序列會被轉換為片語測試。單字“or”會被理解為產生 OR 運算子，而破折號會產生 NOT 運算子；其他標點符號則會被忽略。這近似於一些常見網路搜尋工具的行為。</p><p><code>websearch_to_tsquery('english', '"fat rat" or cat dog')</code> → <code>'fat' &#x3C;-> 'rat' | 'cat' &#x26; 'dog'</code></p></td></tr><tr><td><p><code>querytree</code> ( <code>tsquery</code> ) → <code>text</code></p><p>產生 <code>tsquery</code> 中可索引部分的表示。結果為空或只有 <code>T</code> 時，表示該查詢無法使用索引。</p><p><code>querytree('foo &#x26; ! bar'::tsquery)</code> → <code>'foo'</code></p></td></tr><tr><td><p><code>setweight</code> ( <em><code>vector</code></em> <code>tsvector</code>, <em><code>weight</code></em> <code>"char"</code> ) → <code>tsvector</code></p><p>將指定的 <em><code>weight</code></em> 指派給 <em><code>vector</code></em> 的每個元素。</p><p><code>setweight('fat:2,4 cat:3 rat:5B'::tsvector, 'A')</code> → <code>'cat':3A 'fat':2A,4A 'rat':5A</code></p></td></tr><tr><td><p><code>setweight</code> ( <em><code>vector</code></em> <code>tsvector</code>, <em><code>weight</code></em> <code>"char"</code>, <em><code>lexemes</code></em> <code>text[]</code> ) → <code>tsvector</code></p><p>將指定的 <em><code>weight</code></em> 指派給 <em><code>vector</code></em> 中列在 <em><code>lexemes</code></em> 裡的元素。<em><code>lexemes</code></em> 中的字串會原樣作為詞素，不做進一步處理。與 <em><code>vector</code></em> 中任何詞素都不相符的字串會被忽略。</p><p><code>setweight('fat:2,4 cat:3 rat:5,6B'::tsvector, 'A', '{cat,rat}')</code> → <code>'cat':3A 'fat':2,4 'rat':5A,6A</code></p></td></tr><tr><td><p><code>strip</code> ( <code>tsvector</code> ) → <code>tsvector</code></p><p>從 <code>tsvector</code> 中移除位置與權重。</p><p><code>strip('fat:2,4 cat:3 rat:5A'::tsvector)</code> → <code>'cat' 'fat' 'rat'</code></p></td></tr><tr><td><p><code>to_tsquery</code> ( [ <em><code>config</code></em> <code>regconfig</code>, ] <em><code>query</code></em> <code>text</code> ) → <code>tsquery</code></p><p>將文字轉換為 <code>tsquery</code>，並依照指定的或預設的組態將單字正規化。單字必須以有效的 <code>tsquery</code> 運算子組合。</p><p><code>to_tsquery('english', 'The &#x26; Fat &#x26; Rats')</code> → <code>'fat' &#x26; 'rat'</code></p></td></tr><tr><td><p><code>to_tsvector</code> ( [ <em><code>config</code></em> <code>regconfig</code>, ] <em><code>document</code></em> <code>text</code> ) → <code>tsvector</code></p><p>將文字轉換為 <code>tsvector</code>，並依照指定的或預設的組態將單字正規化。結果中會包含位置資訊。</p><p><code>to_tsvector('english', 'The Fat Rats')</code> → <code>'fat':2 'rat':3</code></p></td></tr><tr><td><p><code>to_tsvector</code> ( [ <em><code>config</code></em> <code>regconfig</code>, ] <em><code>document</code></em> <code>json</code> ) → <code>tsvector</code></p><p><code>to_tsvector</code> ( [ <em><code>config</code></em> <code>regconfig</code>, ] <em><code>document</code></em> <code>jsonb</code> ) → <code>tsvector</code></p><p>將 JSON 文件中的每個字串值轉換為 <code>tsvector</code>，並依照指定的或預設的組態將單字正規化。接著依文件順序串接這些結果以產生輸出。產生位置資訊時，就好像每一對字串值之間存在一個停用詞。（請注意，當輸入為 <code>jsonb</code> 時，JSON 物件之欄位的“文件順序”取決於實作；請留意範例中的差異。）</p><p><code>to_tsvector('english', '{"aa": "The Fat Rats", "b": "dog"}'::json)</code> → <code>'dog':5 'fat':2 'rat':3</code></p><p><code>to_tsvector('english', '{"aa": "The Fat Rats", "b": "dog"}'::jsonb)</code> → <code>'dog':1 'fat':4 'rat':5</code></p></td></tr><tr><td><p><code>json_to_tsvector</code> ( [ <em><code>config</code></em> <code>regconfig</code>, ] <em><code>document</code></em> <code>json</code>, <em><code>filter</code></em> <code>jsonb</code> ) → <code>tsvector</code></p><p><code>jsonb_to_tsvector</code> ( [ <em><code>config</code></em> <code>regconfig</code>, ] <em><code>document</code></em> <code>jsonb</code>, <em><code>filter</code></em> <code>jsonb</code> ) → <code>tsvector</code></p><p>選取 JSON 文件中 <em><code>filter</code></em> 所要求的每個項目，並將每個項目轉換為 <code>tsvector</code>，依照指定的或預設的組態將單字正規化。接著依文件順序串接這些結果以產生輸出。產生位置資訊時，就好像每一對選取的項目之間存在一個停用詞。（請注意，當輸入為 <code>jsonb</code> 時，JSON 物件之欄位的“文件順序”取決於實作。）<em><code>filter</code></em> 必須是一個 <code>jsonb</code> 陣列，包含零個或多個下列關鍵字：<code>"string"</code>（包含所有字串值）、<code>"numeric"</code>（包含所有數值）、<code>"boolean"</code>（包含所有布林值）、<code>"key"</code>（包含所有鍵），或 <code>"all"</code>（包含以上全部）。作為特例，<em><code>filter</code></em> 也可以是屬於這些關鍵字之一的簡單 JSON 值。</p><p><code>json_to_tsvector('english', '{"a": "The Fat Rats", "b": 123}'::json, '["string", "numeric"]')</code> → <code>'123':5 'fat':2 'rat':3</code></p><p><code>json_to_tsvector('english', '{"cat": "The Fat Rats", "dog": 123}'::json, '"all"')</code> → <code>'123':9 'cat':1 'dog':7 'fat':4 'rat':5</code></p></td></tr><tr><td><p><code>ts_delete</code> ( <em><code>vector</code></em> <code>tsvector</code>, <em><code>lexeme</code></em> <code>text</code> ) → <code>tsvector</code></p><p>將所有出現的給定 <em><code>lexeme</code></em> 從 <em><code>vector</code></em> 中移除。<em><code>lexeme</code></em> 字串會原樣視為詞素，不做進一步處理。</p><p><code>ts_delete('fat:2,4 cat:3 rat:5A'::tsvector, 'fat')</code> → <code>'cat':3 'rat':5A</code></p></td></tr><tr><td><p><code>ts_delete</code> ( <em><code>vector</code></em> <code>tsvector</code>, <em><code>lexemes</code></em> <code>text[]</code> ) → <code>tsvector</code></p><p>將出現於 <em><code>lexemes</code></em> 中的詞素全部從 <em><code>vector</code></em> 中移除。<em><code>lexemes</code></em> 中的字串會原樣作為詞素，不做進一步處理。與 <em><code>vector</code></em> 中任何詞素都不相符的字串會被忽略。</p><p><code>ts_delete('fat:2,4 cat:3 rat:5A'::tsvector, ARRAY['fat','rat'])</code> → <code>'cat':3</code></p></td></tr><tr><td><p><code>ts_filter</code> ( <em><code>vector</code></em> <code>tsvector</code>, <em><code>weights</code></em> <code>"char"[]</code> ) → <code>tsvector</code></p><p>只選取具有給定 <em><code>weights</code></em> 的 <em><code>vector</code></em> 元素。</p><p><code>ts_filter('fat:2,4 cat:3b,7c rat:5A'::tsvector, '{a,b}')</code> → <code>'cat':3B 'rat':5A</code></p></td></tr><tr><td><p><code>ts_headline</code> ( [ <em><code>config</code></em> <code>regconfig</code>, ] <em><code>document</code></em> <code>text</code>, <em><code>query</code></em> <code>tsquery</code> [, <em><code>options</code></em> <code>text</code> ] ) → <code>text</code></p><p>以縮略的形式顯示 <em><code>query</code></em> 在 <em><code>document</code></em> 中的相符之處，其中文件必須是原始文字，而不是 <code>tsvector</code>。文件中的單字在與查詢比對之前，會依照指定的或預設的組態進行正規化。這個函式的用法在<a href="https://docs.postgresql.tw/19/the-sql-language/textsearch/textsearch-controls.md#textsearch-headline">第 12.3.4 節</a>中討論，該節也說明了可用的 <em><code>options</code></em>。</p><p><code>ts_headline('The fat cat ate the rat.', 'cat')</code> → <code>The fat &#x3C;b>cat&#x3C;/b> ate the rat.</code></p></td></tr><tr><td><p><code>ts_headline</code> ( [ <em><code>config</code></em> <code>regconfig</code>, ] <em><code>document</code></em> <code>json</code>, <em><code>query</code></em> <code>tsquery</code> [, <em><code>options</code></em> <code>text</code> ] ) → <code>text</code></p><p><code>ts_headline</code> ( [ <em><code>config</code></em> <code>regconfig</code>, ] <em><code>document</code></em> <code>jsonb</code>, <em><code>query</code></em> <code>tsquery</code> [, <em><code>options</code></em> <code>text</code> ] ) → <code>text</code></p><p>以縮略的形式顯示 <em><code>query</code></em> 在 JSON <em><code>document</code></em> 之字串值中的相符之處。更多細節請參閱<a href="https://docs.postgresql.tw/19/the-sql-language/textsearch/textsearch-controls.md#textsearch-headline">第 12.3.4 節</a>。</p><p><code>ts_headline('{"cat":"raining cats and dogs"}'::jsonb, 'cat')</code> → <code>{"cat": "raining &#x3C;b>cats&#x3C;/b> and dogs"}</code></p></td></tr><tr><td><p><code>ts_rank</code> ( [ <em><code>weights</code></em> <code>real[]</code>, ] <em><code>vector</code></em> <code>tsvector</code>, <em><code>query</code></em> <code>tsquery</code> [, <em><code>normalization</code></em> <code>integer</code> ] ) → <code>real</code></p><p>計算一個分數，顯示 <em><code>vector</code></em> 與 <em><code>query</code></em> 的相符程度。詳情請參閱<a href="https://docs.postgresql.tw/19/the-sql-language/textsearch/textsearch-controls.md#textsearch-ranking">第 12.3.3 節</a>。</p><p><code>ts_rank(to_tsvector('raining cats and dogs'), 'cat')</code> → <code>0.06079271</code></p></td></tr><tr><td><p><code>ts_rank_cd</code> ( [ <em><code>weights</code></em> <code>real[]</code>, ] <em><code>vector</code></em> <code>tsvector</code>, <em><code>query</code></em> <code>tsquery</code> [, <em><code>normalization</code></em> <code>integer</code> ] ) → <code>real</code></p><p>使用涵蓋密度（cover density）演算法，計算一個分數，顯示 <em><code>vector</code></em> 與 <em><code>query</code></em> 的相符程度。詳情請參閱<a href="https://docs.postgresql.tw/19/the-sql-language/textsearch/textsearch-controls.md#textsearch-ranking">第 12.3.3 節</a>。</p><p><code>ts_rank_cd(to_tsvector('raining cats and dogs'), 'cat')</code> → <code>0.1</code></p></td></tr><tr><td><p><code>ts_rewrite</code> ( <em><code>query</code></em> <code>tsquery</code>, <em><code>target</code></em> <code>tsquery</code>, <em><code>substitute</code></em> <code>tsquery</code> ) → <code>tsquery</code></p><p>將出現的 <em><code>target</code></em> 替換為 <em><code>substitute</code></em>，替換範圍限於 <em><code>query</code></em> 之內。詳情請參閱<a href="https://docs.postgresql.tw/19/the-sql-language/textsearch/textsearch-features.md#TEXTSEARCH-QUERY-REWRITING">第 12.4.2.1 節</a>。</p><p><code>ts_rewrite('a &#x26; b'::tsquery, 'a'::tsquery, 'foo|bar'::tsquery)</code> → <code>'b' &#x26; ( 'foo' | 'bar' )</code></p></td></tr><tr><td><p><code>ts_rewrite</code> ( <em><code>query</code></em> <code>tsquery</code>, <em><code>select</code></em> <code>text</code> ) → <code>tsquery</code></p><p>替換 <em><code>query</code></em> 的部分內容，依據的是執行 <code>SELECT</code> 命令所取得的目標與替代項目。詳情請參閱<a href="https://docs.postgresql.tw/19/the-sql-language/textsearch/textsearch-features.md#TEXTSEARCH-QUERY-REWRITING">第 12.4.2.1 節</a>。</p><p><code>SELECT ts_rewrite('a &#x26; b'::tsquery, 'SELECT t,s FROM aliases')</code> → <code>'b' &#x26; ( 'foo' | 'bar' )</code></p></td></tr><tr><td><p><code>tsquery_phrase</code> ( <em><code>query1</code></em> <code>tsquery</code>, <em><code>query2</code></em> <code>tsquery</code> ) → <code>tsquery</code></p><p>建構一個片語查詢，搜尋 <em><code>query1</code></em> 與 <em><code>query2</code></em> 在連續詞素上的相符（與 <code>&#x3C;-></code> 運算子相同）。</p><p><code>tsquery_phrase(to_tsquery('fat'), to_tsquery('cat'))</code> → <code>'fat' &#x3C;-> 'cat'</code></p></td></tr><tr><td><p><code>tsquery_phrase</code> ( <em><code>query1</code></em> <code>tsquery</code>, <em><code>query2</code></em> <code>tsquery</code>, <em><code>distance</code></em> <code>integer</code> ) → <code>tsquery</code></p><p>建構一個片語查詢，搜尋 <em><code>query1</code></em> 與 <em><code>query2</code></em> 恰好相隔 <em><code>distance</code></em> 個詞素出現的相符。</p><p><code>tsquery_phrase(to_tsquery('fat'), to_tsquery('cat'), 10)</code> → <code>'fat' &#x3C;10> 'cat'</code></p></td></tr><tr><td><p><code>tsvector_to_array</code> ( <code>tsvector</code> ) → <code>text[]</code></p><p>將 <code>tsvector</code> 轉換為詞素陣列。</p><p><code>tsvector_to_array('fat:2,4 cat:3 rat:5A'::tsvector)</code> → <code>{cat,fat,rat}</code></p></td></tr><tr><td><p><code>unnest</code> ( <code>tsvector</code> ) → <code>setof record</code> ( <em><code>lexeme</code></em> <code>text</code>, <em><code>positions</code></em> <code>smallint[]</code>, <em><code>weights</code></em> <code>text</code> )</p><p>將 <code>tsvector</code> 展開成一組資料列，每個詞素一筆。</p><p><code>select * from unnest('cat:3 fat:2,4 rat:5A'::tsvector)</code> →</p><pre><code> lexeme | positions | weights
--------+-----------+---------
 cat    | {3}       | {D}
 fat    | {2,4}     | {D,D}
 rat    | {5}       | {A}
</code></pre></td></tr></tbody></table>

<br>

#### 注意

所有接受選用 `regconfig` 引數的文字搜尋函式，在省略該引數時，都會使用 [default\_text\_search\_config](https://docs.postgresql.tw/19/server-administration/runtime-config/runtime-config-client.md#GUC-DEFAULT-TEXT-SEARCH-CONFIG) 所指定的組態。

[表 9.44](#textsearch-functions-debug-table) 中的函式之所以另外列出，是因為它們通常不會用在日常的文字搜尋操作中。它們主要有助於開發與除錯新的文字搜尋組態。

#### 表 9.44. 文字搜尋除錯函式 [#](#textsearch-functions-debug-table) <a href="#textsearch-functions-debug-table" id="textsearch-functions-debug-table"></a>

| <p>函式</p><p>說明</p><p>範例</p>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         |
| ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| <p><code>ts\_debug</code> ( \[ <em><code>config</code></em> <code>regconfig</code>, ] <em><code>document</code></em> <code>text</code> ) → <code>setof record</code> ( <em><code>alias</code></em> <code>text</code>, <em><code>description</code></em> <code>text</code>, <em><code>token</code></em> <code>text</code>, <em><code>dictionaries</code></em> <code>regdictionary\[]</code>, <em><code>dictionary</code></em> <code>regdictionary</code>, <em><code>lexemes</code></em> <code>text\[]</code> )</p><p>依照指定的或預設的文字搜尋組態，從 <em><code>document</code></em> 中擷取並正規化語彙單元，並回傳每個語彙單元如何被處理的資訊。詳情請參閱<a href="https://docs.postgresql.tw/19/the-sql-language/textsearch/textsearch-debugging.md#textsearch-configuration-testing">第 12.8.1 節</a>。</p><p><code>ts\_debug('english', 'The Brightest supernovaes')</code> → <code>(asciiword,"Word, all ASCII",The,{english\_stem},english\_stem,{}) ...</code></p> |
| <p><code>ts\_lexize</code> ( <em><code>dict</code></em> <code>regdictionary</code>, <em><code>token</code></em> <code>text</code> ) → <code>text\[]</code></p><p>如果字典認得輸入的語彙單元，就回傳替換詞素的陣列；如果字典認得該語彙單元但它是停用詞，就回傳空陣列；如果它不是已知的單字，則回傳 NULL。詳情請參閱<a href="https://docs.postgresql.tw/19/the-sql-language/textsearch/textsearch-debugging.md#textsearch-dictionary-testing">第 12.8.3 節</a>。</p><p><code>ts\_lexize('english\_stem', 'stars')</code> → <code>{star}</code></p>                                                                                                                                                                                                                                                                                                                                                                                                                                             |
| <p><code>ts\_parse</code> ( <em><code>parser\_name</code></em> <code>text</code>, <em><code>document</code></em> <code>text</code> ) → <code>setof record</code> ( <em><code>tokid</code></em> <code>integer</code>, <em><code>token</code></em> <code>text</code> )</p><p>使用指定名稱的剖析器，從 <em><code>document</code></em> 中擷取語彙單元。詳情請參閱<a href="https://docs.postgresql.tw/19/the-sql-language/textsearch/textsearch-debugging.md#textsearch-parser-testing">第 12.8.2 節</a>。</p><p><code>ts\_parse('default', 'foo - bar')</code> → <code>(1,foo) ...</code></p>                                                                                                                                                                                                                                                                                                                                                       |
| <p><code>ts\_parse</code> ( <em><code>parser\_oid</code></em> <code>oid</code>, <em><code>document</code></em> <code>text</code> ) → <code>setof record</code> ( <em><code>tokid</code></em> <code>integer</code>, <em><code>token</code></em> <code>text</code> )</p><p>使用以 OID 指定的剖析器，從 <em><code>document</code></em> 中擷取語彙單元。詳情請參閱<a href="https://docs.postgresql.tw/19/the-sql-language/textsearch/textsearch-debugging.md#textsearch-parser-testing">第 12.8.2 節</a>。</p><p><code>ts\_parse(3722, 'foo - bar')</code> → <code>(1,foo) ...</code></p>                                                                                                                                                                                                                                                                                                                                                          |
| <p><code>ts\_token\_type</code> ( <em><code>parser\_name</code></em> <code>text</code> ) → <code>setof record</code> ( <em><code>tokid</code></em> <code>integer</code>, <em><code>alias</code></em> <code>text</code>, <em><code>description</code></em> <code>text</code> )</p><p>回傳一個資料表，描述指定名稱的剖析器能夠辨識的每一種語彙單元類型。詳情請參閱<a href="https://docs.postgresql.tw/19/the-sql-language/textsearch/textsearch-debugging.md#textsearch-parser-testing">第 12.8.2 節</a>。</p><p><code>ts\_token\_type('default')</code> → <code>(1,asciiword,"Word, all ASCII") ...</code></p>                                                                                                                                                                                                                                                                                                                                                |
| <p><code>ts\_token\_type</code> ( <em><code>parser\_oid</code></em> <code>oid</code> ) → <code>setof record</code> ( <em><code>tokid</code></em> <code>integer</code>, <em><code>alias</code></em> <code>text</code>, <em><code>description</code></em> <code>text</code> )</p><p>回傳一個資料表，描述以 OID 指定的剖析器能夠辨識的每一種語彙單元類型。詳情請參閱<a href="https://docs.postgresql.tw/19/the-sql-language/textsearch/textsearch-debugging.md#textsearch-parser-testing">第 12.8.2 節</a>。</p><p><code>ts\_token\_type(3722)</code> → <code>(1,asciiword,"Word, all ASCII") ...</code></p>                                                                                                                                                                                                                                                                                                                                                   |
| <p><code>ts\_stat</code> ( <em><code>sqlquery</code></em> <code>text</code> \[, <em><code>weights</code></em> <code>text</code> ] ) → <code>setof record</code> ( <em><code>word</code></em> <code>text</code>, <em><code>ndoc</code></em> <code>integer</code>, <em><code>nentry</code></em> <code>integer</code> )</p><p>執行 <em><code>sqlquery</code></em>（它必須回傳單一 <code>tsvector</code> 欄位），並回傳資料中所包含之每個相異詞素的統計資訊。詳情請參閱<a href="https://docs.postgresql.tw/19/the-sql-language/textsearch/textsearch-features.md#textsearch-statistics">第 12.4.4 節</a>。</p><p><code>ts\_stat('SELECT vector FROM apod')</code> → <code>(foo,10,15) ...</code></p>                                                                                                                                                                                                                                                                |

<br>

***

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


---

# 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/the-sql-language/functions/functions-textsearch.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.
