// Modifying geospatial indexing on a container private static async Task ModifyContainerWithSpatialIndexingAsync() { Container containerToUpdate = cosmosClient.GetContainer(databaseId, containerId); ContainerProperties updateContainerProperties = new ContainerProperties(containerId, Program.partitionKey); //Changing the Geopspatial Config from the deafult of geography to geometry. This change will need to include the addition of a bounding box. GeospatialConfig geospatialConfig = new GeospatialConfig(GeospatialType.Geometry); updateContainerProperties.GeospatialConfig = geospatialConfig; SpatialPath locationPath = new SpatialPath { Path = "/location/?", BoundingBox = new BoundingBoxProperties() { Xmin = 30, Ymin = -10, Xmax = 40, Ymax = 10 } }; Console.WriteLine("Updating CONTAINER w/ new spatial index..."); locationPath.SpatialTypes.Add(SpatialType.Point); locationPath.SpatialTypes.Add(SpatialType.LineString); locationPath.SpatialTypes.Add(SpatialType.Polygon); updateContainerProperties.IndexingPolicy.SpatialIndexes.Add(locationPath); await containerToUpdate.ReplaceContainerAsync(updateContainerProperties); }
private async Task <Container> GetContainerReference() { var database = (await _cosmosClient.CreateDatabaseIfNotExistsAsync(_configuration.DbName)).Database; ContainerProperties containerProperties = new ContainerProperties() { Id = _configuration.DbContainerName, PartitionKeyPath = "/docType", IndexingPolicy = new IndexingPolicy() { Automatic = true, IndexingMode = IndexingMode.Consistent }, }; var spaitalPath = new SpatialPath() { Path = "/location/*" }; spaitalPath.SpatialTypes.Add(SpatialType.Point); containerProperties.IndexingPolicy.SpatialIndexes.Add(spaitalPath); var containerResponse = await database.CreateContainerIfNotExistsAsync(containerProperties); if (containerResponse.StatusCode == HttpStatusCode.OK || containerResponse.StatusCode == HttpStatusCode.Created) { return(containerResponse.Container); } else { throw new Exception($"{containerResponse.StatusCode}-{containerResponse.StatusCode.ToString()}"); } }
// Geospatial indexing on a container private static async Task <Container> GetContainerWithSpatialIndexingAsync() { ContainerProperties spatialContainerProperties = new ContainerProperties(containerId, Program.partitionKey); SpatialPath locationPath = new SpatialPath { Path = "/location/?" }; Console.WriteLine("Creating new CONTAINER w/ spatial index..."); locationPath.SpatialTypes.Add(SpatialType.Point); spatialContainerProperties.IndexingPolicy.SpatialIndexes.Add(locationPath); Container simpleContainer = await database.CreateContainerIfNotExistsAsync(spatialContainerProperties); return(simpleContainer); }
protected override ContainerProperties GetContainerProperties() { var properties = new ContainerProperties { Id = ContainerId, PartitionKeyPath = PartitionKeyPath, GeospatialConfig = new GeospatialConfig(GeospatialType.Geography) }; properties.IndexingPolicy.IncludedPaths.Add(new IncludedPath { Path = "/*" }); var locationPath = new SpatialPath { Path = "/location/?" }; locationPath.SpatialTypes.Clear(); locationPath.SpatialTypes.Add(SpatialType.Point); properties.IndexingPolicy.SpatialIndexes.Add(locationPath); return(properties); }
/// <summary> /// Create necessary indexes /// </summary> /// <param name="containerResponse"></param> /// <returns></returns> private async Task <Container> CreateIndexes(ContainerResponse containerResponse) { var indexingPolicy = containerResponse.Resource.IndexingPolicy; indexingPolicy.IndexingMode = IndexingMode.Consistent; // Add a spatial index if (indexingPolicy.SpatialIndexes.Count == 0) { SpatialPath spatialPath = new SpatialPath { Path = "/location/*" }; spatialPath.SpatialTypes.Add(SpatialType.Point); indexingPolicy.SpatialIndexes.Add(spatialPath); } // Add a composite index if (indexingPolicy.CompositeIndexes.Count == 0) { indexingPolicy.CompositeIndexes.Add( new Collection <CompositePath> { new CompositePath() { Path = "/name", Order = CompositePathSortOrder.Ascending }, new CompositePath() { Path = "/founded", Order = CompositePathSortOrder.Ascending } }); } return(await containerResponse.Container.ReplaceContainerAsync(containerResponse.Resource)); }
public async Task ContainerContractTest() { ClientEncryptionIncludedPath clientEncryptionIncludedPath1 = new ClientEncryptionIncludedPath() { Path = "/path", ClientEncryptionKeyId = "dekId", EncryptionAlgorithm = "AEAD_AES_256_CBC_HMAC_SHA256", EncryptionType = "Randomized" }; Collection <ClientEncryptionIncludedPath> paths = new Collection <ClientEncryptionIncludedPath>() { clientEncryptionIncludedPath1 }; ContainerProperties containerProperties = new ContainerProperties(Guid.NewGuid().ToString(), "/users") { IndexingPolicy = new IndexingPolicy() { Automatic = true, IndexingMode = IndexingMode.Consistent, IncludedPaths = new Collection <IncludedPath>() { new IncludedPath() { Path = "/*" } }, ExcludedPaths = new Collection <ExcludedPath>() { new ExcludedPath() { Path = "/test/*" } }, CompositeIndexes = new Collection <Collection <CompositePath> >() { new Collection <CompositePath>() { new CompositePath() { Path = "/address/city", Order = CompositePathSortOrder.Ascending }, new CompositePath() { Path = "/address/zipcode", Order = CompositePathSortOrder.Descending } } }, SpatialIndexes = new Collection <SpatialPath>() { new SpatialPath() { Path = "/address/spatial/*", SpatialTypes = new Collection <SpatialType>() { SpatialType.LineString } } } }, ClientEncryptionPolicy = new ClientEncryptionPolicy(paths) }; CosmosJsonDotNetSerializer serializer = new CosmosJsonDotNetSerializer(); Stream stream = serializer.ToStream(containerProperties); ContainerProperties deserialziedTest = serializer.FromStream <ContainerProperties>(stream); ContainerResponse response = await this.database.CreateContainerAsync(containerProperties); Assert.IsNotNull(response); Assert.IsTrue(response.RequestCharge > 0); Assert.IsNotNull(response.Headers); Assert.IsNotNull(response.Headers.ActivityId); ContainerProperties responseProperties = response.Resource; Assert.IsNotNull(responseProperties.Id); Assert.IsNotNull(responseProperties.ResourceId); Assert.IsNotNull(responseProperties.ETag); Assert.IsTrue(responseProperties.LastModified.HasValue); Assert.IsTrue(responseProperties.LastModified.Value > new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc), responseProperties.LastModified.Value.ToString()); Assert.AreEqual(1, responseProperties.IndexingPolicy.IncludedPaths.Count); IncludedPath includedPath = responseProperties.IndexingPolicy.IncludedPaths.First(); Assert.AreEqual("/*", includedPath.Path); Assert.AreEqual("/test/*", responseProperties.IndexingPolicy.ExcludedPaths.First().Path); Assert.AreEqual(1, responseProperties.IndexingPolicy.CompositeIndexes.Count); Assert.AreEqual(2, responseProperties.IndexingPolicy.CompositeIndexes.First().Count); CompositePath compositePath = responseProperties.IndexingPolicy.CompositeIndexes.First().First(); Assert.AreEqual("/address/city", compositePath.Path); Assert.AreEqual(CompositePathSortOrder.Ascending, compositePath.Order); Assert.AreEqual(1, responseProperties.IndexingPolicy.SpatialIndexes.Count); SpatialPath spatialPath = responseProperties.IndexingPolicy.SpatialIndexes.First(); Assert.AreEqual("/address/spatial/*", spatialPath.Path); Assert.AreEqual(4, spatialPath.SpatialTypes.Count); // All SpatialTypes are returned Assert.AreEqual(1, responseProperties.ClientEncryptionPolicy.IncludedPaths.Count()); Assert.AreEqual(1, responseProperties.ClientEncryptionPolicy.PolicyFormatVersion); ClientEncryptionIncludedPath clientEncryptionIncludedPath = responseProperties.ClientEncryptionPolicy.IncludedPaths.First(); Assert.IsTrue(this.VerifyClientEncryptionIncludedPath(clientEncryptionIncludedPath1, clientEncryptionIncludedPath)); }
private void AddSpatialPath(SpatialPath spatialSpec) { this.indexingPolicy.SpatialIndexes.Add(spatialSpec); }
public async Task ContainerContractTest() { ContainerProperties containerProperties = new ContainerProperties(Guid.NewGuid().ToString(), "/users") { IndexingPolicy = new IndexingPolicy() { Automatic = true, IndexingMode = IndexingMode.Consistent, IncludedPaths = new Collection <IncludedPath>() { new IncludedPath() { Path = "/*" } }, ExcludedPaths = new Collection <ExcludedPath>() { new ExcludedPath() { Path = "/test/*" } }, CompositeIndexes = new Collection <Collection <CompositePath> >() { new Collection <CompositePath>() { new CompositePath() { Path = "/address/city", Order = CompositePathSortOrder.Ascending }, new CompositePath() { Path = "/address/zipcode", Order = CompositePathSortOrder.Descending } } }, SpatialIndexes = new Collection <SpatialPath>() { new SpatialPath() { Path = "/address/spatial/*", SpatialTypes = new Collection <SpatialType>() { SpatialType.LineString } } } } }; var serializer = new CosmosJsonDotNetSerializer(); Stream stream = serializer.ToStream(containerProperties); ContainerProperties deserialziedTest = serializer.FromStream <ContainerProperties>(stream); ContainerResponse response = await this.database.CreateContainerAsync(containerProperties); Assert.IsNotNull(response); Assert.IsTrue(response.RequestCharge > 0); Assert.IsNotNull(response.Headers); Assert.IsNotNull(response.Headers.ActivityId); ContainerProperties responseProperties = response.Resource; Assert.IsNotNull(responseProperties.Id); Assert.IsNotNull(responseProperties.ResourceId); Assert.IsNotNull(responseProperties.ETag); Assert.IsTrue(responseProperties.LastModified.HasValue); Assert.IsTrue(responseProperties.LastModified.Value > new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc), responseProperties.LastModified.Value.ToString()); Assert.AreEqual(1, responseProperties.IndexingPolicy.IncludedPaths.Count); IncludedPath includedPath = responseProperties.IndexingPolicy.IncludedPaths.First(); Assert.AreEqual("/*", includedPath.Path); Assert.AreEqual("/test/*", responseProperties.IndexingPolicy.ExcludedPaths.First().Path); Assert.AreEqual(1, responseProperties.IndexingPolicy.CompositeIndexes.Count); Assert.AreEqual(2, responseProperties.IndexingPolicy.CompositeIndexes.First().Count); CompositePath compositePath = responseProperties.IndexingPolicy.CompositeIndexes.First().First(); Assert.AreEqual("/address/city", compositePath.Path); Assert.AreEqual(CompositePathSortOrder.Ascending, compositePath.Order); Assert.AreEqual(1, responseProperties.IndexingPolicy.SpatialIndexes.Count); SpatialPath spatialPath = responseProperties.IndexingPolicy.SpatialIndexes.First(); Assert.AreEqual("/address/spatial/*", spatialPath.Path); Assert.AreEqual(1, spatialPath.SpatialTypes.Count); Assert.AreEqual(SpatialType.LineString, spatialPath.SpatialTypes.First()); }
private async static Task SpatialIndexes() { Console.Clear(); Console.WriteLine(">>> Spatial Indexes <<<"); Console.WriteLine(); var containerDef = new ContainerProperties { Id = "spatialindexing", PartitionKeyPath = "/state", }; // Add a spatial index for the point data in the GeoJSON property /geo1 var geoPath = new SpatialPath { Path = "/geo1/?" }; geoPath.SpatialTypes.Add(SpatialType.Point); containerDef.IndexingPolicy.SpatialIndexes.Add(geoPath); await Shared.Client.GetDatabase("mydb").CreateContainerAsync(containerDef, 1000); var container = Shared.Client.GetContainer("mydb", "spatialindexing"); for (var i = 1; i <= 1000; i++) { var longitude = (i % 100 == 0 ? -73.992 : -119.417931); var latitude = (i % 100 == 0 ? 40.73104 : 36.778259); var state = (i % 100 == 0 ? "NY" : "CA"); dynamic doc = new { id = Guid.NewGuid().ToString(), title = $"Document {i}", state, geo1 = new { type = "Point", coordinates = new[] { longitude, latitude }, }, geo2 = new { type = "Point", coordinates = new[] { longitude, latitude }, }, }; await container.CreateItemAsync(doc, new PartitionKey(doc.state)); } var sql = @" SELECT * FROM c WHERE ST_DISTANCE(c.geo1, { 'type': 'Point', 'coordinates': [-73.992, 40.73104] }) <= 10" ; var result = await container.GetItemQueryIterator <dynamic>(sql).ReadNextAsync(); var list = result.ToList(); Console.WriteLine($"Query indexed spatial property Cost = {result.RequestCharge} RUs for {list.Count} results"); sql = @" SELECT * FROM c WHERE ST_DISTANCE(c.geo2, { 'type': 'Point', 'coordinates': [-73.992, 40.73104] }) <= 10" ; result = await container.GetItemQueryIterator <dynamic>(sql).ReadNextAsync(); list = result.ToList(); Console.WriteLine($"Query unindexed spatial property Cost = {result.RequestCharge} RUs for {list.Count} results"); // Delete the container await container.DeleteContainerAsync(); }