创建MongoDB 搜索索引时,可以使用以下方法指定要索引的字段:
动态映射:启用 MongoDB 搜索根据默认或配置的字段类型 (
typeSet) 自动为所有字段建立索引。静态映射:允许您指定要索引的字段。
数据类型的限制
默认下, MongoDB Search 停止复制副本副本集或单个分片上超过 2.1 十亿个索引对象的索引更改,其中每个已建立索引的文档或嵌套
embeddedDocument都算作单个对象。这意味着您的索引仍然可查询,但可能会得到过时的结果。如果计划对可能超过 2.1 亿个对象的字段进行索引,其中索引对象是顶层文档或嵌入式文档,请使用 numPartitions 索引选项对索引进行分区(仅在搜索节点部署上支持)或 分片集群。
您无法对字段名称开头包含美元 (
$) 符号的字段创建索引。与其他字段类型相比,自动完成字段类型可以创建大型索引,且构建时间更长。虽然您可以通过将
autocomplete类型包含在自定义typeSet定义中来在动态映射中使用该类型,但我们建议仅在静态映射中使用autocomplete类型,以避免对性能、存储和评分产生意外影响。要学习;了解更多信息,请参阅如何为自动完成索引字段和MongoDB搜索索引性能。
语法
The following syntax demonstrates how to enable MongoDB Search to index fields using dynamic and static mappings. To learn more about dynamic and static mappings, see Dynamic and Static Mappings.
1 { 2 "mappings": { 3 "dynamic": true|false | { 4 "typeSet": "<typeset-name>" 5 }, 6 "fields": { 7 "<field-name>": { 8 "type": "<field-type>", 9 ... 10 }, 11 ... 12 } 13 }, 14 "typeSets": [ 15 { 16 "name": "<typeset-name>", 17 "types": [ 18 { 19 "type": "<field-type>", 20 ... 21 }, 22 ... 23 ] 24 }, 25 ... 26 ] 27 }
动态和静态映射
您可以通过以下方式配置MongoDB搜索:
动态映射,用于根据默认或配置的类型设立(
typeSet) 自动为字段索引仅索引指定字段的静态映射
You can also use dynamic mappings with static mappings. Static mappings override the dynamic mappings configuration.
动态映射
MongoDB 搜索动态映射允许您将MongoDB 搜索配置为自动递归地索引数据中的字段。可以根据默认类型设立或通过配置 typeSet 对字段进行索引。
您可以启用或配置动态映射:
在根
mappings级别应用整个文档。[推荐] 在
document字段中键入要应用指定对象的字段。[推荐] 在
embeddedDocuments字段类型中,要应用需要逐元素查询比较的指定对象数组(类似于$elemMatch)。
注意
最佳实践
动态映射可能会导致对大量唯一字段索引,从而占用更多磁盘空间并且可能降低性能。仅当需要对定期更改或未知的字段索引时,才使用动态映射。始终在文档中而不是在父文档级别使用动态映射。
提示
当您使用动态映射对数据进行索引时:
MongoDB 搜索还会对数据中
document对象中的typeSet支持的所有嵌套字段进行动态索引。如果字段包含多态数据, MongoDB Search 会自动将该字段索引为索引中使用的
typeSet支持的所有类型。如果字段包含typeSet不支持类型的数据, MongoDB 搜索 将不会索引该数据。
启用默认 typeSet
当 dynamic 设置为 true 时,MongoDB Search 使用默认的 typeSet。在默认的 typeSet 中,MongoDB Search 将 BSON 类型索引为 MongoDB Search 字段类型。下表显示使用默认 typeSet 时,MongoDB Search 自动索引为 MongoDB Search 字段类型的 BSON 类型。MongoDB Search 还会自动索引包含在数组和对象中的以下 BSON 类型。
以下是为动态映射启用默认类型设立的语法:
1 { 2 "mappings": { 3 "dynamic": true 4 } 5 }
For index examples that demonstrate indexing all fields using the default typeSet, see Dynamic Mapping Examples.
configurea typeSet
通过配置 typeSet,指定要动态索引的 MongoDB 搜索字段类型。您可以将任何 BSON 类型配置为任何 MongoDB Search 字段类型自动编制索引,但不包括 document、embeddedDocuments、vector 或已弃用的字段类型。
typeSet 采用以下语法:
注意
您无法从 Atlas 用户界面 Visual Editor 中配置 typeSet。请改用 Atlas 用户界面 JSON Editor。
1 { 2 "mappings": { 3 "dynamic": { 4 "typeSet": "<typeset-name>" 5 }, 6 "fields": { 7 "<field-name>": { 8 "type": "<field-type>", 9 ... 10 }, 11 ... 12 } 13 }, 14 "typeSets": [ 15 { 16 "name": "<typeset-name>", 17 "types": [ 18 { 19 "type": "<field-type>" 20 }, 21 ... 22 ] 23 }, 24 ... 25 ] 26 }
在配置 typeSet 之前,请考虑以下事项:
不能在同一
typeSet对象中多次定义相同的字段类型。您只能为每种字段类型配置一个typeSet定义。示例,您不能在同一
typeSet定义中为number类型定义多个配置。您可以使用
multi分析器配置动态映射。
For index examples that demonstrate using custom typeSet configuration, see Dynamic Mapping Examples.
静态映射
使用静态映射为您不希望动态编制索引的字段配置索引选项,或者独立于索引中的其他字段配置单个字段。使用静态映射时, MongoDB Search 仅对您在 mappings.fields 中指定的字段编制索引。您还可以使用静态映射将字段排除在索引之外。
To use static mappings to configure index options for only some fields, set mappings.dynamic to false and specify the field name, data type, and other configuration options for each field that you want to index. You can specify the fields in any order.
如果您忽略 mappings.dynamic 字段,则默认为 false。
1 { 2 "mappings": { 3 "dynamic": true|false, 4 "fields": { 5 "<field-name>": { 6 "type": "<field-type>", 7 ... 8 }, 9 ... 10 } 11 } 12 }
To define the index for a nested field, you must define the mappings for each parent field of that nested field. You can't use dot notation to statically index nested fields. For examples, see the Examples or Combined Mapping Example below.
You can use static mappings to index fields as multiple types. To index a field as multiple types, define the types in the field definition array for the field. You can index any field as any supported types using static mappings. To learn more, see MongoDB Search Field Types.
以下示例显示了将一个字段索引为多种类型的字段定义。
1 { 2 "mappings": { 3 "dynamic": true|false | { 4 "typeSet": "<type-set-name>" 5 }, 6 "fields": { 7 "<field-name>": [ 8 { 9 "type": "<field-type>", 10 ... 11 }, 12 { 13 "type": "<field-type>", 14 ... 15 }, 16 ... 17 ], 18 ... 19 } 20 }. 21 "typeSets": [ 22 { 23 "name": "<typeset-name>", 24 "types": [ 25 { 26 "type": "<field-type>" 27 }, 28 ... 29 ] 30 }, 31 ... 32 ] 33 }
For other index examples that demonstrate static mappings, see Static Mapping Example.
MongoDB搜索字段类型
MongoDB Search 不支持以下BSON数据类型:
Decimal128
带作用域的 JavaScript 代码
Max key
Min key
正则表达式
时间戳
MongoDB Search automatically stores fields of type string on mongot. You can store fields of all supported data types on MongoDB Search using the Define Stored Source Fields in Your MongoDB Search Index option in your index definition. To learn more about mongot and MongoDB Search node architecture, see MongoDB Search Deployment Options.
下表列举了支持的 BSON 数据类型 和可用于对 BSON 数据类型进行索引的 MongoDB Search 字段类型 。该表还列出了可用于查询字段值的 操作符和收集器。
BSON 类型 | MongoDB搜索字段类型 | 运算符和收集器 |
|---|---|---|
支持数组中数据类型的操作符。 | ||
布尔 | ||
Date | ||
Date | ||
double | ||
double | ||
double | ||
32 位整数 | ||
32 位整数 | ||
64 位整型 | ||
64 位整型 | ||
null | 不适用 | |
对象 | 支持对象中字段类型的操作符。 | |
对象 | embeddedDocument(用于对象数组) | |
ObjectId | ||
字符串 | ||
字符串 | ||
字符串 | ||
字符串 | ||
向量 |
这些操作符不支持字符串数组。
MongoDB Search 不包含用于索引null 值的字段类型,因为MongoDB Search 会自动为静态和动态索引字段的 null 值索引。
已弃用。要了解有关已弃用的分面(Facet)字段类型及其更新的对应字段类型的更多信息,请参阅比较分面字段类型。
double 或 int 值的数组。
注意
You can store fields of all supported data types on MongoDB Search using the storedSource option.
示例
对 sample_mflix.movies 集合的以下示例索引定义展示了如何使用动态和静态映射对字段进行索引。您可以在集群上加载示例数据来尝试这些示例。要了解如何创建 MongoDB Search 索引,请参阅MongoDB Search 快速入门。
动态映射示例
以下示例索引定义演示了如何使用动态映射。
以下索引定义:
- 在根级别启用动态映射,以根据名为
only_strings的typeSet定义自动为集合中的字段索引,该定义使用positions索引选项并将store设立为false来为集合中的 string 字段编制索引。这些配置选项不存储查询突出显示的字段值或术语偏移,从而节省磁盘空间。
{ "mappings": { "dynamic": { "typeSet": "only_strings" } }, "typeSets": [ { "name": "only_strings", "types": [ { "type": "string", "store": false, "indexOptions": "positions" } ] } ] }
以下索引定义:
在根级别启用动态映射,以根据名为
only_strings的typeSet动态索引集合中的字段,这将使用positions索引选项并将store设立为false来为集合中的 string 字段编制索引,不存储查询突出显示的字段值或术语偏移量,从而节省磁盘空间。将
awards字段索引为document类型,并使用带有only_numberstypeSet的dynamic选项,仅动态索引awards文档中的数字子字段。这意味着数字子字段wins和nominations已建立索引,但 string 子字段text未建立索引。
{ "mappings": { "dynamic": { "typeSet": "only_strings" }, "fields": { "awards": { "type": "document", "dynamic": { "typeSet": "only_numbers" } } } }, "typeSets": [ { "name": "only_numbers", "types": [ { "type": "number" } ] }, { "name": "only_strings", "types": [ { "type": "string", "store": false, "indexOptions": "positions" } ] } ] }
以下索引定义:
使用名为
indexedTypes的typeSet为特定字段类型配置动态映射,其行为如下:自动将 string 字段索引为
token类型。自动将数字字段索引为
number类型。
不将
plot字段纳入索引。
{ "mappings": { "dynamic": { "typeSet": "indexedTypes" }, "fields": { "plot": [] } }, "typeSets": [ { "name": "indexedTypes", "types": [ { "type": "token" }, { "type": "number" } ] } ] }
静态映射示例
以下示例索引定义演示了如何使用静态映射。
以下索引定义:
在根级别 (
dynamic:false) 指定静态字段映射,这意味着未在mappings.fields中指定的字段不会被编入索引。索引定义明确包含以下用于索引的字段:awards字段,其类型为document。它具有三个嵌入式子字段:wins、nominations和text。wins和nominations子字段包含数值数据,并按number类型进行索引。nominations使用int64表示形式进行索引,这使其能够支持比默认number类型更大的整数值。text子字段包含 string 数据,并使用针对英文文本优化的 lucene.english 分析器将其索引为string类型。text字段还使用ignoreAbove选项忽略任何长度超过 255 个字符的字符串。title字段,包含字符串数据,并使用lucene.whitespace分析器将其索引为string类型,该分析器针对包含大量空格的文本(例如产品名称或标题)进行了优化。title字段使用multi选项将lucene.french分析器指定为从节点(secondary node from replica set)分析器。这样,您就可以在{ "value": "title", "multi": "frenchAnalyzer" }查询中将path指定为$search,从而更好地定位法语查询词语。genres字段,包含一个字符串数组,并按string类型进行索引。默认下, MongoDB Search 使用 lucene.standard 分析器对数组元素进行索引。对于索引数组, MongoDB Search 仅需要数组元素的数据类型。您不必在索引定义中指定数据包含在数组中。
{ "mappings": { "dynamic": false, "fields": { "awards": { "type": "document", "fields": { "wins": { "type": "number" }, "nominations": { "type": "number", "representation": "int64" }, "text": { "type": "string", "analyzer": "lucene.english", "ignoreAbove": 255 } } }, "title": { "type": "string", "analyzer": "lucene.whitespace", "multi": { "frenchAnalyzer": { "type": "string", "analyzer": "lucene.french" } } }, "genres": { "type": "string" } } } }
组合映射示例
以下索引定义示例将动态映射与静态映射相结合。
此索引定义:
指定静态字段映射 (
dynamic:false),这意味着未显式提及的字段和字段类型不会被编入索引。因此,索引定义包括:title字段,包含字符串数据,并使用lucene.whitespace分析器将其索引为string类型,该分析器针对包含大量空格的文本(例如产品名称或标题)进行了优化。awards字段,其类型为document,包含三个子字段。wins和nominations子字段包含数值数据,text子字段包含 string 数据。索引定义无需为文档中的每个嵌套字段显式设置数据类型,而是可为文档中的所有子字段启用动态映射。这会将wins和nominations索引为number类型,并将text索引为string类型。默认下,使用 lucene.standard 分析器对text字段中的数据进行索引。
{ "mappings": { "dynamic": false, "fields": { "title": { "type": "string", "analyzer": "lucene.whitespace" }, "awards": { "type": "document", "dynamic": true } } } }
此索引定义:
在根级别指定静态字段映射 (
dynamic:false),这意味着未在mappings.fields选项中指定的字段不会被编制索引。使用名为
movieAwards的自定义typeSet为awards字段(类型为document)定义动态映射。awards字段具有三个嵌入式子字段:wins、nominations和text。该索引定义使用typeSet为document字段类型配置动态索引,而不是通过在根级别将dynamic设置为true或使用静态映射显式索引每个嵌套字段来自动为子字段建立索引。名为movieAwards的定义。movieAwardstypeSet执行以下操作:使用 multi 选项将 string 字段索引为
string类型,以指定多个分析器:lucene.english和lucene.french。使用
number类型的默认设置,将数字字段索引为number类型。{ "mappings": { "dynamic": false, "fields": { "awards": { "type": "document", "dynamic": { "typeSet": "movieAwards" } } } }, "typeSets": [ { "name": "movieAwards", "types": [ { "type": "string", "multi": { "english": { "type": "string", "analyzer": "lucene.english" }, "french": { "type": "string", "analyzer": "lucene.french" } } }, { "type": "number" } ] } ] }