Edit

Share via


Customize indexing policies in Cosmos DB in Microsoft Fabric (preview)

Important

This feature is in preview.

Indexing in Cosmos DB is designed to deliver fast and flexible query performance, no matter how your data evolves. In this guide, you modify the indexing policy for a container using the Fabric portal or an Azure SDK.

Prerequisites

  • Python 3.12 or later
  • Node.js 22 or later
  • .NET SDK 9.0 or later

Set using the Fabric portal

First, use the Fabric portal to set the indexing policy for a container

  1. Open the Fabric portal (https://app.fabric.microsoft.com).

  2. Navigate to your existing Cosmos DB database.

  3. Select and expand your existing container. Then, select Settings.

  4. In the Settings section, select the Indexing Policy tab.

    Screenshot of the 'Indexing Policy' section for a container within a database in the Fabric portal.

  5. In the editor, update the setting to a new value. For example, you can set the indexing policy to only index the name and category properties for items in the container using this policy.

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

Set using the Azure SDK

Finally, use the Azure SDK to set the indexing policy for a container.

database = client.get_database_client("<database-name>")

container = database.get_container_client("<container-name>")

# Create policy that only indexes specific paths
indexing_policy = {
  "indexingMode": "consistent",
  "automatic": True,
  "includedPaths": [
    {
      "path": "/name/?"
    },
    {
      "path": "/category/?"
    }
  ],
  "excludedPaths": [
    {
      "path": "/*"
    }
  ]
}

# Create policy that only indexes specific paths
database.replace_container(container, partition_key=PartitionKey(path='/<partition-key-path>'), indexing_policy=indexing_policy)
const container: Container = client.database('<database-name>').container('<container-name>');

const { resource: containerProperties } = await container.read();

// Create policy that only indexes specific paths
containerProperties['indexingPolicy'] = {
  indexingMode: 'consistent',
  automatic: true,
  includedPaths: [
    {
      path: '/name/?'
    },
    {
      path: '/category/?'
    }
  ],
  excludedPaths: [
    {
      path: '/*'
    }
  ]
}

await container.replace(containerProperties);
Container container = client
    .GetDatabase("<database-name>")
    .GetContainer("<container-name>");

ContainerProperties properties = await container.ReadContainerAsync();

// Create policy that only indexes specific paths
IndexingPolicy indexingPolicy = new()
{
    IndexingMode = IndexingMode.Consistent,
    Automatic = true
};
indexingPolicy.ExcludedPaths.Add(
    new ExcludedPath{ Path = "/*" }
);
indexingPolicy.IncludedPaths.Add(
    new IncludedPath { Path = "/name/?"  }
);
indexingPolicy.IncludedPaths.Add(
    new IncludedPath { Path = "/category/?" }
);
properties.IndexingPolicy = indexingPolicy;

await container.ReplaceContainerAsync(properties);