共用方式為


在 Microsoft Fabric 的 Cosmos DB 中自訂索引政策

在 Cosmos DB 中編製索引的設計目的是要提供快速且彈性的查詢效能,無論您的數據如何演進。 在本指南中,您會使用網狀架構入口網站或 Azure SDK 來修改容器的索引編製原則。

先決條件

  • Python 3.12 或更新版本
  • Node.js 22 或更新版本
  • .NET SDK 9.0 或更新版本

使用 Fabric 入口網站進行設定

首先,使用 Fabric 入口網站來設定容器的索引編製原則

  1. 開啟網狀架構入口網站 (https://app.fabric.microsoft.com)。

  2. 流覽至現有的 Cosmos DB 資料庫。

  3. 選取並展開現有的容器。 然後,選取 [ 設定]。

  4. 設定區段中,選取編製索引原則標籤。

    Fabric 入口網站中的資料庫容器之 [編製索引原則] 區段螢幕快照。

  5. 在編輯器中,將設定更新為新的值。 舉例來說,考慮這個包含商業資料與系統元資料的範例文件結構:

    {
      "id": "product-123",
      "_etag": "abc123def456",
      "name": "Wireless Headphones",
      "category": "Electronics",
      "price": 99.99,
      "metadata": {
        "createdBy": "system",
        "lastModified": "2025-10-30T10:30:00Z",
        "version": 1.2,
        "tags": ["internal", "generated"],
        "audit": {
          "importSource": "legacy-system",
          "reviewStatus": "pending"
        }
      }
    }
    
  6. 你可以建立索引政策,索引除通常不用於查詢的元資料欄位外的所有屬性:

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

    備註

    路徑/_etag/?使用?僅排除該屬性本身,而/metadata/*則使用*排除整個metadata物件及其所有子屬性。

    在範例文件套用此索引政策時:

    • 索引屬性id、、namecategoryprice (以及除排除外的所有其他屬性)
    • 被排除在索引之外
      • _etag 財產(單一價值)
      • 整個物件,包括 metadatacreatedBylastModifiedversiontags,以及其巢狀 audit 物件及其屬性

    此方法透過排除通常不被使用者查詢使用的系統元資料,優化儲存與效能,同時保持所有商業資料可搜尋。

使用 Azure SDK 進行設定

最後,使用 Azure SDK 來設定容器的索引編製原則。

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

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

# Create policy that indexes all paths except metadata fields
indexing_policy = {
  "indexingMode": "consistent",
  "automatic": True,
  "includedPaths": [
    {
      "path": "/*"
    }
  ],
  "excludedPaths": [
    {
      "path": "/_etag/?"
    },
    {
      "path": "/metadata/*"
    }
  ]
}

# Apply the indexing policy to the container
await 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 indexes all paths except metadata fields
containerProperties['indexingPolicy'] = {
  indexingMode: 'consistent',
  automatic: true,
  includedPaths: [
    {
      path: '/*'
    }
  ],
  excludedPaths: [
    {
      path: '/_etag/?'
    },
    {
      path: '/metadata/*'
    }
  ]
}

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

ContainerProperties properties = await container.ReadContainerAsync();

// Create policy that indexes all paths except metadata fields
IndexingPolicy indexingPolicy = new()
{
    IndexingMode = IndexingMode.Consistent,
    Automatic = true
};
indexingPolicy.IncludedPaths.Add(
    new IncludedPath { Path = "/*" }
);
indexingPolicy.ExcludedPaths.Add(
    new ExcludedPath{ Path = "/_etag/?" }
);
indexingPolicy.ExcludedPaths.Add(
    new ExcludedPath{ Path = "/metadata/*" }
);
properties.IndexingPolicy = indexingPolicy;

await container.ReplaceContainerAsync(properties);