Cloud Firestore 索引定義參考資料

Cloud Firestore 會自動建立索引來支援最常見的查詢類型,但可讓您定義自訂索引及覆寫索引,詳情請參閱 Cloud Firestore 指南

您可以透過 Firebase 控制台或 CLI 建立、修改及部署自訂索引。在 CLI 中編輯索引設定檔,使用預設檔案名稱 firestore.indexes.json,並使用 firebase deploy 指令部署。

您可以使用 firebase firestore:indexes 透過 CLI 匯出索引。

索引設定檔會定義一個包含 indexes 陣列和選用 fieldOverrides 陣列的物件。範例如下:

{
  // Required, specify compound and vector indexes
  indexes: [
    {
      collectionGroup: "posts",
      queryScope: "COLLECTION",
      fields: [
        { fieldPath: "author", arrayConfig: "CONTAINS" },
        { fieldPath: "timestamp", order: "DESCENDING" }
      ]
    },
    {
      collectionGroup: "coffee-beans",
      queryScope: "COLLECTION",
      fields: [
        {
          fieldPath: "embedding_field",
          vectorConfig: { dimension: 256, flat: {} }
        }
      ]
    }
  ],

  // Optional, disable indexes or enable single-field collection group indexes
  fieldOverrides: [
    {
      collectionGroup: "posts",
      fieldPath: "myBigMapField",
      // We want to disable indexing on our big map field, and so empty the indexes array
      indexes: []
    }
  ]
}

部署索引設定

使用 firebase deploy 指令部署索引設定。如果只想為專案中設定的資料庫部署索引,請新增 --only firestore 旗標。請參閱這個指令的選項參考資料

如要列出已部署的索引,請執行 firebase firestore:indexes 指令。新增 --database=<databaseID> 旗標以列出專案預設資料庫以外的資料庫索引。

如果是使用 Firebase 主控台編輯索引,請務必一併更新本機索引檔案。如要進一步瞭解如何管理索引,請參閱 Cloud Firestore 指南

JSON 格式

索引

indexes 陣列中單一物件的結構定義如下。選用屬性是以 ? 字元識別。

請注意,Cloud Firestore 文件欄位只能在一種模式中編入索引,因此欄位物件只能包含 orderarrayConfigvectorConfig 屬性的其中一個。

  collectionGroup: string  // Labeled "Collection ID" in the Firebase console
  queryScope: string       // One of "COLLECTION", "COLLECTION_GROUP"
  fields: array
    fieldPath: string
    order?: string         // One of "ASCENDING", "DESCENDING"; excludes arrayConfig and vectorConfig properties
    arrayConfig?: string   // If this parameter used, must be "CONTAINS"; excludes order and vectorConfig properties
    vectorConfig?: object  // Indicates that this is a vector index; excludes order and arrayConfig properties
      dimension: number    // The resulting index will only include vectors of this dimension
      flat: {}             // Indicates the vector index is a flat index

FieldOverrides

fieldOverrides 陣列中單一物件的結構定義如下。選用屬性是以 ? 字元識別。

請注意,Cloud Firestore 文件欄位只能在單一模式下建立索引,因此欄位物件不能同時包含 orderarrayConfig 屬性。

  collectionGroup: string  // Labeled "Collection ID" in the Firebase console
  fieldPath: string
  ttl?: boolean            // Set specified field to have TTL policy and be eligible for deletion
  indexes: array           // Use an empty array to disable indexes on this collectionGroup + fieldPath
    queryScope: string     // One of "COLLECTION", "COLLECTION_GROUP"
    order?: string         // One of "ASCENDING", "DESCENDING"; excludes arrayConfig property
    arrayConfig?: string   // If this parameter used, must be "CONTAINS"; excludes order property

存留時間政策

您可以使用 fieldOverrides 陣列啟用或停用存留時間政策,如下所示:

  // Optional, disable index single-field collection group indexes
  fieldOverrides: [
    {
      collectionGroup: "posts",
      fieldPath: "ttlField",
      ttl: "true",  // Explicitly enable TTL on this Field.
      // Disable indexing so empty the indexes array
      indexes: []
    }
  ]

如何保留欄位中的預設索引並啟用存留時間政策:

{
  "fieldOverrides": [
    {
      "collectionGroup": "yourCollectionGroup",
      "fieldPath": "yourFieldPath",
      "ttl": true,
      "indexes": [
        { "order": "ASCENDING", "queryScope": "COLLECTION_GROUP" },
        { "order": "DESCENDING", "queryScope": "COLLECTION_GROUP" },
        { "arrayConfig": "CONTAINS", "queryScope": "COLLECTION_GROUP" }
      ]
    }
  ]
}

如要進一步瞭解存留時間 (TTL) 政策,請參閱官方說明文件