使用PowerShell更新CosmosDB索引策略,如何移除默认添加的_etag排除路径?
解决Azure Cosmos DB索引策略自动添加_etag排除路径的问题
原因
Azure Cosmos DB默认会自动将_etag这类系统属性加入excludedPaths,这是平台为系统属性设置的默认索引规则。当索引策略的Automatic属性设为true时,该规则会自动生效,导致你定义的排除路径被追加系统默认项。
解决方案
根据需求,可选择以下两种处理方式:
方式1:关闭自动索引,完全自定义排除路径
若不需要平台自动管理索引规则,将Automatic设为false,仅保留你需要的排除路径即可:
$resourceGroupName = "你的资源组名" $accountName = "你的Cosmos DB账户名" $databaseName = "你的数据库名" $containerName = "你的容器名" # 获取容器当前配置 $container = Get-AzCosmosDBSqlContainer -ResourceGroupName $resourceGroupName -AccountName $accountName -DatabaseName $databaseName -Name $containerName # 定义自定义索引策略 $indexingPolicy = @{ Automatic = $false IndexingMode = "Consistent" # 可根据需求改为"Lazy"或"None" IncludedPaths = @() ExcludedPaths = @( @{ Path = "/*" } ) } # 更新容器索引策略 $container.IndexingPolicy = $indexingPolicy Set-AzCosmosDBSqlContainer -InputObject $container
方式2:保留自动索引,显式包含_etag路径
若需要保留平台自动索引逻辑,但只想排除/*而不排除_etag,可在IncludedPaths中显式指定_etag的路径,覆盖默认排除规则:
$resourceGroupName = "你的资源组名" $accountName = "你的Cosmos DB账户名" $databaseName = "你的数据库名" $containerName = "你的容器名" # 获取容器当前配置 $container = Get-AzCosmosDBSqlContainer -ResourceGroupName $resourceGroupName -AccountName $accountName -DatabaseName $databaseName -Name $containerName # 定义自定义索引策略 $indexingPolicy = @{ Automatic = $true IndexingMode = "Consistent" IncludedPaths = @( @{ Path = "/\"_etag\"/?" } ) ExcludedPaths = @( @{ Path = "/*" } ) } # 更新容器索引策略 $container.IndexingPolicy = $indexingPolicy Set-AzCosmosDBSqlContainer -InputObject $container
内容的提问来源于stack exchange,提问作者moderator
相关产品推荐
相关产品推荐

