我正在尝试为 COMOS 中的几个属性定义索引,但我对自动索引有点困惑。根据 Cosmos DB documentation :
By default, Azure Cosmos DB automatically indexes every property for all items in your container without having to define any schema or configure secondary indexes.
另外,请参阅this :
In some situations, you may want to override this automatic behavior to better suit your requirements. You can customize a container's indexing policy by setting its indexing mode, and include or exclude property paths.
从以上几点我了解到的是,除非我们定义自定义索引策略,否则自动索引将设置为 true
(这是有道理的)。但是,如果我们定义了自己的包含和排除路径
用于索引,否则它应该是false
。
这可能意味着,如果我按如下方式定义容器属性,则 Cosmos 上的 Indexing Policy
Automatic
属性应设置为 false
数据库。
using Microsoft.Azure.Cosmos; //Azure Cosmos SDK v3.3.1
.
.
var containerProperties = new ContainerProperties
{
Id = "SOME_CONTAINER_NAME",
PartitionKeyPath = "/MY_PARTITION_KEY",
};
containerProperties.IndexingPolicy.IncludedPaths.Add(new IncludedPath {Path = "/\"{MY_PARTITION_KEY}\"/?"});
containerProperties.IndexingPolicy.ExcludedPaths.Add(new ExcludedPath {Path = "/*"});
但是,我发现上述 CosmosDb 索引配置的 automatic
设置为 true
。
Automatic
属性与 IndexingPolicy
class
中的 IncludedPaths
、ExcludedPaths
属性是否不相关?如果是这样,当我们在索引策略上定义了 IncludedPaths
和 ExcludedPaths
时,automatic
属性意味着什么?
编辑 1
它变得有点棘手和困惑。即使将 Automatic
属性设置为 false
后,该属性在门户中仍保持 true
。
那就是下面的代码似乎没有任何效果。
containerProperties.IndexingPolicy.Automatic = false;
编辑2
即使我从门户设置
更新自动
属性,该值也不会改变。我也没有收到任何错误。
最佳答案
我来自 CosmosDB 工程团队。 “自动”属性和包含/排除路径无关。
现在大多数容器已弃用“自动”属性。它可用于通过在每个文档级别覆盖索引指令,将集合水平隔离为两组文档 - 一组是辅助索引的,另一组不是。除了缺乏具体的业务值(value)之外,将自动属性设置为 false 还会导致查询结果不一致,具体取决于查询是否使用索引(例如,与扫描相反)。因此我们现在已弃用该属性(不能将其设置为 false)。
我们通常所说的“自动索引”是指容器中所有文档的所有路径都默认被索引。这可以从以下事实看出:默认索引策略在 IncludedPaths 部分中包含/*(“根”路径下的所有内容)。希望这会有所帮助。
关于azure - Cosmos DB 中的自动索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58703586/