Cloud Firestore automatically creates indexes to support the most common types of queries, but allows you to define custom indexes and index overrides as described in the Cloud Firestore guides.
You can create, modify and deploy custom indexes in the Firebase console, or using the CLI. From the CLI, edit your index configuration file, with default filename firestore.indexes.json
, and deploy using the firebase deploy
command.
You can export indexes with the CLI using firebase firestore:indexes
.
An index configuration file defines one object containing an indexes
array and an optional fieldOverrides
array. Here's an example:
{// Required, specify compound and vector indexesindexes:[{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 indexesfieldOverrides:[{collectionGroup:"posts",fieldPath:"myBigMapField",// We want to disable indexing on our big map field, and so empty the indexes arrayindexes:[]}]}
Deploy an index configuration
Deploy your index configuration with the firebase deploy
command. If you only want to deploy indexes for the databases configured in your project, add the --only firestore
flag. See the options reference for this command.
To list deployed indexes, run the firebase firestore:indexes
command. Add the --database=<databaseID>
flag to list indexes for a database other than your project's default database.
If you make edits to the indexes using the Firebase console, make sure you also update your local indexes file. For more on managing indexes, see the Cloud Firestore guides.
JSON format
Indexes
The schema for one object in the indexes
array is as follows. Optional properties are identified with the ?
character.
Note that Cloud Firestore document fields can only be indexed in one mode, thus a field object can only contain one of the order
, arrayConfig
, and vectorConfig
properties.
collectionGroup:string// Labeled "Collection ID" in the Firebase consolequeryScope:string// One of "COLLECTION", "COLLECTION_GROUP"fields:arrayfieldPath:stringorder?:string// One of "ASCENDING", "DESCENDING"; excludes arrayConfig and vectorConfig propertiesarrayConfig?:string// If this parameter used, must be "CONTAINS"; excludes order and vectorConfig propertiesvectorConfig?:object// Indicates that this is a vector index; excludes order and arrayConfig propertiesdimension:number// The resulting index will only include vectors of this dimensionflat:{}// Indicates the vector index is a flat index
FieldOverrides
The schema for one object in the fieldOverrides
array is as follows. Optional properties are identified with the ?
character.
Note that Cloud Firestore document fields can only be indexed in one mode, thus a field object cannot contain both the order
and arrayConfig
properties.
collectionGroup:string// Labeled "Collection ID" in the Firebase consolefieldPath:stringttl?:boolean// Set specified field to have TTL policy and be eligible for deletionindexes:array// Use an empty array to disable indexes on this collectionGroup + fieldPathqueryScope:string// One of "COLLECTION", "COLLECTION_GROUP"order?:string// One of "ASCENDING", "DESCENDING"; excludes arrayConfig propertyarrayConfig?:string// If this parameter used, must be "CONTAINS"; excludes order property
TTL Policy
A TTL policy can be enabled or disabled using the fieldOverrides
array as follows:
// Optional, disable index single-field collection group indexesfieldOverrides:[{collectionGroup:"posts",fieldPath:"ttlField",ttl:"true",// Explicitly enable TTL on this Field.// Disable indexing so empty the indexes arrayindexes:[]}]
To keep the default indexing in the field and enable a TTL policy:
{"fieldOverrides":[{"collectionGroup":"yourCollectionGroup","fieldPath":"yourFieldPath","ttl":true,"indexes":[{"order":"ASCENDING","queryScope":"COLLECTION_GROUP"},{"order":"DESCENDING","queryScope":"COLLECTION_GROUP"},{"arrayConfig":"CONTAINS","queryScope":"COLLECTION_GROUP"}]}]}
For more information about time-to-live (TTL) policies review the official documentation.