对于 AI 代理:可在 https://www.mongodb.com/zh-cn/docs/llms.txt 获取文档索引—通过在任何 URL 路径后添加 .md 可获取所有页面的 Markdown 版本。
Docs 菜单

Tokenizers for Custom Search Analyzers

自定义分析器的分词器决定MongoDB Search 如何将文本拆分为离散的数据段以索引。分词器需要一个 type 字段,有些还接受额外的选项。

语法
"tokenizer": {
"type": "<tokenizer-type>",
"<additional-option>": "<value>"
}

The sample index definitions and queries on this page use the sample collection named minutes. To try these examples, load the minutes collection on your cluster and navigate to the Create a Search Index page in the Atlas UI following the steps in the Create a MongoDB Search Index tutorial. Then, select the minutes collection as your data source, and follow the example procedure to create an index from the Atlas UI or using mongosh.

edgeGram 分词器将文本输入左侧(或“边缘”)的输入标记为给定大小的 n 元模型。您无法在同义词自动完成字段映射定义analyzer 字段中将自定义分析器与 edgeGram 分词器结合使用。

The edgeGram tokenizer has the following attributes:

注意

edgeGram 分词器可按单词或跨输入文本中的单词生成多个输出标记,从而生成标记图。

由于自动完成字段类型映射定义和具有同义词映射的分析器仅在与不生成图的分词器一起使用时才有效,因此不能将 analyzer 字段中带有 edgeGram 分词器的自定义分析器用于自动完成字段类型映射定义或带有同义词映射的分析器。

名称
类型
必需?
说明

type

字符串

标识此分词器类型的可读标签。值必须是 edgeGram

minGram

整型

要包含在创建的最短词元中的字符数。

maxGram

整型

要包含在创建的最长词元中的字符数。

以下索引定义使用名为 edgegramExample 的自定义分析器对 minutes 集合中的 message 字段建立索引。它使用 edgeGram 分词器创建 27 个字符长的词元(可搜索词),从 message 字段中单词左侧的第一个字符开始。

下面的查询在 minutes 集合的 message 字段中搜索以 tr 开头的文本。

MongoDB Search 在结果中返回具有 _id: 1_id: 3 的文档,因为MongoDB Search 使用文档的 edgeGram分词器创建了一个值为 tr 的词元,该词元与搜索术语匹配。如果使用 standard分词器为 message字段索引, MongoDB Search 将不会返回搜索术语tr 的任何结果。

下表列出了 edgeGram 分词器以及与之相比较的 standard 分词器为结果中的文档创建的词元:

分词器
Token Outputs

standard

try, to , sign , in

edgeGram

tr, try , try{SPACE} , try t , try to , try to{SPACE}

keyword分词器将整个输入标记为单个词元。MongoDB Search 不会使用 keyword分词器对超过 32766 个字符的字符串字段索引。

The keyword tokenizer has the following attributes:

名称
类型
必需?
说明

type

字符串

标识此分词器类型的可读标签。值必须是 keyword

以下索引定义使用名为 keywordExample 的自定义分析器对 minutes 集合中的 message 字段建立索引。它使用 keyword 分词器在整个字段上创建作为单个术语的词元(可搜索词)。

下面的查询在 minutes 集合的 message 字段中搜索词语 try to sign-in

MongoDB Search 在结果中返回具有 _id: 3 的文档,因为MongoDB Search 使用文档的 keyword分词器创建了一个值为 try to sign-in 的词元,该词元与搜索术语匹配。如果使用 standard分词器为 message字段索引,则MongoDB Search 将针对搜索术语try to sign-in 返回具有 _id: 1_id: 2_id: 3 的文档,因为每个文档都包含 standard分词器创建。

下表列出了 keyword 分词器以及与之相比较的 standard 分词器为具有 _id: 3 的文档创建的词元:

分词器
Token Outputs

standard

try, to , sign , in

keyword

try to sign-in

nGram 分词器标记为给定大小的文本块或“n-grams”。对于同义词自动完成字段映射定义,不能在 analyzer 字段中使用带有 nGram 分词器的自定义分析器。

The nGram tokenizer has the following attributes:

名称
类型
必需?
说明

type

字符串

标识此分词器类型的可读标签。值必须是 nGram

minGram

整型

要包含在创建的最短词元中的字符数。

maxGram

整型

要包含在创建的最长词元中的字符数。

以下索引定义使用名为 ngramExample 的自定义分析器为 minutes 集合中的 title 字段建立索引,并使用 nGram 分词器在 title 字段中创建长度介于 46 个字符之间的词元(可搜索术语)。

下面的查询在 minutes 集合的 title 字段中搜索词语 week

