无法更新索引策略

时间:2015-09-23 08:52:47

标签: azure azure-cosmosdb

我似乎无法从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": []
}

我认为这是默认设置。

1 个答案:

答案 0 :(得分:3)

您需要更改代码段,以便在之前client.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()更新现有馆藏的索引政策。