创建MongoDB 搜索索引时,可以使用以下方法指定要索引的字段:
动态映射:启用 MongoDB 搜索根据默认或配置的字段类型 (
typeSet) 自动为所有字段建立索引。静态映射:允许您指定要索引的字段。
数据类型的限制
默认情况下,对于大于 2,100,000,000 个索引对象的索引,MongoDB Search 将停止在副本集或单个分片上复制对这些索引的更改,其中每个已建立索引的文档或嵌套
embeddedDocument都算作一个对象。这意味着您的索引是可查询的,但您可能会得到过时的结果。If you plan to index fields that might exceed 2,100,000,000 objects, where an index object is a top-level document or an embedded document, use the numPartitions index option to partition your index (supported only on Search Nodes deployment) or shard your cluster.
您无法对字段名称开头包含美元 (
$) 符号的字段创建索引。The autocomplete field type can create large indexes and take longer to build than other field types. While you can use the
autocompletetype in dynamic mappings by including it in a customtypeSetdefinition, we recommend using theautocompletetype in static mappings only to avoid unintended performance, storage, and scoring implications. To learn more, see How to Index Fields for Autocompletion and Improve MongoDB Search Index Performance.
语法
以下语法演示了如何启用 MongoDB Search 以使用动态和静态映射来索引字段。要了解有关动态和静态映射的更多信息,请参阅动态和静态映射。
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. When you define a static mapping for a field, MongoDB Search indexes that field only according to the static mapping definition. The dynamic mapping no longer applies to that field.
例如,如果您启用 dynamic: true,MongoDB Search 默认会将 string 字段索引为 string 类型。如果您为特定 string 字段添加静态 autocomplete 映射,MongoDB Search 仅会将该字段索引为 autocomplete 类型。它也不会索引为 string,除非您在静态映射中明确包含 string。要将字段索引为这两种类型,必须在字段的静态映射数组中定义这两种类型。
动态映射
MongoDB 搜索动态映射允许您将MongoDB 搜索配置为自动递归地索引数据中的字段。可以根据默认类型设立或通过配置 typeSet 对字段进行索引。
您可以启用或配置动态映射:
在根
mappings级别应用整个文档。[推荐] 在
document字段中键入要应用指定对象的字段。[推荐] 在
embeddedDocuments字段类型中,要应用需要逐元素查询比较的指定对象数组(类似于$elemMatch)。
注意
最佳实践
动态映射可能会导致索引大量独特字段,这会占用更多磁盘空间,并可能会降低性能。仅在需要索引定期变更或未知字段时使用动态映射。始终在文档内使用动态映射,而不是在父文档级别使用动态映射。
提示
当您使用动态映射对数据进行索引时:
MongoDB 搜索还会对数据中
document对象中的typeSet支持的所有嵌套字段进行动态索引。如果字段包含多态数据,MongoDB Search 会自动将该字段索引为索引中使用的
typeSet支持的所有类型。如果字段包含typeSet不支持类型的数据,MongoDB Search 将不会索引该数据。
启用默认 typeSet
MongoDB Search uses the default typeSet when dynamic is set to true. In the default typeSet, MongoDB Search indexes the BSON types as MongoDB Search field types. The following table shows the BSON types that MongoDB Search automatically indexes as MongoDB Search field types when you use the default typeSet. MongoDB Search also automatically indexes the following BSON types when they are contained within arrays and objects.
以下是为动态映射启用默认类型设立的语法:
1 { 2 "mappings": { 3 "dynamic": true 4 } 5 }
有关演示使用默认typeSet对所有字段进行索引的索引示例,请参阅动态映射示例。
configurea typeSet
Specify the MongoDB Search field types to index dynamically by configuring a typeSet. You can configure any BSON type to be automatically indexed as any MongoDB Search field type except document, embeddedDocuments, vector, or deprecated field types.
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分析器配置动态映射。
有关演示使用自定义 typeSet 配置的索引示例,请参阅动态映射示例。
静态映射
使用静态映射为您不希望动态编制索引的字段配置索引选项,或者独立于索引中的其他字段配置单个字段。使用静态映射时, MongoDB Search 仅对您在 mappings.fields 中指定的字段编制索引。您还可以使用静态映射将字段排除在索引之外。
要使用静态映射仅为某些字段配置索引选项,请将 mappings.dynamic 设为 false,并为要建立索引的每个字段指定字段名称、数据类型和其他配置选项。您可以按任意顺序指定字段。
如果您忽略 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 }
要定义嵌套字段的索引,必须为该嵌套字段的每个父字段定义映射。不能使用点表示法对嵌套字段进行静态索引。有关示例,请参阅下面的示例或组合映射示例。
您可以使用静态映射将字段索引为多种类型。要将字段索引为多种类型,请在字段定义数组中为字段定义类型。您可以使用静态映射将任何字段索引为任何支持的类型。要学习;了解更多信息,请参阅MongoDB搜索字段类型。
以下示例显示了将一个字段索引为多种类型的字段定义。
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 }
有关演示静态映射的其他索引示例,请参阅静态映射示例。
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.
The following table enumerates the supported BSON data types and the MongoDB Search field types that you can use to index the BSON data types. The table also lists the operators and collectors that you can use to query the field values.
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.
示例
The following sample index definitions on the sample_mflix.movies collection demonstrate how to index fields using dynamic and static mappings. You can load the sample data on your cluster to try the examples. To learn more about how to create MongoDB Search indexes, see MongoDB Search Quick Start.
动态映射示例
以下示例索引定义演示了如何使用动态映射。
以下索引定义:
- 在根级别启用动态映射,以根据名为
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" } ] } ] }