无法更新索引策略

Cannot update the index policy

我似乎无法从 C# SDK 更新索引策略。

if (collection == null)
{
    collection = Client
        .CreateDocumentCollectionAsync(
            databaseLink, 
            new DocumentCollection { Id = collectionId },
            new RequestOptions { OfferType = "S1" })
        .Result;
}

collection.IndexingPolicy.IndexingMode = IndexingMode.Consistent;

collection.IndexingPolicy.IncludedPaths.Add(new IncludedPath
{
    Path = "/*",
    Indexes = new System.Collections.ObjectModel.Collection<Index>
        {
            new RangeIndex(DataType.String) { Precision = -1 },
            new RangeIndex(DataType.Number) { Precision = -1 },
            new RangeIndex(DataType.Point)
        }
});

无论如何,索引看起来像这样:

{
  "indexingMode": "consistent",
  "automatic": true,
  "includedPaths": [
    {
      "path": "/*",
      "indexes": [
        {
          "kind": "Range",
          "dataType": "Number",
          "precision": -1
        },
        {
          "kind": "Hash",
          "dataType": "String",
          "precision": 3
        }
      ]
    },
    {
      "path": "/\"_ts\"/?",
      "indexes": [
        {
          "kind": "Range",
          "dataType": "Number",
          "precision": -1
        },
        {
          "kind": "Hash",
          "dataType": "String",
          "precision": 3
        }
      ]
    }
  ],
  "excludedPaths": []
}

我假设这是默认设置。

您需要更改代码片段以定义集合规范的索引策略 priorclient.CreateDocumentCollectionAsync() - 以便它包含在网络请求中为 DocumentDB 创建一个集合。

在创建集合时设置自定义索引策略

var collection = new DocumentCollection { Id = "myCollection" };

collection.IndexingPolicy.IndexingMode = IndexingMode.Consistent;

collection.IndexingPolicy.IncludedPaths.Add(
    new IncludedPath { 
        Path = "/*", 
        Indexes = new Collection<Index> { 
            new RangeIndex(DataType.String) { Precision = -1 }, 
            new RangeIndex(DataType.Number) { Precision = -1 }
        }
    });

await client.CreateDocumentCollectionAsync(database.SelfLink, collection);

更新现有集合的索引政策

您还可以使用 Client.ReplaceDocumentCollectionAsync().

更新现有集合的索引策略