Is there a working example for Firestore Index merge?

Viewed 369

I am trying to reduce my firestore indices by using the index merge feature as mentioned in the firebase docs: https://firebase.google.com/docs/firestore/query-data/index-overview#taking_advantage_of_index_merging

However the queries always require the full composite index.

i.e.

querying for

  • department in [1] AND priority == 1
  • order by name

requires a composite index on

  • department asc, priority asc, name asc.

As per doc it should be enough to use either an index

  • department asc, name asc. or
  • priority asc, name asc.

since the index should be merged with the automatic single field index for department or priority.

I was looking around for a working example in order to figure out what I did wrong but could not find anything.

Any ideas?

2 Answers

Solved.

The problem was that index merge uses only other composite indices for merging. It will not merge the automatic single indices.

For a more detailed answer see How to get Firestore Index Merge to work?

According to my understanding, the concept of merged indexes ought to solve the limited number of indexes per database. From that perspective I don't see, at least from the issue description, any advantage of using this feature in your use case.

Mentioned documentation shows how to create less indexes than queries, while in presented use case the requirement is to create one shorter index for one query.

In the example presented in the documentation we have 4 queries and 4 composite indexes, and using merge indexes we have 3 indexes and 5 queries (one additional than example use case). When you are reaching a quota of 200 composite indexes merged indexes might be big difference giving additional number query possibilities.

Other words if there is complicated query, it can use already created composite indexes for simpler queries and Firestore merge them.

The query from example needs both indexes mentioned: department+name and priority+name. however you will get 2 additional query possibilities, without additional indexes:

  • where('department','==',<something>).orderby('name')
  • where('priority','==',<something>).orderby('name')

If you have 7 fields to filter and one sort, just create 7 simplest composite indexes for the sort ( field+sort ) and than you will be able to use set of more than hundred queries (probably 127, if I understand correctly) just from just 7 composite indexes. I think this is a concept of merge indexes.

Related