Firestore: How to exempt a field from being indexed

02 Jan 2019

Both Cloud Datastore and Firestore automatically index most of the fields in your database models.


For example, if you use Cloud Datastore with Python 2 on the Standard GAE Environment, you'd create your database model like this:

class User(ndb.Model):
    name = ndb.StringProperty()
    age = ndb.IntegerProperty()
    active = ndb.BooleanProperty()

By default, all three fields would be indexed. But if you don't want some (or any) of them to be indexed, you can set their indexed attribute as False:

class User(ndb.Model):
    name = ndb.StringProperty(indexed=False)
    age = ndb.IntegerProperty(indexed=False)
    active = ndb.BooleanProperty(indexed=False)

You can read more about optimizing indexes in this article: Optimizing Indices In Google Cloud Datastore - How And Why?


But what if you use the new GAE Python Standard Environment, which uses Python 3 and Cloud Firestore?

You'd have to disable field indexing via the Firebase Console.

Open your Firestore database in the Firebase Console. Click on Indexes and choose the Single field tab.

This page will show up:

Then scroll down to the Exemptions section:

Click on Add exemption and add the model name and the field you'd like to have exempted from indexing:

Click Next and disable the indexes you'd like to have exempt:

Voila! You've successfully exempted a field from having an index created which will save your database storage costs.

Please share this article via your social media channels:

Comments (0)

Your comment
Enter your real email address, because you'll have to verify your comment (anti-spam).


Don't miss any new post! Sign up for our newsletter (we promise not to spam)!