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

Exception when adding range index to Azure DocumentDB collection

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

The special mandatory indexing path \"\/\" is not provided in any of the path type sets. Please provide this path in one of the sets.

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

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

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

您必须包含“/”作为附加的 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 始终要求您包含或排除“/”,以便索引方案明确无误。希望这有帮助。