Static mapping is an Atlas Search index configuration that names the fields and subfields to index. Set `mappings.dynamic` to `false` and define the paths under `mappings.fields`. If `dynamic` is omitted, it also defaults to `false`. Unlike dynamic mapping, which indexes all supported fields automatically, static mapping limits indexing to the fields you choose. section 11.3.3
Choose fields your searches frequently sort or filter on. Selective indexing can improve performance and reduce disk use, making static mapping the recommended production choice. For polymorphic data, documents whose field types do not match the index definition are not indexed for those fields; check the data before relying on those paths in searches. section 11.3.3
To create a statically mapped index with the Atlas CLI:
- Define the cluster, database, collection, and index mappings in `index-definition.json`. Set `"dynamic": false`, then give each indexed field a type. For nested paths, define an `address` field of type `document` with its own `fields` object. section 11.4
- Run `atlas clusters search indexes create --clusterName MongoDB-in-Action --file index-definition.json --output json`. A response marked `IN_PROGRESS` means the index is still being created; list the collection’s search indexes to check its status. section 11.4
Listing 11.2 Atlas Search index config
{
"name": "MongoDB-in-Action",
"database": "sample_training",
"collectionName": "inspections",
"mappings": {
"dynamic": false,
"fields": {
"business_name": {
"type": "string",
"analyzer": "lucene.standard"
},
"date": {
"type": "date"
},
"result": {
"type": "string",
"analyzer": "lucene.standard"
},
"sector": {
"type": "string",
"analyzer": "lucene.standard"
},
"address": {
"type": "document",
"fields": {
"city": {
"type": "string",
"analyzer": "lucene.standard"
},
"zip": {
"type": "string",
"analyzer": "lucene.standard"
},
"street": {
"type": "string",
"analyzer": "lucene.standard"
},
"number": {
"type": "string",
"analyzer": "lucene.standard"
}
}
}
}
}
}
The index configuration illustrates string and date field types, `lucene.standard` on string fields, and nested address subfields. section 11.4
An existing index can also be changed to static mapping with `db.collection.updateSearchIndex()` by supplying a configuration with `dynamic: false` and explicit `fields`. These search-index shell commands require an Atlas cluster tier of at least M10. section 11.6
