Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automatic indexing in Comos DB

I'm trying to define indexing on few properties in Comos but I'm a little bit confused about automatic indexing. As per the Cosmos DB documentation:

By default, Azure Cosmos DB automatically indexes every property for all items in your container without having to define any schema or configure secondary indexes.

Also, refer to this:

In some situations, you may want to override this automatic behavior to better suit your requirements. You can customize a container's indexing policy by setting its indexing mode, and include or exclude property paths.

What I understand from the above points is that unless we define our custom indexing policy the automatic indexing is set to true (which makes sense). If however, we defined our own include and exclude paths for indexing else it should be false.

It would probably mean that if I define the container properties as below, the Indexing Policy Automatic property should be set to false on Cosmos DB.

using Microsoft.Azure.Cosmos;  //Azure Cosmos SDK v3.3.1

 .

 .

var containerProperties = new ContainerProperties
        {
            Id = "SOME_CONTAINER_NAME",
            PartitionKeyPath = "/MY_PARTITION_KEY",
        };

 containerProperties.IndexingPolicy.IncludedPaths.Add(new IncludedPath {Path = "/\"{MY_PARTITION_KEY}\"/?"});         
     
 containerProperties.IndexingPolicy.ExcludedPaths.Add(new ExcludedPath {Path = "/*"});

However, I see that with the above configuration on CosmosDb indexing being defined with automatic set to true. Indexing Policy on Azure Portal

Are Automatic property and IncludedPaths, ExcludedPaths properties in IndexingPolicy class unrelated? If so, what does automatic property means when we have defined IncludedPaths and ExcludedPaths on indexing policy?

Edit 1

It becomes a little bit more tricky and confusing. Even after setting Automatic property to false the property remains true in the portal.

That is the below code does not seem to have any effect.

containerProperties.IndexingPolicy.Automatic = false;

Edit 2

Even if I update the automatic property from the portal settings, the value does not change. And I also do not receive any error.

like image 807
Ankit Vijay Avatar asked Nov 05 '19 02:11

Ankit Vijay


People also ask

Which 2 types of indexes are available in Cosmos DB?

Azure Cosmos DB supports two indexing modes: Consistent: The index is updated synchronously as you create, update or delete items. This means that the consistency of your read queries will be the consistency configured for the account. None: Indexing is disabled on the container.

What is indexing in Cosmos DB?

As well as different types of indexes, Cosmos DB supports two kinds of indexing modes (Consistent or None). Consistent indexes are updates synchronously as you add, update or delete items within your container, while None essentially means that indexing is disabled on the container.

Which of these situations should you set the indexing policy to none?

Indexing mode None: Indexing is disabled on the container. This is commonly used when a container is used as a pure key-value store without the need for secondary indexes. It can also be used to improve the performance of bulk operations.


1 Answers

I am from the CosmosDB Engineering Team. The "automatic" property and the Included/Excluded paths are unrelated.

The "automatic" property is deprecated for most containers now. It could be used to isolate a collection horizontally into two sets of documents - a set that is secondary-indexed and another set that is not, by overriding indexing directive on a per-document level. Besides a lack of concrete business value, setting the automatic property to false also caused inconsistencies in query results based on whether the query utilized the index (as opposed to a scan, for instance). So we have now deprecated the property (it cannot be set to false).

The "automatic indexing" that we generally refer to is the fact that all paths in all your documents in your container are indexed by default. This can be seen by the fact that the default indexing policy includes /* (everything under the 'root' path) in the IncludedPaths section. Hope this helps.

like image 161
Krishnan Sundaram Avatar answered Sep 28 '22 22:09

Krishnan Sundaram