【问题标题】:Cosmos Db collection with certain indexes included, only仅包含某些索引的 Cosmos Db 集合
【发布时间】:2018-07-04 15:19:43
【问题描述】:

我正在尝试 exclude all 和 include 仅在 CosmosDb 中的集合上的某些索引。

这是我的代码:

// 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);
}

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

消息:{"Errors":["索引路径 '/\"CreatedOnUtc\"' 不能 接受,在位置“5”附近失败。请确保路径是 有效路径。常见错误包括无效字符或缺少 标签周围的引号。"]} ActivityId: 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 主机/64 位 微软WindowsNT/10.0.16299.0

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

消息:{“错误”:[“无法接受索引路径'/CreatedOnUtc', 在位置“3”附近失败。请确保该路径是有效路径。 常见错误包括无效字符或周围没有引号 标签。”]} ActivityId:30dc7429-df87-4080-912e-60aa731ae809,请求 网址: /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 主机/64 位 微软WindowsNT/10.0.16299.0

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

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

【问题讨论】:

  • 你为什么要做"/\"id\""而不是简单的"/id"
  • 我也试过"/id"..
  • 我没有看到您在示例中指定索引类型。我说的是这样的事情:Indexes = new Collection&lt;Index&gt; { new RangeIndex(DataType.String) { Precision = 20 } } });
  • 我想我也试过了,我会再试一次,然后回到这里
  • 我看到你有绝对路径。您是否尝试过在最后添加所需的特殊字符?例如CreatedOnUtc 的路径应该是/CreatedOnUtc/?

标签: c# azure-cosmosdb


【解决方案1】:

您似乎在索引路径末尾缺少必要的通配符 ?

索引路径末尾的? 字符是为对该属性进行查询的查询提供服务所必需的。如果您打算查询此路径的属性的子属性,则应使用字符 *

您可以在indexing policies documentation page 上阅读更多相关信息

【讨论】:

  • 谢谢!我浏览过许多文章和视频;但我想知道我怎么会错过?
猜你喜欢
  • 2019-01-23
  • 2019-04-22
  • 2013-02-19
  • 1970-01-01
  • 2017-07-21
  • 2020-03-01
  • 2016-06-23
  • 2015-03-03
  • 1970-01-01
相关资源
最近更新 更多