Review the default index policy for an Azure Cosmos DB for NoSQL container with the portal

Every container in Azure Cosmos DB has an indexing policy that directs the service on how to index items within the container. By default, this indexing policy indexes every property of every item. The default indexing policy makes it easy to get started with Azure Cosmos DB quickly as you don’t have to think about indexing, performance, and management at the start of a project.

In this lab, you’ll observe and manipulate the default index policy for a few containers using the Data Explorer.

Create an Azure Cosmos DB for NoSQL account

Azure Cosmos DB is a cloud-based NoSQL database service that supports multiple APIs. When provisioning an Azure Cosmos DB account for the first time, you will select which of the APIs you want the account to support (for example, Mongo API or NoSQL API). Once the Azure Cosmos DB for NoSQL account is done provisioning, you can retrieve the endpoint and key and use them to connect to the Azure Cosmos DB for NoSQL account using the Azure SDK for .NET or any other SDK of your choice.

  1. In a new web browser window or tab, navigate to the Azure portal (portal.azure.com).

  2. Sign into the portal using the Microsoft credentials associated with your subscription.

  3. Select + Create a resource, search for Cosmos DB, and then create a new Azure Cosmos DB for NoSQL account resource with the following settings, leaving all remaining settings to their default values:

    Setting Value
    Subscription Your existing Azure subscription
    Resource group Select an existing or create a new resource group
    Account Name Enter a globally unique name
    Location Choose any available region
    Capacity mode Provisioned throughput
    Apply Free Tier Discount Do Not Apply

    📝 Your lab environments may have restrictions preventing you from creating a new resource group. If that is the case, use the existing pre-created resource group.

  4. Wait for the deployment task to complete before continuing with this task.

  5. Go to the newly created Azure Cosmos DB account resource and navigate to the Keys pane.

  6. This pane contains the connection details and credentials necessary to connect to the account from the SDK. Specifically:

    1. Notice the URI field. You will use this endpoint value later in this exercise.

    2. Notice the PRIMARY KEY field. You will use this key value later in this exercise.

Seed the Azure Cosmos DB for NoSQL account with data

The cosmicworks command-line tool deploys sample data to any Azure Cosmos DB for NoSQL account. The tool is open-source and available through NuGet. You will install this tool to the Azure Cloud Shell and then use it to seed your database.

  1. Start Visual Studio Code.

  2. In Visual Studio Code, open the Terminal menu and then select New Terminal to open a new terminal instance.

    📝 If you are not already familiar with the Visual Studio Code interface, review the Get Started guide for Visual Studio Code

  3. Install the cosmicworks command-line tool for global use on your machine.

     dotnet tool install cosmicworks --global --version 1.*
    

    💡 This command may take a couple of minutes to complete. This command will output the warning message (*Tool ‘cosmicworks’ is already installed’) if you have already installed the latest version of this tool in the past.

  4. Run cosmicworks to seed your Azure Cosmos DB account with the following command-line options:

    Option Value
    –endpoint The endpoint value you copied earlier in this lab
    –key The key value you coped earlier in this lab
    –datasets product
     cosmicworks --endpoint <cosmos-endpoint> --key <cosmos-key> --datasets product
    

    📝 For example, if your endpoint is: https­://dp420.documents.azure.com:443/ and your key is: fDR2ci9QgkdkvERTQ==, then the command would be: cosmicworks --endpoint https://dp420.documents.azure.com:443/ --key fDR2ci9QgkdkvERTQ== --datasets product

  5. Wait for the cosmicworks command to finish populating the account with a database, container, and items.

  6. Close the integrated terminal.

View and manipulate the default indexing policy

When a container is created by code, portal, or a tool; the indexing policy is set to an intelligent default if you do not specify it otherwise. You will observe that default indexing policy and make a change to the policy.

  1. Return to your web browser.

  2. Within the Azure Cosmos DB account resource, navigate to the Data Explorer pane.

  3. In the Data Explorer, expand the cosmicworks database node, then observe the new products container node within the NOSQL API navigation tree.

  4. Select the products container node within the NOSQL API navigation tree, and then select New SQL Query.

  5. Delete the contents of the editor area.

  6. Create a new SQL query that will return all documents where the name is equivalent to HL Headset:

     SELECT * FROM p WHERE p.name = 'HL Headset'
    
  7. Select Execute Query.

  8. Observe the results of the query.

  9. In the Query tab, select Query Stats.

  10. Observe the value of the Request Charge field within the Query Statistics section.

    📝 All paths are currently indexed, so this query should be relatively efficient.

  11. Within the products container node of the NOSQL API navigation tree, select Scale & Settings.

  12. Observe the default indexing policy within the Indexing Policy section:

     {
       "indexingMode": "consistent",
       "automatic": true,
       "includedPaths": [
         {
           "path": "/*"
         }
       ],
       "excludedPaths": [
         {
           "path": "/\"_etag\"/?"
         }
       ]
     }
    

    📝 This default policy will index all possible paths with the exception of _etag.

  13. Within the editor, replace the content of the indexing policy to only index the /price path:

     {
       "indexingMode": "consistent",
       "automatic": true,
       "includedPaths": [
         {
           "path": "/price/?"
         }
       ],
       "excludedPaths": [
         {
           "path": "/*"
         }
       ]
     }
    
  14. Select Save to persist your changes.

  15. Select New SQL Query.

  16. Delete the contents of the editor area.

  17. Create a new SQL query that will return all documents where the name is equivalent to HL Headset:

     SELECT * FROM p WHERE p.name = 'HL Headset'
    
  18. Select Execute Query.

  19. Observe the results of the query.

  20. In the Query tab, select Query Stats.

  21. Observe the value of the Request Charge field within the Query Statistics section.

    📝 Now that the name property is not indexed, the request charge has increased.

  22. Delete the contents of the editor area.

  23. Create a new SQL query that will return all documents where the price is greater than $3,000:

     SELECT * FROM p WHERE p.price > 3000
    
  24. Select Execute Query.

  25. Observe the results of the query.

  26. In the Query tab, select Query Stats.

  27. Observe the value of the Request Charge field within the Query Statistics section.