/// <summary> /// Get a DocuemntCollection by id, or create a new one if one with the id provided doesn't exist. /// </summary> /// <param name="id">The id of the DocumentCollection to search for, or create.</param> /// <returns>The matched, or created, DocumentCollection object</returns> private static async Task <DocumentCollection> GetOrCreateCollectionAsync(Database db, string id) { DocumentCollection collection = client.CreateDocumentCollectionQuery(db.SelfLink).Where(c => c.Id == id).ToArray().FirstOrDefault(); if (collection == null) { IndexingPolicy optimalQueriesIndexingPolicy = new IndexingPolicy(); optimalQueriesIndexingPolicy.IncludedPaths.Add(new IncludedPath { Path = "/*", Indexes = new System.Collections.ObjectModel.Collection <Index>() { new RangeIndex(DataType.Number) { Precision = -1 }, new RangeIndex(DataType.String) { Precision = -1 } } }); DocumentCollection collectionDefinition = new DocumentCollection { Id = id }; collectionDefinition.IndexingPolicy = optimalQueriesIndexingPolicy; collection = await DocumentClientHelper.CreateDocumentCollectionWithRetriesAsync(client, db, collectionDefinition); } return(collection); }
public static async Task Main(string[] args) { using (CosmosClient client = new CosmosClient(_endpointUri, _primaryKey)) { Database targetDatabase = client.GetDatabase("EntertainmentDatabase"); IndexingPolicy indexingPolicy = new IndexingPolicy { IndexingMode = IndexingMode.Consistent, Automatic = true, IncludedPaths = { new IncludedPath { Path = "/*" } } }; var containerProperties = new ContainerProperties("CustomCollection", "/type") { IndexingPolicy = indexingPolicy }; var containerResponse = await targetDatabase.CreateContainerIfNotExistsAsync(containerProperties, 10000); var customContainer = containerResponse.Container; await Console.Out.WriteLineAsync($"Custom Container Id:\t{customContainer.Id}"); } }
public IndexingPolicyViewModel(IndexingPolicy policy) { _isCreating = true; // deep clone the indexing policy var json = JsonConvert.SerializeObject(policy); Policy = JsonConvert.DeserializeObject <IndexingPolicy>(json); _isCreating = false; IncludedPaths.ListChanged += (s, e) => { if (IncludedPaths.All(ip => !ip.HasErrors)) { RaisePropertyChanged(nameof(IncludedPaths)); } }; ExcludedPaths.ListChanged += (s, e) => { if (ExcludedPaths.All(ep => !ep.HasErrors)) { RaisePropertyChanged(nameof(ExcludedPaths)); } }; PropertyChanged += (s, e) => { if (e.PropertyName != nameof(IsValid)) { RaisePropertyChanged(nameof(IsValid)); } }; }
public async Task <bool> EnsureCreatedAsync <TEntity>( string databaseId, string collectionId, int collectionThroughput, IndexingPolicy indexingPolicy = null) where TEntity : class { var isSharedCollection = typeof(TEntity).UsesSharedCollection(); var collectionResource = await _cosmonautClient.GetCollectionAsync(databaseId, collectionId); if (collectionResource != null) { return(true); } var newCollection = new DocumentCollection { Id = collectionId }; SetPartitionKeyIfCollectionIsNotShared(typeof(TEntity), isSharedCollection, newCollection); SetPartitionKeyAsIdIfCollectionIsShared(isSharedCollection, newCollection); if (indexingPolicy != null) { newCollection.IndexingPolicy = indexingPolicy; } newCollection = await _cosmonautClient.CreateCollectionAsync(databaseId, newCollection, new RequestOptions { OfferThroughput = collectionThroughput }); return(newCollection != null); }
public async Task <bool> EnsureCreatedAsync <TEntity>( string databaseId, string collectionId, int collectionThroughput, IndexingPolicy indexingPolicy = null, ThroughputBehaviour onDatabaseBehaviour = ThroughputBehaviour.UseDatabaseThroughput, UniqueKeyPolicy uniqueKeyPolicy = null) where TEntity : class { var collectionResource = await _cosmonautClient.GetCollectionAsync(databaseId, collectionId); var databaseHasOffer = (await _cosmonautClient.GetOfferV2ForDatabaseAsync(databaseId)) != null; if (collectionResource != null) { return(true); } var newCollection = new DocumentCollection { Id = collectionId, IndexingPolicy = indexingPolicy ?? CosmosConstants.DefaultIndexingPolicy, UniqueKeyPolicy = uniqueKeyPolicy ?? CosmosConstants.DefaultUniqueKeyPolicy }; SetPartitionKeyDefinitionForCollection(typeof(TEntity), newCollection); var finalCollectionThroughput = databaseHasOffer ? onDatabaseBehaviour == ThroughputBehaviour.DedicateCollectionThroughput ? (int?)collectionThroughput : null : collectionThroughput; newCollection = await _cosmonautClient.CreateCollectionAsync(databaseId, newCollection, new RequestOptions { OfferThroughput = finalCollectionThroughput }); return(newCollection != null); }
public async Task <bool> EnsureCreatedAsync <TEntity>( string databaseId, string collectionId, int collectionThroughput, IndexingPolicy indexingPolicy = null) where TEntity : class { var collectionResource = await _cosmonautClient.GetCollectionAsync(databaseId, collectionId); if (collectionResource != null) { return(true); } var newCollection = new DocumentCollection { Id = collectionId, IndexingPolicy = indexingPolicy ?? CosmosConstants.DefaultIndexingPolicy }; SetPartitionKeyDefinitionForCollection(typeof(TEntity), newCollection); newCollection = await _cosmonautClient.CreateCollectionAsync(databaseId, newCollection, new RequestOptions { OfferThroughput = collectionThroughput }); return(newCollection != null); }
public static async Task Main(string[] args) { CosmosClient client = new CosmosClient(_endpoint, _conString); //DatabaseResponse dbr = await client.CreateDatabaseIfNotExistsAsync("lizard"); //Database targetDatabase = dbr.Database; //await Console.Out.WriteLineAsync($"Db id:\t{targetDatabase.Id}"); Database myDb = client.GetDatabase("lizard"); // Custom Indexing // PATH and expression // / could be a path, and * could be expressions inside it // Container -> Index IndexingPolicy indPolicy = new IndexingPolicy { IndexingMode = IndexingMode.Consistent, Automatic = true, IncludedPaths = { new IncludedPath { Path = "/*" } } }; var containerProps = new ContainerProperties("CustomCollection", "/type") { IndexingPolicy = indPolicy }; var containerResponse = await myDb.CreateContainerIfNotExistsAsync(containerProps, 1000); var customContainer = containerResponse.Container; await Console.Out.WriteLineAsync($"Container id:\t{customContainer.Id}"); }
public async Task GivenDocumentCollectionAlreadyExists_WhenCreatingDocumentCollectionIfNotExists_ThenIndexingPolicyShouldBeUpdated() { Uri collectionUri = new Uri("http://database/collection"); IndexingPolicy expectedIndexingPolicy = new IndexingPolicy(); DocumentCollection documentCollection = new DocumentCollection() { IndexingPolicy = expectedIndexingPolicy, }; DocumentCollection existingDocumentCollection = new DocumentCollection(); _documentClient.ReadDocumentCollectionAsync(collectionUri, Arg.Any <RequestOptions>()).Returns( new ResourceResponse <DocumentCollection>(existingDocumentCollection)); // Check to make sure the index policy is requested to be updated. _documentClient.ReplaceDocumentCollectionAsync( Arg.Is <DocumentCollection>(x => x == existingDocumentCollection && x.IndexingPolicy == expectedIndexingPolicy), Arg.Any <RequestOptions>()) .Returns(new ResourceResponse <DocumentCollection>(existingDocumentCollection)); DocumentCollection result = await _documentClient.CreateDocumentCollectionIfNotExistsAsync(new Uri("http://database"), collectionUri, documentCollection); Assert.NotNull(result); }
private async Task InitializeAsync() { _client = new CosmosClient(_connectionString, new CosmosClientOptions { RequestTimeout = TimeSpan.FromSeconds(_lockTtlSeconds / 2.0), MaxRetryWaitTimeOnRateLimitedRequests = TimeSpan.FromSeconds(_lockTtlSeconds / 2.0), }); CheckValidResponse(await _client.CreateDatabaseIfNotExistsAsync(_databaseId)); var db = _client.GetDatabase(_databaseId); var indexNone = new IndexingPolicy { Automatic = false, IndexingMode = IndexingMode.Consistent, ExcludedPaths = { new ExcludedPath { Path = "/*" } }, }; CheckValidResponse(await db.CreateContainerIfNotExistsAsync( new ContainerProperties("SessionStore", "/id") { IndexingPolicy = indexNone, DefaultTimeToLive = 300, })); _container = db.GetContainer("SessionStore"); await CreateLockSp(); }
public static async Task Main(string[] args) { using (CosmosClient client = new CosmosClient(_endpointUri, _primaryKey)) { DatabaseResponse databaseResponse = await client.CreateDatabaseIfNotExistsAsync("Products"); Database targetDatabase = databaseResponse.Database; await Console.Out.WriteLineAsync($"Database Id:\t{targetDatabase.Id}"); IndexingPolicy indexingPolicy = new IndexingPolicy { IndexingMode = IndexingMode.Consistent, Automatic = true, IncludedPaths = { new IncludedPath { Path = "/*" } } }; var containerProperties = new ContainerProperties("Clothing", "/productId") { IndexingPolicy = indexingPolicy }; var containerResponse = await targetDatabase.CreateContainerIfNotExistsAsync(containerProperties, 10000); var customContainer = containerResponse.Container; await Console.Out.WriteLineAsync($"Custom Container Id:\t{customContainer.Id}"); } }
public DatabaseConfiguration(string endpoint, string authKey, ConnectionPolicy connectionPolicy, string databaseId, string collectionId, string partitionKey) { RequestOptions = new RequestOptions { OfferThroughput = 1000 }; IndexingPolicy = new IndexingPolicy() { IncludedPaths = new Collection <IncludedPath>() { new IncludedPath { Path = "/*", Indexes = new Collection <Index>() { new RangeIndex(DataType.String) { Precision = -1 }, new RangeIndex(DataType.Number) { Precision = -1 }, new SpatialIndex(DataType.Point) } } } }; Endpoint = endpoint; AuthKey = authKey; ConnectionPolicy = connectionPolicy; CollectionId = collectionId; PartitionKey = partitionKey; DatabaseId = databaseId; }
/// <summary> /// CosmosDBのコンテナを作成(既にある場合は何もしない) /// </summary> /// <param name="cosmosDbClient"></param> /// <param name="databaseId"></param> /// <param name="containerId"></param> /// <param name="throughput"></param> /// <param name="partitionKeyPath"></param> /// <param name="indexPolicy"></param> /// <param name="uniqueKeys"></param> /// <returns></returns> private static async Task <bool> CreateCosmosDbContainerIfNotExistsAsync(CosmosClient cosmosDbClient, string databaseId, string containerId, int throughput = 400, string partitionKeyPath = "", IndexingPolicy indexPolicy = null, Collection <UniqueKey> uniqueKeys = null ) { var properties = new ContainerProperties(containerId, partitionKeyPath) { //データの有効期限(秒) //DefaultTimeToLive = 30 }; //インデックスポリシー properties.IndexingPolicy = indexPolicy; // ユニークキー uniqueKeys ??= new Collection <UniqueKey>(); if (uniqueKeys.Any()) { foreach (var key in uniqueKeys) { properties.UniqueKeyPolicy.UniqueKeys.Add(key); } } //コンテナの作成 var result = await cosmosDbClient.GetDatabase(databaseId).CreateContainerIfNotExistsAsync(properties, throughput); return(result.StatusCode == HttpStatusCode.Created); }
private static DocumentCollection GetDocumentCollection( string collectionId, string partitionKey = null, IndexingMode indexingMode = IndexingMode.Consistent) { var documentCollection = new DocumentCollection(); documentCollection.Id = collectionId; var rangeIndex = new RangeIndex(DataType.String); rangeIndex.Precision = -1; var indexingPolicy = new IndexingPolicy(rangeIndex); indexingPolicy.IndexingMode = indexingMode; documentCollection.IndexingPolicy = indexingPolicy; if (partitionKey != null) { var partitionKeyDefinition = new PartitionKeyDefinition(); partitionKeyDefinition.Paths = new Collection <string> { partitionKey }; documentCollection.PartitionKey = partitionKeyDefinition; } return(documentCollection); }
private void SetupIdentityResourceOptions() { _identityResourcesUri = UriFactory.CreateDocumentCollectionUri(Database.Id, Constants.CollectionNames.IdentityResource); var partitionKeyDefinition = new PartitionKeyDefinition(); partitionKeyDefinition.Paths.Add(Constants.PartitionKey); var indexingPolicy = new IndexingPolicy { Automatic = true, IndexingMode = IndexingMode.Consistent }; indexingPolicy.IncludedPaths.Add(new IncludedPath { Path = "/Name", Indexes = { new RangeIndex(DataType.String), new HashIndex(DataType.String) } }); var identityResources = new DocumentCollection { Id = Constants.CollectionNames.IdentityResource, PartitionKey = partitionKeyDefinition, IndexingPolicy = indexingPolicy }; _identityResources = DocumentClient .CreateDocumentCollectionIfNotExistsAsync(_identityResourcesUri, identityResources).Result; }
/// <summary> /// Update the indexing policy for a collection. /// </summary> /// <param name="policy"></param> /// <returns></returns> public async Task <ResourceResponse <Microsoft.Azure.Documents.DocumentCollection> > UpdateIndexingPolicyAsync( IndexingPolicy policy) { // DISCUSS: moving this into its own method. Seems in line with being a method on collection, and because // some other properties are better immutable. throw new NotImplementedException(); }
private async Task CreateCollectionAsync() { var databaseUri = UriFactory.CreateDatabaseUri(_database); var pks = new Collection <string>(new[] { "/pk" }); var partitionKeyDefinition = new PartitionKeyDefinition() { Paths = pks }; var indexingPolicy = new IndexingPolicy(new Index[] { new RangeIndex(DataType.Number, -1), new RangeIndex(DataType.String, -1), }); var collection = new DocumentCollection() { Id = _collection, PartitionKey = partitionKeyDefinition, IndexingPolicy = indexingPolicy }; var options = new RequestOptions() { OfferThroughput = 400 }; var response = await DocumentClient.CreateDocumentCollectionAsync(databaseUri, collection, options); var code = response.StatusCode; Check.That(code).IsEqualTo(HttpStatusCode.Created); }
/// <summary> /// Create a collection with the required indexing policies for Order By against any numeric or string property. /// Note that the default policy allows Order By only against numbers. /// </summary> /// <param name="database">The database to create the collection in.</param> /// <returns>The created collection.</returns> private async Task <DocumentCollection> CreateCollectionForOrderBy(Database database) { IndexingPolicy orderByPolicy = new IndexingPolicy(); orderByPolicy.IncludedPaths.Add(new IncludedPath { Path = "/*", Indexes = new System.Collections.ObjectModel.Collection <Index>() { new RangeIndex(DataType.String) { Precision = -1 }, new RangeIndex(DataType.Number) { Precision = -1 } } }); // Here we create as a S1. DocumentCollection collection = await DocumentClientHelper.CreateNewCollection( this.client, database, "tweetsCollection", new DocumentCollectionSpec { IndexingPolicy = orderByPolicy, OfferType = "S1" }); return(collection); }
private async Task CreateCollectionAsync(string cosmosDatabase, string cosmosCollection, int docDBRUs, string operation) { try { var collection = new DocumentCollection { Id = cosmosCollection }; var index = Microsoft.Azure.Documents.Index.Range(DataType.String, -1); var indexing = new IndexingPolicy(index) { IndexingMode = IndexingMode.Consistent }; collection.IndexingPolicy = indexing; if (operation.ToString().Equals("device")) { collection.PartitionKey.Paths.Add("/deviceId"); } var dbUri = "/dbs/" + cosmosDatabase; this.requestOptions = this.GetDocDbOptions(docDBRUs); await this.client.CreateDocumentCollectionAsync(dbUri, collection, this.requestOptions); } catch (DocumentClientException) { // If the collection is already created it will throw a DocumentClientException and we do not want to handle that // So we do not throw this exception } catch (Exception e) { throw new ApplicationException("Error while creating DocumentDb collection", e); } }
private IndexingPolicy CovertToCosmosIndexingPolicy(CustomIndexingPolicy indexingPolicy) { var cosmosIndexingPolicy = new IndexingPolicy { IndexingMode = indexingPolicy.Mode, Automatic = indexingPolicy.Mode == IndexingMode.Consistent ? indexingPolicy.Autmatic : false }; if (indexingPolicy.Mode == IndexingMode.None) { return(cosmosIndexingPolicy); } foreach (var includedPath in indexingPolicy.IncludedPaths) { cosmosIndexingPolicy.IncludedPaths.Add(new IncludedPath { Path = includedPath }); } foreach (var excludedPath in indexingPolicy.ExcludedPaths) { cosmosIndexingPolicy.ExcludedPaths.Add(new ExcludedPath { Path = excludedPath }); } return(cosmosIndexingPolicy); }
public async Task CompositeIndex() { IndexingPolicy indexingPolicy = new IndexingPolicy() { Automatic = true, IncludedPaths = new Collection <IncludedPath>() { new IncludedPath() { Path = DefaultPath, } }, ExcludedPaths = new Collection <ExcludedPath>(), IndexingMode = IndexingMode.Consistent, CompositeIndexes = new Collection <Collection <CompositePath> >() { new Collection <CompositePath>() { new CompositePath() { Path = "/name", Order = CompositePathSortOrder.Ascending }, new CompositePath() { Path = "/age", Order = CompositePathSortOrder.Descending } } } }; await IndexingPolicyTests.RoundTripWithLocal(indexingPolicy); }
/// <summary> /// Create a collection with the required indexing policies for Order By against specific properties. /// </summary> /// <param name="database">The database to create the collection in.</param> /// <returns>The created collection.</returns> private async Task <DocumentCollection> CreateCollectionForOrderBySinglePath(Database database) { IndexingPolicy orderByPolicy = new IndexingPolicy(); // Index the createdAt property for Order By orderByPolicy.IncludedPaths.Add(new IndexingPath { Path = "/\"createdAt\"/?", IndexType = IndexType.Range, NumericPrecision = -1 }); // Index all numeric paths under "user" for Order By orderByPolicy.IncludedPaths.Add(new IndexingPath { Path = "/\"user\"/*", IndexType = IndexType.Range, NumericPrecision = -1 }); // Use the default (Hash) for everything else. orderByPolicy.IncludedPaths.Add(new IndexingPath { Path = "/" }); // Here we create as a S1. DocumentCollection collection = await DocumentClientHelper.CreateNewCollection( this.client, database, "tweetsCollectionSinglePath", new DocumentCollectionInfo { IndexingPolicy = orderByPolicy, OfferType = "S1" }); return(collection); }
/// <summary> /// The create document collection if not exists async. /// </summary> /// <param name="indexingPolicy">The indexing policy</param> /// <returns> /// The <see cref="Task"/>. /// </returns> public async Task CreateDocumentCollectionIfNotExistsAsync(IndexingPolicy indexingPolicy = null) { try { await this.documentDbClient.ReadDocumentCollectionAsync( UriFactory.CreateDocumentCollectionUri(this.databaseName, this.collectionName)) .ConfigureAwait(false); } catch (DocumentClientException de) { // If the document collection does not exist, create a new collection if (de.StatusCode == HttpStatusCode.NotFound) { var collectionInfo = new DocumentCollection { Id = this.collectionName, IndexingPolicy = indexingPolicy ?? new IndexingPolicy(new RangeIndex(DataType.String) { Precision = -1 }) }; await this.documentDbClient.CreateDocumentCollectionAsync( UriFactory.CreateDatabaseUri(this.databaseName), collectionInfo).ConfigureAwait(false); } else { throw; } } }
private static async Task <Container> InitializeContainer(Database database, string containerId) { IndexingPolicy indexingPolicy = new IndexingPolicy { IndexingMode = IndexingMode.Consistent, Automatic = true, IncludedPaths = { new IncludedPath { Path = "/*" } }, ExcludedPaths = { new ExcludedPath { Path = "/\"_etag\"/?" } } }; ContainerProperties containerProperties = new ContainerProperties(containerId, "/type") { IndexingPolicy = indexingPolicy }; Container container = await database.CreateContainerIfNotExistsAsync(containerProperties, 400); await Console.Out.WriteLineAsync($"Container Id:\t{container.Id}"); return(container); }
private static DocumentCollection CloneCollectionConfigs(DocumentCollection coll, bool enableIndexing) { DocumentCollection collectionDefinition = new DocumentCollection(); collectionDefinition.Id = coll.Id; if (coll.PartitionKey.Paths.Count > 0) { foreach (string path in coll.PartitionKey.Paths) { collectionDefinition.PartitionKey.Paths.Add(path); } } if (enableIndexing) { collectionDefinition.IndexingPolicy = coll.IndexingPolicy; } else { IndexingPolicy noIndexing = new IndexingPolicy(); noIndexing.IndexingMode = IndexingMode.None; noIndexing.Automatic = false; collectionDefinition.IndexingPolicy = noIndexing; } collectionDefinition.DefaultTimeToLive = coll.DefaultTimeToLive; return(collectionDefinition); }
public static IndexingPolicy ConvertPSIndexingToIndexingPolicy(PSIndexingPolicy pSIndexingPolicy) { IndexingPolicy indexingPolicy = new IndexingPolicy { Automatic = pSIndexingPolicy.Automatic, IndexingMode = pSIndexingPolicy.IndexingMode, }; if (pSIndexingPolicy.IncludedPaths != null) { IList <IncludedPath> includedPaths = new List <IncludedPath>(); foreach (PSIncludedPath pSIncludedPath in pSIndexingPolicy.IncludedPaths) { includedPaths.Add(PSIncludedPath.ConvertPSIncludedPathToIncludedPath(pSIncludedPath)); } indexingPolicy.IncludedPaths = includedPaths; } if (pSIndexingPolicy.ExcludedPaths != null && pSIndexingPolicy.ExcludedPaths.Count > 0) { IList <ExcludedPath> excludedPaths = new List <ExcludedPath>(); foreach (PSExcludedPath pSExcludedPath in pSIndexingPolicy.ExcludedPaths) { excludedPaths.Add(PSExcludedPath.ConvertPSExcludedPathToExcludedPath(pSExcludedPath)); } indexingPolicy.ExcludedPaths = excludedPaths; } if (pSIndexingPolicy.CompositeIndexes != null) { IList <IList <CompositePath> > compositeIndexes = new List <IList <CompositePath> >(); foreach (IList <PSCompositePath> pSCompositePathList in pSIndexingPolicy.CompositeIndexes) { IList <CompositePath> compositePathList = new List <CompositePath>(); foreach (PSCompositePath pSCompositePath in pSCompositePathList) { compositePathList.Add(PSCompositePath.ConvertPSCompositePathToCompositePath(pSCompositePath)); } compositeIndexes.Add(compositePathList); } indexingPolicy.CompositeIndexes = compositeIndexes; } if (pSIndexingPolicy.SpatialIndexes != null && pSIndexingPolicy.SpatialIndexes.Count > 0) { IList <SpatialSpec> spatialIndexes = new List <SpatialSpec>(); foreach (PSSpatialSpec pSSpatialSpec in pSIndexingPolicy.SpatialIndexes) { spatialIndexes.Add(PSSpatialSpec.ConvertPSSpatialSpecToSpatialSpec(pSSpatialSpec)); } indexingPolicy.SpatialIndexes = new List <SpatialSpec>(spatialIndexes); } return(indexingPolicy); }
public async Task <ResourceResponse <Microsoft.Azure.Documents.DocumentCollection> > CreateAsync( OfferName offerName, IndexingPolicy indexingPolicy = null) { // DISCUSS: create enum for S1,S2,S3 now that it isn't going to grow. Support this, but // mark obsolete for the future. throw new NotImplementedException(); }
public Task <string> GetOrCreateCollectionAsync( string collectionName, string partitionKey, int desiredThroughput, IndexingPolicy indexingPolicy, CancellationToken cancellation) { return(GetOrCreateCollectionAsyncInternal(collectionName, partitionKey, new RequestOptions { OfferThroughput = desiredThroughput }, indexingPolicy, cancellation)); }
public Task <string> GetOrCreateCollectionAsync( string collectionName, string partitionKey, int desiredThroughput, IndexingPolicy indexingPolicy, CancellationToken cancellation) { Assert.IsFalse(String.IsNullOrEmpty(collectionName), TestResources.MissingCollectionNameInGetOrCreateCollection); createdCollections.Add(collectionName); return(Task.FromResult(collectionName)); }
public async Task <IActionResult> Run( [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req, ILogger log) { log.LogInformation("初期処理を開始"); { log.LogInformation(await CreateCosmosDbDatabaseIfNotExistsAsync(_cosmosDbClient, _settings.DatabaseId) ? $"CosmosDBのデータベースを作成しました。 データベース名:`{_settings.DatabaseId}`" : $"データベース名: `{_settings.DatabaseId}` はすでに存在します。"); var indexPolicy = new IndexingPolicy { IndexingMode = IndexingMode.Consistent, Automatic = true }; //IncludePathの指定 indexPolicy.IncludedPaths.Add(new IncludedPath { Path = $"/*" }); ////ExcludePathの指定 indexPolicy.ExcludedPaths.Add(new ExcludedPath { Path = "/name/?" }); //UniqueKeyの指定 var uniqueKeys = new Collection <UniqueKey> { new UniqueKey { Paths = { "/personalId" } } }; int.TryParse(_settings.Throughput, out var throughput); var createContainerResult = await CreateCosmosDbContainerIfNotExistsAsync( _cosmosDbClient, _settings.DatabaseId, _settings.ContainerId, throughput : throughput, partitionKeyPath : _settings.PartitionKey, indexPolicy : indexPolicy, uniqueKeys : uniqueKeys); log.LogInformation(createContainerResult ? $"CosmosDBのコンテナを作成しました。 コンテナ名:`{_settings.ContainerId}`" : $"データベース名: `{_settings.ContainerId}` はすでに存在します。"); } log.LogInformation("初期処理が完了しました。"); return(new OkObjectResult("")); }
/// <summary> /// Create a collection - the primary interface for elastic collections /// </summary> /// <param name="partitioningPolicy"></param> /// <param name="throughputRequestUnits"></param> /// <param name="indexingPolicy"></param> /// <returns></returns> public async Task <ResourceResponse <Microsoft.Azure.Documents.DocumentCollection> > CreateAsync( PartitioningPolicy partitioningPolicy, int throughputRequestUnits, IndexingPolicy indexingPolicy = null) { // DISCUSS: required parameters are partitioningPolicy and throughputRequestUnits throw new NotImplementedException(); }
public static DocumentCollection GetOrCreateDocumentCollection(DocumentClient client, string databaseLink, string collectionId, IndexingPolicy indexingPolicy) { IQueryable<DocumentCollection> collectionQuery = from coll in client.CreateDocumentCollectionQuery(databaseLink) where coll.Id == collectionId select coll; IEnumerable<DocumentCollection> enumerable = collectionQuery.AsEnumerable(); if (!enumerable.Any()) { DocumentCollection collection = new DocumentCollection() { Id = collectionId }; if (indexingPolicy != null) { collection.IndexingPolicy.Automatic = indexingPolicy.Automatic; collection.IndexingPolicy.IndexingMode = indexingPolicy.IndexingMode; foreach (var path in indexingPolicy.IncludedPaths) { collection.IndexingPolicy.IncludedPaths.Add(path); } foreach (var path in indexingPolicy.ExcludedPaths) { collection.IndexingPolicy.ExcludedPaths.Add(path); } } return client.CreateDocumentCollectionAsync(databaseLink, collection).Result.Resource; } else { return enumerable.First(); } }
private static async Task UseRangeIndexesOnStrings() { var collectionUri = UriFactory.CreateDocumentCollectionUri(databaseId, collectionId); Console.WriteLine("\n6. Use range indexes on strings"); var collDefinition = new DocumentCollection { Id = collectionId }; // This is how you can specify a range index on strings (and numbers) for all properties. This is the recommended indexing policy for collections. IndexingPolicy indexingPolicy = new IndexingPolicy(new RangeIndex(DataType.String) { Precision = -1 }); // For demo purposes, we are going to use the default (range on numbers, hash on strings) for the whole document (/* ) // and just include a range index on strings for the "region". indexingPolicy = new IndexingPolicy(); indexingPolicy.IncludedPaths.Add(new IncludedPath { Path = "/*" }); indexingPolicy.IncludedPaths.Add(new IncludedPath { Path = "/region/?", Indexes = new Collection<Index>() { new RangeIndex(DataType.String) { Precision = -1 } } }); collDefinition.IndexingPolicy = indexingPolicy; var collection = await DocumentClientHelper.CreateDocumentCollectionWithRetriesAsync(client, databaseId, collDefinition); Console.WriteLine("Collection {0} created with index policy \n{1}", collection.Id, collection.IndexingPolicy); await client.CreateDocumentAsync(collection.SelfLink, new { id = "doc1", region = "USA" }); await client.CreateDocumentAsync(collection.SelfLink, new { id = "doc2", region = "UK" }); await client.CreateDocumentAsync(collection.SelfLink, new { id = "doc3", region = "Armenia" }); await client.CreateDocumentAsync(collection.SelfLink, new { id = "doc4", region = "Egypt" }); // Now ordering against region is allowed. You can run the following query Console.WriteLine("Documents ordered by region"); foreach (var doc in client.CreateDocumentQuery(collectionUri, "SELECT * FROM orders o ORDER BY o.region")) { Console.WriteLine(doc); } // You can also perform filters against string comparisons like >= 'UK'. Note that you can perform a prefix query, // the equivalent of LIKE 'U%' (is >= 'U' AND < 'U') Console.WriteLine("Documents with region begining with U"); foreach (var doc in client.CreateDocumentQuery(collection.SelfLink, "SELECT * FROM orders o WHERE o.region >= 'U'")) { Console.WriteLine(doc); } // Cleanup await client.DeleteDocumentCollectionAsync(collection.SelfLink); }
private IndexingPolicy GetIndexingPolicy() { if (string.IsNullOrWhiteSpace(SpatialIndexPath)) { return null; } var pol = new IndexingPolicy { IncludedPaths = new System.Collections.ObjectModel.Collection<IncludedPath>() { new IncludedPath { Path = SpatialIndexPath, Indexes = new System.Collections.ObjectModel.Collection<Index>() { new SpatialIndex(DataType.Point), new RangeIndex(DataType.Number) {Precision = -1}, new RangeIndex(DataType.String) {Precision = -1} }, }, new IncludedPath { Path = "/*", Indexes = new System.Collections.ObjectModel.Collection<Index>() { new RangeIndex(DataType.Number) {Precision = -1}, new RangeIndex(DataType.String) {Precision = -1} }, } } }; return pol; }
private static async Task UseRangeIndexesOnStrings(Database database) { Console.WriteLine("Trying Range index on strings. This enables Order By and range queries on strings."); var collection = new DocumentCollection { Id = ConfigurationManager.AppSettings["CollectionId"] }; // Overide to Range, Max (-1) for Strings. This allows you to perform string range queries and string order by queries. // Note that this might have a higher index storage overhead however if you have long strings or a large number of unique // strings. You can be selective of which paths need a Range index through IncludedPath configuration, collection.IndexingPolicy.IncludedPaths.Add(new IncludedPath { Path = "/*", Indexes = new Collection<Index>() { new RangeIndex(DataType.Number) { Precision = -1 }, new RangeIndex(DataType.String) { Precision = -1 } } }); // Alternatively, you can use the default for /* and just range for the "region". // Not creating collection with this in the sample, but this can be used instead. IndexingPolicy alternateIndexingPolicy = new IndexingPolicy(); alternateIndexingPolicy.IncludedPaths.Add(new IncludedPath { Path = "/*" }); alternateIndexingPolicy.IncludedPaths.Add(new IncludedPath { Path = "/region/?", Indexes = new Collection<Index>() { new RangeIndex(DataType.Number) { Precision = -1 } } }); collection = await DocumentClientHelper.CreateDocumentCollectionWithRetriesAsync(client, database, collection); await client.CreateDocumentAsync(collection.SelfLink, new { id = "doc1", region = "USA" }); await client.CreateDocumentAsync(collection.SelfLink, new { id = "doc2", region = "UK" }); await client.CreateDocumentAsync(collection.SelfLink, new { id = "doc3", region = "Armenia" }); await client.CreateDocumentAsync(collection.SelfLink, new { id = "doc4", region = "Egypt" }); // Now ordering against region is allowed. You can run the following query foreach (var doc in client.CreateDocumentQuery( collection.SelfLink, "SELECT * FROM orders o ORDER BY o.region")) { Console.WriteLine(doc); } // You can also perform filters against string comparisons like >= 'UK'. Note that you can perform a prefix query // i.e., the equivalent of LIKE 'U%' is >='U' AND < 'U\uffff' foreach (var doc in client.CreateDocumentQuery( collection.SelfLink, "SELECT * FROM orders o WHERE o.region >= 'UK'")) { Console.WriteLine(doc); } // Cleanup await client.DeleteDocumentCollectionAsync(collection.SelfLink); Console.WriteLine("Done with Range indexing on strings."); }
/// <summary> /// Create a collection with the required indexing policies for Order By against any numeric or string property. /// Note that the default policy allows Order By only against numbers. /// </summary> /// <param name="database">The database to create the collection in.</param> /// <returns>The created collection.</returns> private async Task<DocumentCollection> CreateCollectionForOrderBy(Database database) { IndexingPolicy orderByPolicy = new IndexingPolicy(); orderByPolicy.IncludedPaths.Add(new IncludedPath { Path = "/*", Indexes = new System.Collections.ObjectModel.Collection<Index>() { new RangeIndex(DataType.String) { Precision = -1 }, new RangeIndex(DataType.Number) { Precision = -1 } } }); // Here we create as a S1. DocumentCollection collection = await DocumentClientHelper.CreateNewCollection( this.client, database, "tweetsCollection", new DocumentCollectionSpec { IndexingPolicy = orderByPolicy, OfferType = "S1" }); return collection; }
/// <summary> /// Modify a collection to use spatial indexing policy and wait for it to complete. /// </summary> /// <param name="collection">The DocumentDB collection.</param> /// <param name="indexingPolicy">The indexing policy to use.</param> /// <returns>The Task for asynchronous execution.</returns> private static async Task ModifyCollectionWithSpatialIndexing(DocumentCollection collection, IndexingPolicy indexingPolicy) { Console.WriteLine("Updating collection with spatial indexing enabled in indexing policy..."); collection.IndexingPolicy = indexingPolicy; await client.ReplaceDocumentCollectionAsync(collection); }
/// <summary> /// Create a collection with the required indexing policies for Order By against specific properties. /// </summary> /// <param name="database">The database to create the collection in.</param> /// <returns>The created collection.</returns> private async Task<DocumentCollection> CreateCollectionForOrderBySinglePath(Database database) { IndexingPolicy orderByPolicy = new IndexingPolicy(); // Index the createdAt property for Order By orderByPolicy.IncludedPaths.Add(new IncludedPath { Path = "/createdAt/?", Indexes = new System.Collections.ObjectModel.Collection<Index>() { new RangeIndex(DataType.Number) { Precision = -1 } } }); // Index all string and numeric paths under "user" for Order By orderByPolicy.IncludedPaths.Add(new IncludedPath { Path = "/user/*", Indexes = new System.Collections.ObjectModel.Collection<Index>() { new RangeIndex(DataType.Number) { Precision = -1 }, new RangeIndex(DataType.String) { Precision = -1 }, } }); // Use the default (Hash) for everything else. orderByPolicy.IncludedPaths.Add(new IncludedPath { Path = "/*" }); // Here we create as a S1. DocumentCollection collection = await DocumentClientHelper.CreateNewCollection( this.client, database, "tweetsCollectionSinglePath", new DocumentCollectionSpec { IndexingPolicy = orderByPolicy, OfferType = "S1" }); return collection; }
private static async Task UseRangeIndexesOnStrings() { var collectionUri = UriFactory.CreateDocumentCollectionUri(databaseId, collectionId); Console.WriteLine("\n6. Use range indexes on strings"); var collDefinition = new DocumentCollection { Id = collectionId }; // We COULD create a Range index for ALL paths for Numbers AND Strings. // This indexing policy gives you the most flexibility as it allows you to perform range queries and order by on strings and number on the whole document. // This might have a higher index storage overhead if you have long strings or a large number of unique strings. // You can be selective of which paths need a Range index through IncludedPath configuration IndexingPolicy indexingPolicy = new IndexingPolicy(); indexingPolicy.IncludedPaths.Add(new IncludedPath { Path = "/*", Indexes = new Collection<Index>() { new RangeIndex(DataType.Number) { Precision = -1 }, new RangeIndex(DataType.String) { Precision = -1 } } }); // So instead, for demo purposes, we are going to use the default (range on numbers, hash on strings) for the whole document (/* ) // and just include a range index on strings for the "region". indexingPolicy = new IndexingPolicy(); indexingPolicy.IncludedPaths.Add(new IncludedPath { Path = "/*" }); indexingPolicy.IncludedPaths.Add(new IncludedPath { Path = "/region/?", Indexes = new Collection<Index>() { new RangeIndex(DataType.String) { Precision = -1 } } }); collDefinition.IndexingPolicy = indexingPolicy; var collection = await DocumentClientHelper.CreateDocumentCollectionWithRetriesAsync(client, databaseId, collDefinition); Console.WriteLine("Collection {0} created with index policy \n{1}", collection.Id, collection.IndexingPolicy); await client.CreateDocumentAsync(collection.SelfLink, new { id = "doc1", region = "USA" }); await client.CreateDocumentAsync(collection.SelfLink, new { id = "doc2", region = "UK" }); await client.CreateDocumentAsync(collection.SelfLink, new { id = "doc3", region = "Armenia" }); await client.CreateDocumentAsync(collection.SelfLink, new { id = "doc4", region = "Egypt" }); // Now ordering against region is allowed. You can run the following query Console.WriteLine("Documents ordered by region"); foreach (var doc in client.CreateDocumentQuery(collectionUri, "SELECT * FROM orders o ORDER BY o.region")) { Console.WriteLine(doc); } // You can also perform filters against string comparisons like >= 'UK'. Note that you can perform a prefix query, // the equivalent of LIKE 'U%' (is >= 'U' AND < 'U') Console.WriteLine("Documents with region begining with U"); foreach (var doc in client.CreateDocumentQuery(collection.SelfLink, "SELECT * FROM orders o WHERE o.region >= 'U'")) { Console.WriteLine(doc); } // Cleanup await client.DeleteDocumentCollectionAsync(collection.SelfLink); }
/// <summary> /// Get a DocuemntCollection by id, or create a new one if one with the id provided doesn't exist. /// </summary> /// <param name="id">The id of the DocumentCollection to search for, or create.</param> /// <returns>The matched, or created, DocumentCollection object</returns> private static async Task<DocumentCollection> GetOrCreateCollectionAsync(Database db, string id) { DocumentCollection collection = client.CreateDocumentCollectionQuery(db.SelfLink).Where(c => c.Id == id).ToArray().FirstOrDefault(); if (collection == null) { IndexingPolicy optimalQueriesIndexingPolicy = new IndexingPolicy(); optimalQueriesIndexingPolicy.IncludedPaths.Add(new IncludedPath { Path = "/*", Indexes = new System.Collections.ObjectModel.Collection<Index>() { new RangeIndex(DataType.Number) { Precision = -1 }, new RangeIndex(DataType.String) { Precision = -1 } } }); DocumentCollection collectionDefinition = new DocumentCollection { Id = id }; collectionDefinition.IndexingPolicy = optimalQueriesIndexingPolicy; collection = await DocumentClientHelper.CreateDocumentCollectionWithRetriesAsync(client, db, collectionDefinition); } return collection; }
/// <summary> /// Modify a collection to use spatial indexing policy and wait for it to complete. /// </summary> /// <param name="collection">The DocumentDB collection.</param> /// <param name="indexingPolicy">The indexing policy to use.</param> /// <returns>The Task for asynchronous execution.</returns> private static async Task ModifyCollectionWithSpatialIndexing(DocumentCollection collection, IndexingPolicy indexingPolicy) { Console.WriteLine("Updating collection with spatial indexing enabled in indexing policy..."); collection.IndexingPolicy = indexingPolicy; await client.ReplaceDocumentCollectionAsync(collection); Console.WriteLine("waiting for indexing to complete..."); long indexTransformationProgress = 0; while (indexTransformationProgress < 100) { ResourceResponse<DocumentCollection> response = await client.ReadDocumentCollectionAsync(collection.SelfLink); indexTransformationProgress = response.IndexTransformationProgress; await Task.Delay(TimeSpan.FromSeconds(1)); } }