Basically, I have a query from Firestore, and some of the filter fields may be empty, in that case the specific query will be ignored,Since inequality queries with multiple fields requires indexes, what's the best way to create indexes of such query without going through all possible combinations.
e.g, 'country' and 'budget_high', 'gender' and 'budget_high', 'age' and 'budget_high', 'country, gender' and 'budget_high' and so on.
This is my query.
if (country !== "") query = query.where("country", "==", country);
if (gender !== "") query = query.where("gender", "==", gender);
if (age !== "") query = query.where("age", "==", age);
if (religion !== "") query = query.where("religion", "==", religion);
if (budget_high !== "") query = query.where("budget_high", "<=", Number(budget_high));
return query
.get()
.then((querySnapshot) => {
var data = [];
querySnapshot.forEach((doc) => {
let user_data = doc.data();
user_data.created_at = `${user_data.created_at.toDate()}`;
data.push(user_data);
});
return data;
})
.catch((error) => {
console.log("Error getting documents: ", error);
return [];
});
};
Here are my Single Field Exemptions for collection (not collection group)

First, we need a field that will be present in all queries. You can orderBy one of the fields inorder for it to always be present (budget_high in this case since you are calling inequality query on it).
So create 4 composite indexes.
This will effectively handle your query combinations.
Read the merging index docs
Following up on @Peter O. response, the order of the fields in the indexes is important. Please try the following:
I hope this helps.