向Azure DocumentDB集合添加范围索引时出现异常

时间:2015-05-09 11:18:44

标签: c# azure-cosmosdb

我正在尝试将范围索引添加到Azure DocumentDB集合中的特定属性,如this文章中所述。当我执行创建集合的代码时,我收到以下错误:

  

任何未提供特殊强制索引路径\\“\ / \\”   路径类型集。请在其中一组中提供此路径。

我用来创建集合的代码是:

var collection = new DocumentCollection { id = "myCollectionID" };

collection.IndexingPolicy.IncludedPaths.Add(new IndexingPath
{
    IndexType = IndexType.Range,
    Path = "/\"TimeStamp\"/\"Epoch\"/?",
    NumericPrecision = 7
});

this._client.CreateDocumentCollectionAsync(database.SelfLink, collection);

如果我将索引的路径设置为“/”,代码就可以工作,但我更喜欢能够在特定属性上创建索引。我做错了什么?

1 个答案:

答案 0 :(得分:7)

你必须包括" /"作为附加的IncludedPath,如:

var collection = new DocumentCollection { id = "myCollectionID" };

collection.IndexingPolicy.IncludedPaths.Add(new IndexingPath
{
    IndexType = IndexType.Range,
    Path = "/\"TimeStamp\"/\"Epoch\"/?",
    NumericPrecision = 7
});

collection.IndexingPolicy.IncludedPaths.Add(new IndexingPath
{
    IndexType = IndexType.Hash,
    Path = "/"
});

this._client.CreateDocumentCollectionAsync(database.SelfLink, collection);

或者,如果要完全从索引中排除所有其他路径,可以执行以下操作:

var collection = new DocumentCollection { id = "myCollectionID" };

collection.IndexingPolicy.IncludedPaths.Add(new IndexingPath
{
    IndexType = IndexType.Range,
    Path = "/\"TimeStamp\"/\"Epoch\"/?",
    NumericPrecision = 7
});

collection.IndexingPolicy.ExcludedPaths.Add("/");

this._client.CreateDocumentCollectionAsync(database.SelfLink, collection);

DocumentDB始终要求您包含或排除" /",以便索引方案明确无误。希望这会有所帮助。