György Gulyás
György Gulyás

Reputation: 1588

How can I get the ContainerProperties with Indexes from an existing container in CosmosDB

I created a container with container properties, and this ContainerProperties contains indexes.

How can I get back the ContainerProperties from cosmos later. I use following code:

FeedIterator<ContainerProperties> resultSet = CosmosDatabase.GetContainerQueryIterator<ContainerProperties>( ($"select * from c where c.id = \"{collectionName}\"") );
FeedResponse<ContainerProperties> queryProperties = resultSet.ReadNextAsync().Result;

ContainerProperties containerProperties = queryProperties.Resource.ToList().FirstOrDefault()

There is a ContainerProperties and the name is filled correctly, and this is what I want, but the indexes are empty. The containerProperties.IndexingPolicy.IncludedPaths and the containerProperties.IndexingPolicy.ExcludedPaths does not contains any element. But when I created it, it does.

Upvotes: 1

Views: 925

Answers (1)

Matias Quaranta
Matias Quaranta

Reputation: 15603

From the comments, you are using a preview version of the SDK, please update to the latest GA release, which is 3.0.0 at this time.

On that version, running the code that you shared, returns a ContainerProperties instance that has the IndexingPolicy populated.

ContainerProperties containerDefinition = new ContainerProperties(new Guid().ToString(), "/id");
containerDefinition.IndexingPolicy.ExcludedPaths.Add(new Cosmos.ExcludedPath()
{
    Path = "/*"
});
ContainerResponse response = await cosmosDatabase.CreateContainerAsync(containerDefinition);
FeedIterator<ContainerProperties> resultSet = cosmosDatabase.GetContainerQueryIterator<ContainerProperties>(($"select * from c where c.id = \"{response.Container.Id}\""));
FeedResponse<ContainerProperties> queryProperties = resultSet.ReadNextAsync().Result;
ContainerProperties containerSettings = queryProperties.Resource.ToList().FirstOrDefault();
Assert.AreEqual(containerDefinition.IndexingPolicy.ExcludedPaths.First().Path, containerSettings.IndexingPolicy.ExcludedPaths.First().Path);

Upvotes: 1

Related Questions