Cloud Firestore 索引定义参考

Cloud Firestore 会自动创建索引来支持最常见的查询类型,但允许您按照 Cloud Firestore 指南中的说明定义自定义索引和索引覆盖。

您可以通过 Firebase 控制台或使用 CLI 创建、修改和部署自定义索引。在 CLI 中,修改索引配置文件(默认文件名为 firestore.indexes.json),然后使用 firebase deploy 命令进行部署。

您可以通过 CLI 使用 firebase firestore:indexes 导出索引。

索引配置文件定义了一个对象,该对象包含一个 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

TTL 政策

您可以使用 fieldOverrides 数组启用或停用 TTL 政策,如下所示:

  // 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: []
    }
  ]

如需保留字段中的默认索引并启用 TTL 政策,请执行以下操作:

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

如需详细了解存留时间 (TTL) 政策,请参阅官方文档