# NGram Tokenizer

**ngram tokenizer** 遇到指定的字符（如 : 空白、标点）时分割文本，然后返回指定长度的每个单词的 [N-grams](https://en.wikipedia.org/wiki/N-gram)。

N-grams 就像一个滑动窗口在单词上移动，是一个连续的指定长度的字符序列。 通常用于查询不使用空格或具有较长复合词（如德语）的语言。

## **输出示例**

使用默认设置，**ngram tokenizer**将初始文本视为单个词元，并生成最小长度为1且最大长度为2的 N-gram：

```
POST _analyze
{
  "tokenizer": "ngram",
  "text": "Quick Fox"
}
```

上面的句子会生成如下的词元:

```
[ Q, Qu, u, ui, i, ic, c, ck, k, "k ", " ", " F", F, Fo, o, ox, x ]
```

## **配置**

| min\_gram    | 以 gram 为单位的最小字符长度。 默认为1。                                                                                                                                      |
| ------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| max\_gram    | 以 gram 为单位的最大字符长度。 默认为2。                                                                                                                                      |
| token\_chars | 应包含在词元中的字符类。 Elasticsearch将分割不属于指定类的字符。 默认为\[]（保留所有字符）。字符类可能是以下任何一种：   单词  - 例如a，b，ï或京    数字  - 例如3或7       空格  - 例如“”或“ n”    标点符号  - 例如！ 要么 ”   符号  - 例如$或√ |

> 提示:
>
> 通常，&#x5C06;***min\_gram***&#x548C;***max\_gram***&#x8BBE;为同样的值。值越小，匹配到的文档越多，但是匹配的质量越差。值越大，越能匹配到指定的文档。3 是一个不错的初始值。

## **配置示例**

下面的例子中，我们配置 **ngram tokenizer**处理单词和数字，生成 tri-grams (grams 为 `3`):

```
PUT my_index
{
  "settings": {
    "analysis": {
      "analyzer": {
        "my_analyzer": {
          "tokenizer": "my_tokenizer"
        }
      },
      "tokenizer": {
        "my_tokenizer": {
          "type": "ngram",
          "min_gram": 3,
          "max_gram": 3,
          "token_chars": [
            "letter",
            "digit"
          ]
        }
      }
    }
  }
}

POST my_index/_analyze
{
  "analyzer": "my_analyzer",
  "text": "2 Quick Foxes."
}
```

输出为：

```
[ Qui, uic, ick, Fox, oxe, xes ]
```


---

# Agent Instructions: Querying This Documentation

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

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://xiaoxiami.gitbook.io/elasticsearch/ji-chu/33-analysisfen-679029/334-tokenizersff08-fen-ci-qi-ff09/ngram-tokenizer.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
