Cloud Firestore インデックス定義リファレンス

Cloud Firestore では、最も一般的なタイプのクエリをサポートするインデックスが自動的に作成されますが、Cloud Firestore ガイドで説明されているように、カスタム インデックスとインデックスのオーバーライドを定義することもできます。

カスタム インデックスは、Firebase コンソールまたは CLI を使用して作成、変更、デプロイできます。CLI から、デフォルトのファイル名 firestore.indexes.json でインデックス構成ファイルを編集し、firebase deploy コマンドを使用してデプロイします。

CLI で firebase firestore:indexes を使用してインデックスをエクスポートするには、

インデックス構成ファイルでは、indexes 配列とオプションの fieldOverrides 配列を含む 1 つのオブジェクトを定義します。次の例をご覧ください。

{
  // 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 配列の 1 つのオブジェクトのスキーマは次のとおりです。オプション プロパティは ? 文字で識別されます。

Cloud Firestore ドキュメントのフィールドには 1 つのモードでのみインデックスを付けることができます。したがって、フィールド オブジェクトに含めることができるプロパティは、orderarrayConfigvectorConfig のいずれか 1 つだけです。

  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 配列の 1 つのオブジェクトのスキーマは次のとおりです。オプション プロパティは ? 文字で識別されます。

Cloud Firestore ドキュメントのフィールドには 1 つのモードでのみインデックスを付けることができます。したがって、フィールド オブジェクトに order プロパティと arrayConfig プロパティの両方を含めることはできません。

  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 ポリシー

TTL ポリシーは、次のように 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: []
    }
  ]

フィールド内のインデックスをデフォルトのままにして、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)ポリシーの詳細については、公式ドキュメントをご覧ください。