MongoDB Search 在结果中返回具有 _id: 1 的文档,因为MongoDB Search 使用文档的 nGram分词器创建了一个值为 week 的词元,该词元与搜索术语匹配。如果使用 standardedgeGram分词器为 title字段索引, MongoDB Search 将不会返回搜索术语week 的任何结果。

下表列出了 nGram 分词器以及与之相比,standardedgeGram 分词器为具有 _id: 1 的文档创建的标记:

分词器
Token Outputs

standard

The, team's , weekly , meeting

edgeGram

The{SPACE}, The t , The te

nGram

The{SPACE}, The t, The te, he t, ... , week, weekl, weekly, eekl, ..., eetin, eeting, etin, eting, ting

regexCaptureGroup 分词器与 Java 正则表达式模式相匹配以提取词元。

提示

To learn more about Java regular expression syntax, see the Pattern class in the Java documentation.

The regexCaptureGroup tokenizer has the following attributes:

名称
类型
必需?
说明

type

字符串

标识此分词器类型的可读标签。值必须是 regexCaptureGroup

pattern

字符串

要匹配的正则表达式。

group

整型

匹配表达式中要提取为标记的字符群组的索引。使用 0 提取所有字符组。

以下索引定义使用名为 phoneNumberExtractor 的自定义分析器对 minutes 集合中的 page_updated_by.phone 字段建立索引。它使用以下命令:

  • mappings 字符过滤器,删除前三位数字周围的括号并用破折号替换所有空格和句点

  • regexCaptureGroup 分词器,用于根据文本输入中存在的第一个美国格式的电话号码创建单个词元

以下查询会在 minutes 集合的 page_updated_by.phone 字段中搜索电话号码 123-456-9870

MongoDB Search 在结果中返回具有 _id: 3 的文档,因为MongoDB Search 使用文档的 regexCaptureGroup分词器创建了一个值为 123-456-7890 的词元,该词元与搜索术语匹配。如果使用 standard分词器为 page_updated_by.phone字段索引, MongoDB Search 将返回搜索术语123-456-7890 的所有文档。

下表列出了 regexCaptureGroup 分词器以及与之相比较的 standard 分词器为具有 _id: 3 的文档创建的词元:

分词器
Token Outputs

standard

123, 456.9870

regexCaptureGroup

123-456-9870

regexSplit 分词器使用基于 Java 正则表达式的分隔符拆分词元。

提示

To learn more about Java regular expression syntax, see the Pattern class in the Java documentation.

The regexSplit tokenizer has the following attributes:

名称
类型
必需?
说明

type

字符串

标识此分词器类型的可读标签。值必须是 regexSplit

pattern

字符串

要匹配的正则表达式。

以下索引定义使用名为 dashDotSpaceSplitter 的自定义分析器对 minutes 集合中的 page_updated_by.phone 字段建立索引。它使用 regexSplit 分词器从 page_updated_by.phone 字段中的一个或多个连字符、句点和空格创建词元(可搜索词)。

下面的查询在 minutes 集合的 page_updated_by.phone 字段中搜索数字 9870

MongoDB Search 在结果中返回具有 _id: 3 的文档,因为MongoDB Search 使用文档的 regexSplit分词器创建了一个值为 9870 的词元,该词元与搜索术语匹配。如果使用 standard分词器为 page_updated_by.phone字段索引, MongoDB Search 将不会返回搜索术语9870 的任何结果。

下表列出了 regexCaptureGroup 分词器以及与之相比较的 standard 分词器为具有 _id: 3 的文档创建的词元:

分词器
Token Outputs

standard

123, 456.9870

regexSplit

(123), 456 , 9870

standard 分词器根据 Unicode 文本分割算法中的分词规则进行分词。

The standard tokenizer has the following attributes:

名称
类型
必需?
说明

type

字符串

标识此分词器类型的可读标签。值必须是 standard

maxTokenLength

整型

no

单个标记的最大长度。大于此长度的标记将按 maxTokenLength 分割为多个标记。

默认值255

以下索引定义使用名为 standardExample 的自定义分析器对 minutes 集合中的 message 字段建立索引。它使用 standard 分词器和停用词词元过滤器

下面的查询在 minutes 集合的 message 字段中搜索词语 signature

MongoDB Search 返回具有 _id: 4 的文档,因为MongoDB Search 使用文档的 standard分词器创建了一个值为 signature 的词元,该词元与搜索术语匹配。如果使用 keyword分词器为 message字段索引, MongoDB Search 将不会返回搜索术语signature 的任何结果。

下表列出了 standard 分词器以及与之相比较的 keyword 分析器为具有 _id: 4 的文档创建的词元:

