2015-09-23 73 views
0

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

我认为这是默认的。

+0

[DocumentDb:查询没有索引]的可能重复(http://stackoverflow.com/questions/32733740/documentdb-querying-without-an-index) –

回答

2

您将要更改代码段对收集规范定义的索引策略之前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()也更新索引策略设置自定义索引策略。