仅包含某些索引的 Cosmos Db 集合



我正在尝试excludeCosmosDB 中集合上的所有索引,并且只include某些索引。

这是我的代码:

// A property in the class...
public override IEnumerable<IncludedPath> CollectionIndexes => new List<IncludedPath>
{
new IncludedPath{ Path= "/"CreatedOnUtc""  }, // I have tried "/CreatedOnUtc" as well
// few more indexes...    
};
// Code in the CreateCollection method...
collection.IndexingPolicy.ExcludedPaths.Add(new ExcludedPath { Path = "/*" }); // I have tried the code without this line as well
foreach (var path in CollectionIndexes) // Add indexes over certain properties only.
{
collection.IndexingPolicy.IncludedPaths.Add(path);
}

当我运行代码时,我每次都会收到这个异常:

消息:{"错误":["索引路径'/\'已创建OnUtc\"'无法 已接受,在位置"5"附近失败。请确保路径是 有效路径。常见错误包括无效字符或缺少 标签周围的引号。活动标识: 2627fe24-64a0-46b3-bf9a-13cb160c17a7, 请求 URI:/apps/DocDbApp/services/DocDbMaster0/partitions/780e44f4-38c8-11e6-8106-8cdcd42c33be/replicas/1p/, RequestStats: , SDK: Microsoft.Azure.Documents.Common/1.22.0.0, documentdb-dotnet-sdk/1.22.0 Host/64-bit 微软视窗NT/10.0.16299.0

当我将路径更改为"/CreatedOnUtc"时,我仍然会看到相同的消息:

消息:{"错误":["无法接受索引路径'/CreatedOnUtc', 在位置"3"附近失败。请确保路径是有效的路径。 常见错误包括无效字符或周围没有引号 标签。活动 ID: 30dc7429-df87-4080-912e-60aa731ae809, 请求 URI:/apps/DocDbApp/services/DocDbMaster0/partitions/780e44f4-38c8-11e6-8106-8cdcd42c33be/replicas/1p/, RequestStats: , SDK: Microsoft.Azure.Documents.Common/1.22.0.0, documentdb-dotnet-sdk/1.22.0 Host/64-bit 微软视窗NT/10.0.16299.0

我错过了什么?这似乎是非常基本的东西,但我浪费了 24 小时来尝试各种组合!

我正在使用 C# Sql API 来执行此操作。我正在本地PC上的CosmosDb Emulator上尝试此操作。

看起来您缺少索引路径末尾的必要通配符?

索引路径末尾的?字符是提供对此属性进行查询的查询所必需的。如果计划查询此路径的属性的子属性,则应使用字符*

您可以在索引策略文档页面上阅读有关此内容的更多信息

最新更新