自定义分析器的分词器决定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
edgeGram 分词器将文本输入左侧(或“边缘”)的输入标记为给定大小的 n 元模型。您无法在同义词或 自动完成字段映射定义的 analyzer 字段中将自定义分析器与 edgeGram 分词器结合使用。
属性
The edgeGram tokenizer has the following attributes:
注意
名称 | 类型 | 必需? | 说明 |
|---|---|---|---|
| 字符串 | 是 | 标识此分词器类型的可读标签。值必须是 |
| 整型 | 是 | 要包含在创建的最短词元中的字符数。 |
| 整型 | 是 | 要包含在创建的最长词元中的字符数。 |
例子
以下索引定义使用名为 edgegramExample 的自定义分析器对 minutes 集合中的 message 字段建立索引。它使用 edgeGram 分词器创建 2 到 7 个字符长的词元(可搜索词),从 message 字段中单词左侧的第一个字符开始。
下面的查询在 minutes 集合的 message 字段中搜索以 tr 开头的文本。
MongoDB Search 在结果中返回具有 _id: 1 和 _id: 3 的文档,因为MongoDB Search 使用文档的 edgeGram分词器创建了一个值为 tr 的词元,该词元与搜索术语匹配。如果使用 standard分词器为 message字段索引, MongoDB Search 将不会返回搜索术语tr 的任何结果。
下表列出了 edgeGram 分词器以及与之相比较的 standard 分词器为结果中的文档创建的词元:
分词器 | Token Outputs |
|---|---|
|
|
|
|
keyword
keyword分词器将整个输入标记为单个词元。MongoDB Search 不会使用 keyword分词器对超过 32766 个字符的字符串字段索引。
属性
The keyword tokenizer has the following attributes:
名称 | 类型 | 必需? | 说明 |
|---|---|---|---|
| 字符串 | 是 | 标识此分词器类型的可读标签。值必须是 |
例子
以下索引定义使用名为 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 |
|---|---|
|
|
|
|
nGram
nGram 分词器标记为给定大小的文本块或“n-grams”。对于同义词或自动完成字段映射定义,不能在 analyzer 字段中使用带有 nGram 分词器的自定义分析器。
属性
The nGram tokenizer has the following attributes:
名称 | 类型 | 必需? | 说明 |
|---|---|---|---|
| 字符串 | 是 | 标识此分词器类型的可读标签。值必须是 |
| 整型 | 是 | 要包含在创建的最短词元中的字符数。 |
| 整型 | 是 | 要包含在创建的最长词元中的字符数。 |
例子
以下索引定义使用名为 ngramExample 的自定义分析器为 minutes 集合中的 title 字段建立索引,并使用 nGram 分词器在 title 字段中创建长度介于 4 至 6 个字符之间的词元(可搜索术语)。
下面的查询在 minutes 集合的 title 字段中搜索词语 week。
MongoDB Search 在结果中返回具有 _id: 1 的文档,因为MongoDB Search 使用文档的 nGram分词器创建了一个值为 week 的词元,该词元与搜索术语匹配。如果使用 standard 或 edgeGram分词器为 title字段索引, MongoDB Search 将不会返回搜索术语week 的任何结果。
下表列出了 nGram 分词器以及与之相比,standard 和 edgeGram 分词器为具有 _id: 1 的文档创建的标记:
分词器 | Token Outputs |
|---|---|
|
|
|
|
|
|
regexCaptureGroup
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:
名称 | 类型 | 必需? | 说明 |
|---|---|---|---|
| 字符串 | 是 | 标识此分词器类型的可读标签。值必须是 |
| 字符串 | 是 | 要匹配的正则表达式。 |
| 整型 | 是 | 匹配表达式中要提取为标记的字符群组的索引。使用 |
例子
以下索引定义使用名为 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 |
|---|---|
|
|
|
|
regexSplit
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:
名称 | 类型 | 必需? | 说明 |
|---|---|---|---|
| 字符串 | 是 | 标识此分词器类型的可读标签。值必须是 |
| 字符串 | 是 | 要匹配的正则表达式。 |
例子
以下索引定义使用名为 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 分词器根据 Unicode 文本分割算法中的分词规则进行分词。
属性
The standard tokenizer has the following attributes:
名称 | 类型 | 必需? | 说明 |
|---|---|---|---|
| 字符串 | 是 | 标识此分词器类型的可读标签。值必须是 |
| 整型 | no | 单个标记的最大长度。大于此长度的标记将按 默认值: |
例子
以下索引定义使用名为 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 |
|---|---|
|
|
|
|
uaxUrlEmail
uaxUrlEmail 分词器将对 URL 和电子邮件地址进行分词。虽然 uaxUrlEmail 分词器会根据 Unicode 文本分段算法中的分词规则进行分词,但我们建议仅在索引字段值包含 URL 和电子邮件地址时才使用 uaxUrlEmail 分词器。对于不包含 URL 或电子邮件地址的字段,请使用 标准分词器以根据分词规则创建词元。
属性
The uaxUrlEmail tokenizer has the following attributes:
名称 | 类型 | 必需? | 说明 |
|---|---|---|---|
| 字符串 | 是 | 标识此分词器类型的可读标签。值必须是 |
| int | no | 一个词元中的最大字符数。 默认值: |
例子
以下索引定义使用名为 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 |
|---|---|
|
|
|
|
以下索引定义使用名为 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 |
|---|---|---|
|
|
|
|
|
|
whitespace
whitespace分词器根据单词之间出现的空格进行分词。
属性
The whitespace tokenizer has the following attributes:
名称 | 类型 | 必需? | 说明 |
|---|---|---|---|
| 字符串 | 是 | 标识此分词器类型的可读标签。值必须是 |
| 整型 | no | 单个标记的最大长度。大于此长度的标记将按 默认值: |
例子
以下索引定义使用名为 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 |
|---|---|
|
|
|
|