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

# 12.5. 剖析器 \#

### 12.5. 剖析器 [#](#textsearch-parsers) <a href="#textsearch-parsers" id="textsearch-parsers"></a>

文字搜尋剖析器負責將原始文件文字切分為*語彙單元*（token），並識別每個語彙單元的類型，而可能的類型集合是由剖析器本身定義的。請注意，剖析器完全不會修改文字，它只是找出可能的單字邊界。由於職責範圍有限，針對特定應用自訂剖析器的需求，比自訂字典的需求要少。目前 PostgreSQL 只提供一個內建剖析器，而它已被證明適用於各式各樣的應用。

內建剖析器的名稱是 `pg_catalog.default`。它可以辨識 23 種語彙單元類型，如[表 12.1](#textsearch-parsers-table) 所示。

#### 表 12.1. 預設剖析器的語彙單元類型 [#](#textsearch-parsers-table) <a href="#textsearch-parsers-table" id="textsearch-parsers-table"></a>

| 別名                | 說明                   | 範例                                                 |
| ----------------- | -------------------- | -------------------------------------------------- |
| `asciiword`       | 單字，全為 ASCII 字母       | `elephant`                                         |
| `word`            | 單字，全為字母              | `mañana`                                           |
| `numword`         | 單字，包含字母與數字           | `beta1`                                            |
| `asciihword`      | 連字號複合詞，全為 ASCII      | `up-to-date`                                       |
| `hword`           | 連字號複合詞，全為字母          | `lógico-matemática`                                |
| `numhword`        | 連字號複合詞，包含字母與數字       | `postgresql-beta1`                                 |
| `hword_asciipart` | 連字號複合詞的組成部分，全為 ASCII | `postgresql` 於 `postgresql-beta1` 中的情境             |
| `hword_part`      | 連字號複合詞的組成部分，全為字母     | `lógico` 或 `matemática` 於 `lógico-matemática` 中的情境 |
| `hword_numpart`   | 連字號複合詞的組成部分，包含字母與數字  | `beta1` 於 `postgresql-beta1` 中的情境                  |
| `email`           | 電子郵件地址               | `foo@example.com`                                  |
| `protocol`        | 協定開頭                 | `http://`                                          |
| `url`             | URL                  | `example.com/stuff/index.html`                     |
| `host`            | 主機                   | `example.com`                                      |
| `url_path`        | URL 路徑               | `/stuff/index.html`，在 URL 的情境中                     |
| `file`            | 檔案或路徑名稱              | `/usr/local/foo.txt`，如果不在 URL 之中                   |
| `sfloat`          | 科學記號                 | `-1.234e56`                                        |
| `float`           | 十進位記號                | `-1.234`                                           |
| `int`             | 有號整數                 | `-1234`                                            |
| `uint`            | 無號整數                 | `1234`                                             |
| `version`         | 版本號碼                 | `8.3.0`                                            |
| `tag`             | XML 標籤               | `<a href="dictionaries.html">`                     |
| `entity`          | XML 實體               | `&amp;`                                            |
| `blank`           | 空白符號                 | （任何未被辨識為其他類型的空白或標點符號）                              |

<br>

#### 注意

剖析器對「字母」的認定是由資料庫的 locale 設定決定的，具體來說是 `lc_ctype`。只包含基本 ASCII 字母的單字會被回報為獨立的語彙單元類型，因為有時區分它們很有用。在大多數歐洲語言中，語彙單元類型 `word` 與 `asciiword` 應該以相同方式處理。

`email` 並不支援 [RFC 5322](https://datatracker.ietf.org/doc/html/rfc5322) 所定義的所有有效電子郵件字元。具體來說，電子郵件使用者名稱中唯一支援的非英數字元是句點、破折號與底線。

`tag` 並不支援 [W3C Recommendation, XML](https://www.w3.org/TR/xml/) 所定義的所有有效標籤名稱。具體來說，只支援以 ASCII 字母、底線或冒號開頭，且只包含字母、數字、連字號、底線、句點與冒號的標籤名稱。`tag` 也包含以 `<!--` 開頭、以 `-->` 結尾的 XML 註解，以及 XML 宣告（但請注意，這包括任何以 `<?x` 開頭、以 `>` 結尾的內容）。

剖析器有可能從同一段文字產生重疊的語彙單元。例如，連字號複合詞會同時被回報為整個單字以及其中的每個組成部分：

```

SELECT alias, description, token FROM ts_debug('foo-bar-beta1');
      alias      |               description                |     token
-----------------+------------------------------------------+---------------
 numhword        | Hyphenated word, letters and digits      | foo-bar-beta1
 hword_asciipart | Hyphenated word part, all ASCII          | foo
 blank           | Space symbols                            | -
 hword_asciipart | Hyphenated word part, all ASCII          | bar
 blank           | Space symbols                            | -
 hword_numpart   | Hyphenated word part, letters and digits | beta1
```

這種行為是理想的，因為它讓搜尋對整個複合詞以及其組成部分都能運作。以下是另一個具有啟發性的範例：

```

SELECT alias, description, token FROM ts_debug('http://example.com/stuff/index.html');
  alias   |  description  |            token
----------+---------------+------------------------------
 protocol | Protocol head | http://
 url      | URL           | example.com/stuff/index.html
 host     | Host          | example.com
 url_path | URL path      | /stuff/index.html
```

***

原文：[PostgreSQL 18.6 Documentation](https://www.postgresql.org/docs/18/textsearch-parsers.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/textsearch/textsearch-parsers.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.
