Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I create indexes for different combinations of fields in Firestore?

Suppose I have a users collection that I want to filter in a page of mine. The fields to be filtered on are name, age, location.

For this reason, I create a composite index (name, age, location). The issue is that I want to have the opportunity to filter by any combination of the 3 - name and age, name and location, age and location. When I try to do this, I am prompted to create another composite index for the used combination of fields.

My question is - does that mean that I have to add indexes for all possible composite combinations? If yes, how can I make this happen, if I want to potentially filter by 10 fields, and not only 3? Do I have to create indexes for all combinations myself?

like image 566
Yulian Avatar asked Oct 25 '18 15:10

Yulian


People also ask

What is composite index in Firebase?

A composite index stores a sorted mapping of all the documents in a collection, based on an ordered list of fields to index. Note: You can have at most one array field per composite index. Cloud Firestore uses composite indexes to support queries not already supported by single-field indexes.

Is there any way to update a specific index from the array in firestore?

Is there any way to update a specific index from the array in Firestore? No, there is not! This is not possible because if you want to perform an update, you need to know the index of that particular element. When talking about Cloud Firestore arrays, the things are different that you might think.

What are the composite indexes?

They are indexes that combine the behavior of key cyclical indicators that represent widely differing activities of the economy such as production, employment, and many more. The composite indexes are used to identify the volume of overall business activities by composing percentage changes of selected indicators.


1 Answers

If you are using multiple equality (==) clauses for queries, then you can take advantage of index merging. Cloud Firestore can re-use existing indexes. In your case you have 3 columns name, age, and location and you want to order by hearts, then create three indexes like this:

Collection   Fields indexed
users        ↑ name,      ↑ hearts
users        ↑ age,       ↑ hearts
users        ↑ location,  ↑ hearts

Now you can create different queries with combination of name, age and location using multiple equality (==) clauses.

You can refer to the Firestore documentation about index merging here

like image 177
Niyas Avatar answered Oct 12 '22 20:10

Niyas