To enable geospatial indexing in Cosmos DB during migration, follow these steps to ensure it's configured correctly, as this option is not always directly visible in the UI:
- Understand Geospatial Indexing
- Geospatial indexing in Azure Cosmos DB is supported for Azure Cosmos DB Core (SQL) API.
- The feature is applied at the container (collection) level by modifying the indexing policy.
- Modify Indexing Policy
- The geospatial indexing option must be explicitly defined in the indexing policy of the container. You cannot directly enable it during migration through a visible toggle; instead, you need to update the indexing policy either before or after the migration.
Update the Indexing Policy via Azure Portal:
Navigate to your Cosmos DB account in the Azure Portal.
Go to the Data Explorer and find the target container.
Open the Settings of the container.
Locate the Indexing Policy section.
- Add or update the following in the indexing policy:
*az cosmosdb sql container update * *--account-name <CosmosDBAccountName> * *--database-name <DatabaseName> * *--name <ContainerName> * --resource-group <ResourceGroupName> * --indexing-policy '{ "indexingMode": "consistent", "automatic": true, "includedPaths": [ { "path": "/", "indexes": [ {"kind": "Range", "dataType": "Number"}, {"kind": "Range", "dataType": "String"}, {"kind": "Spatial", "dataType": "Point"} ] } ], "excludedPaths": [] }'
Spatial
index is added for the appropriate data type (e.g.,Point
). Save the updated policy.
Update the Indexing Policy via CLI or SDK:
Use the Azure Cosmos DB SDK or Azure CLI to modify the container’s indexing policy programmatically. Example using Azure CLI:
az cosmosdb sql container update
--account-name <CosmosDBAccountName>
--database-name <DatabaseName>
--name <ContainerName>
--resource-group <ResourceGroupName>
--indexing-policy '{ "indexingMode": "consistent", "automatic": true, "includedPaths": [ { "path": "/*", "indexes": [ {"kind": "Range", "dataType": "Number"}, {"kind": "Range", "dataType": "String"}, {"kind": "Spatial", "dataType": "Point"} ] } ], "excludedPaths": [] }'
- Verify Geospatial Data
- Ensure that your data schema contains geospatial fields, such as those with
Point
data types. Example:{
"id": "1", "location": { "type": "Point", "coordinates": [-122.084, 37.422] }
}
- Migrate the Data
- Use Azure Data Migration Tool (DMS) or a custom migration script that supports updating the indexing policy.
- Confirm that geospatial fields are correctly indexed and queries work as expected.
- Test Queries
- Run geospatial queries (ex:
ST_DISTANCE
,ST_WITHIN
) to ensure indexing is working.
If the option still does not show in the Azure portal, modifying the indexing policy directly through the Azure CLI, SDK, or REST API as described above is the best alternative.
Please feel free to click the 'Upvote' (Thumbs-up) button and 'Accept as Answer'. This helps the community by allowing others with similar queries to easily find the solution.