Reputation: 846
My firestore model is like
{ foods : [ 'butter, cheese' : { "Energy - kcal" : 122, "Carbohydrate - g" : 33, "Fat - g" : 23 }, . . ] }
let query = db.collection("foods");
let list = ["Energy - kcal","Carbohydrate - g"];
for(let i=0;i<list.length;i+=1) {
let each = list[i];
switch (each) {
case "Energy - kcal":
query = query.orderBy("Energy - kcal","desc");
break;
case "Carbohydrate - g":
query = query.orderBy("Carbohydrate - g","desc");
break;
case "Cholesterol - g":
query = query.orderBy("Cholesterol - g","desc");
break;
case "Fat - g":
query = query.orderBy("Fat - g","desc");
break;
case "Protein - g":
query = query.orderBy("Protein - g","desc");
break;
case "Fatty acids - g":
query = query.orderBy("Fatty acids - g","desc");
break;
case "Sodium - mmg":
query = query.orderBy("Sodium - mmg","desc");
break;
case "Potassium - mmg":
query = query.orderBy("Potassium - mmg","desc");
break;
case "sugar":
query = query.orderBy("sugar","desc");
break;
case "fiber":
query = query.orderBy("fiber","desc");
break;
case "calcium":
query = query.orderBy("calcium","desc");
break;
case "iron":
query = query.orderBy("iron","desc");
break;
case "vitamin_a":
query = query.orderBy("vitamin_a","desc");
break;
case "vitamin_c":
query = query.orderBy("vitamin_c","desc");
break;
}
}
I have tried indexing all attributes with descending order but still getting
Error: 9 FAILED_PRECONDITION: The query requires an index
What should proper way to identify which fields are used in indexing and how do I take advantage of Cloud Firestore's ability to merge indexes?
Thanks,
Upvotes: 0
Views: 338
Reputation: 138824
You can take advantage of index merging only when you are using multiple (chained) where()
function calls in your query. Since your query will always have a single where()
call based on a single value from within that list
along with an orderBy()
call, you cannot merge anything. So you need to create an index for each property separately.
Edit:
According to your comment, if you need to query your data according to 14 properties separately and order them it the same time, this is the solution. It's not a good one but this what you have. In your case, I recommend deploy indexes with the Firebase CLI.
Upvotes: 1