firebasegoogle-cloud-platformgoogle-cloud-firestore

Simplify index creation in Firestore


I am trying to simplify the index creation in Cloud Firestore. It keeps asking me to create new indexes for anything I do in my app. For e.g., I have the following table, which shows a list of incidents. Users can filter based on 5 different fields and can apply a combination of those fields while searching. I have created like 20 indexes to support this table. It is very easy to miss some of the combinations in the index, and then that query doesn't work.

I am trying to understand, isn't there a way to like, mention I want to search on these five fields in any combination?

Whenever I add another field to the filtering, it adds up a lot of work and testing, and I doubt whether everything will work as expected once I push to production.

enter image description here


Solution

  • I am trying to understand isn't there a way to like mention I want to search on these five fields in any combination?

    No, there is not. You cannot specify some field names and create the corresponding indexes or a combination of indexes automatically. You either perform all those queries, extract from the error message the URL that creates the index automatically for you, or you can use the Firebase CLI to simplify the process.

    However, even though it is mandatory to create an index for every query, it is not always necessary to have one index per query. I'm saying that because you can use index merging:

    For queries with multiple equality (==) clauses and, optionally, an orderBy clause, Cloud Firestore can re-use existing indexes. Cloud Firestore can merge the indexes for simple equality filters to build the composite indexes needed for larger equality queries.