分词器
Token Outputs

standard

write, down , your , signature , or , phone

keyword

write down your signature or phone №

uaxUrlEmail 分词器将对 URL 和电子邮件地址进行分词。虽然 uaxUrlEmail 分词器会根据 Unicode 文本分段算法中的分词规则进行分词,但我们建议仅在索引字段值包含 URL 和电子邮件地址时才使用 uaxUrlEmail 分词器。对于不包含 URL 或电子邮件地址的字段,请使用 标准分词器以根据分词规则创建词元。

The uaxUrlEmail tokenizer has the following attributes:

名称
类型
必需?
说明

type

字符串

标识此分词器类型的可读标签。值必须是 uaxUrlEmail

maxTokenLength

int

no

一个词元中的最大字符数。

默认值255

使用 uaxUrlEmail分词器创建简单索引。

以下索引定义使用名为 basicEmailAddressAnalyzer 的自定义分析器对 minutes 集合中的 page_updated_by.email 字段建立索引。它使用 uaxUrlEmail 分词器从 page_updated_by.email 字段中的 URL 和电子邮件地址创建词元(可搜索的术语)。

下面的查询会在 minutes 集合的 page_updated_by.email 字段中搜索电子邮件 lewinsky@example.com

MongoDB Search 在结果中返回具有 _id: 3 的文档,因为MongoDB Search 使用文档的 uaxUrlEmail分词器创建了一个值为 lewinsky@example.com 的词元,该词元与搜索术语匹配。如果使用 standard分词器为 page_updated_by.email字段索引, MongoDB Search 将返回搜索术语lewinsky@example.com 的所有文档。

下表列出了 uaxUrlEmail 分词器以及与之相比较的 standard 分词器为具有 _id: 3 的文档创建的词元:

分词器
Token Outputs

standard

lewinsky, example.com

uaxUrlEmail

lewinsky@example.com

使用具有自动完成分词策略的 uaxUrlEmail分词器创建索引。

以下索引定义使用名为 emailAddressAnalyzer 的自定义分析器对 minutes 集合中的 page_updated_by.email 字段建立索引。它使用以下命令:

  • 采用 edgeGram 分词策略autocomplete 类型

  • uaxUrlEmail 分词器,用于从 URL 和电子邮件地址创建词元(可搜索词语)

下面的查询在 minutes 集合的 page_updated_by.email 字段中搜索词语 exam

MongoDB Search 在结果中返回具有 _id: 3 的文档,因为MongoDB Search 使用文档的 uaxUrlEmail分词器创建了一个值为 lewinsky@example.com 的词元,该词元与搜索术语匹配。如果使用 standard分词器为 page_updated_by.email字段索引, MongoDB Search 将返回搜索术语lewinsky@example.com 的所有文档。

下表列出了 uaxUrlEmail 分词器以及与之相比较的 standard 分词器为具有 _id: 3 的文档创建的词元:

分词器
MongoDB搜索字段类型
Token Outputs

standard

autocomplete edgeGram

le, lew, lewi, lewin, lewins, lewinsk, lewinsky, lewinsky@, lewinsky, ex, exa, exam, examp, exampl, example, example., example.c, example.co, example.com

uaxUrlEmail

autocomplete edgeGram

le, lew, lewi, lewin, lewins, lewinsk, lewinsky, lewinsky@, lewinsky@e, lewinsky@ex, lewinsky@exa, lewinsky@exam, lewinsky@examp, lewinsky@exampl

whitespace分词器根据单词之间出现的空格进行分词。

The whitespace tokenizer has the following attributes:

名称
类型
必需?
说明

type

字符串

标识此分词器类型的可读标签。值必须是 whitespace

maxTokenLength

整型

no

单个标记的最大长度。大于此长度的标记将按 maxTokenLength 分割为多个标记。

默认值255

以下索引定义使用名为 whitespaceExample 的自定义分析器对 minutes 集合中的 message 字段建立索引。它使用 whitespace 分词器从 message 字段中的任意空格创建词元(可搜索词)。

下面的查询在 minutes 集合的 message 字段中搜索词语 SIGN-IN

MongoDB Search 在结果中返回具有 _id: 3 的文档,因为MongoDB Search 使用文档的 whitespace分词器创建了一个值为 sign-in 的词元,该词元与搜索术语匹配。如果使用 standard分词器为 message字段索引, MongoDB Search 将针对搜索术语sign-in 返回具有 _id: 1_id: 2_id: 3 的文档。

下表列出了 whitespace 分词器以及与之相比较的 standard 分词器为具有 _id: 3 的文档创建的词元:

分词器
Token Outputs

standard

try, to , sign , in

whitespace

try, to , sign-in