1

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

0

1 Answer 1

2

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.

6
  • 1
    Given that some of the search fields are dropdowns (which I expect to map to equality filters), index merging is probably a good way to reduce the number of indexes needed. Note: while this reduces the number of indexes needed, it will also affect performance - as accessing multiple indexes is going to be slower than accessing a single dedicated one. So measure/track performance and decide whether you want to create a dedicated index after all. Commented Mar 27 at 14:00
  • @AlexMamo Thanks for your answer. I earlier didn't know about index merging. I wanted to get some clarification on the answer. So, I use a collection group while querying incidents. The structure of the incidents on the Cloud Firestore documents is enterpriseAccounts > listeners > incidents > <incident document>. Since Cloud Firestore doesn't provide first-class multi-tenant support, I store enterpriseAccountId on each incident and do query like collectionGroup(incidents, where('enterpriseAccountId', '==', enterpriseAccountId), ...). Comment continued. Commented Mar 28 at 12:38
  • I didn't fully understand if I need to add something apart from the appropriate indexes for index merging. So, I plan to add the following indexes enterpriseAccountId asc, identifier asc, timestamp desc, enterpriseAccountId asc, listenerName asc, timestamp desc, enterpriseAccountId asc, reportedAtDate asc, timestamp desc, enterpriseAccountId asc, priority asc, timestamp desc, enterpriseAccountId asc, status asc, timestamp desc. Comment continued. Commented Mar 28 at 12:44
  • After I add the above indexes, would I be able to do queries like collectionGroup(incidents, where('enterpriseAccountId', '==', enterpriseAccountId), where('identifier', '==', identifier), where('priority', '==', priority), orderBy('timestamp', 'desc') even thought I don't have an index enterpriseAccountId asc, identifier asc, priority asc timestamp desc I am in the process of removing the existing indexes and add the new ones as described but thought I will confirm with you also in case my understanding is not correct. Commented Mar 28 at 12:45
  • 1
    @AlexMamo I am able to verify that my above understanding is correct. I created just 5 indexes for the five equality filters and I am able to use a combination of two more filters without adding extra indexes. Please clarify if I am missing something in my understanding. Commented Mar 28 at 14:30

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.