2015-09-23 91 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()也更新索引策略設置自定義索引策略。