Cloud Firestore は、最も一般的なタイプのクエリをサポートするインデックスを自動的に作成しますが、 Cloud Firestore ガイドの説明に従ってカスタム インデックスとインデックス オーバーライドを定義することもできます。
カスタム インデックスは、Firebase コンソールまたは CLI を使用して作成、変更、デプロイできます。 CLI から、デフォルトのファイル名firestore.indexes.json
を使用してインデックス構成ファイルを編集し、 firebase deploy
コマンドを使用してデプロイします。
CLI でfirebase firestore:indexes
を使用してインデックスをエクスポートできます。
インデックス構成ファイルは、 indexes
配列とオプションのfieldOverrides
配列を含む 1 つのオブジェクトを定義します。以下に例を示します。
{
// Required, specify compound indexes
indexes: [
{
collectionGroup: "posts",
queryScope: "COLLECTION",
fields: [
{ fieldPath: "author", arrayConfig: "CONTAINS" },
{ fieldPath: "timestamp", order: "DESCENDING" }
]
}
],
// 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 つのモードでのみインデックスを作成できるため、フィールド オブジェクトにorder
とarrayConfig
プロパティの両方を含めることはできないことに注意してください。
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 property
arrayConfig?: string // If this parameter used, must be "CONTAINS"; excludes order property
フィールドオーバーライド
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" }
]
}
]
}
Time-to-Live (TTL) ポリシーの詳細については、公式ドキュメントを参照してください。