3

I have a Firebase array called products that contains items like this one:

JhMIecX5K47gt0VaoQN: {
    brand: "Esprit"
    category: "Clothes",
    gender: "Boys",
    name: "Pants",
}

Is it possible to query products from this array using multiple filters with the Firebase API. For example I might want to filter by brand and category (all "Pants" by "Esprit"). So far I've tried ordering by child key and then limiting the start and end of this ordering, but I can't figure out how to apply more filters.

I'm using the iOS SDK.

maxjvh
  • 214
  • 1
  • 6
  • 18
  • Firebase can only filter on one property (or value) at a time. If you call multiple `orderBy...` methods in a single query it is supposed to raise an error ti indicate that this is not allowed. Doesn't it do that for you? – Frank van Puffelen Feb 19 '15 at 13:31
  • 1
    See also [more than one dimension](http://stackoverflow.com/questions/25432574/framing-firebase-data-in-more-than-one-dimension), [more than one where condition](http://stackoverflow.com/questions/27432030/how-to-do-the-following-query-in-firebase-more-than-one-where-condition), and [or queries](http://stackoverflow.com/questions/28034092/or-queries-in-firebase/28036032#28036032), – Kato Feb 20 '15 at 15:29
  • @FrankvanPuffelen Yes, I do get an error, but I was wondering if I was doing it the wrong way. Shame it's not supported. – maxjvh Feb 21 '15 at 14:01
  • @FrankvanPuffelen If you format your comment as an answer, I would accept it as the correct answer. – maxjvh Feb 24 '15 at 11:59
  • Done. But I also voted to close this question as a duplicate, which should get picked up at some point. – Frank van Puffelen Feb 24 '15 at 14:40

2 Answers2

5

Firebase can only order/filter on one property (or value) at a time. If you call a orderBy... method multiple times in a single query it will raise an error to indicate this is not allowed.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
2

Actually, in Firebase, when you order by a specific key, there is another index involved : push IDs (if you use them) They are almost perfectly ordered chronologically, so you order by field XXX plus Time.

More details here

In your case, this is not about time, so the only solution is to use additional composite indexes :

JhMIecX5K47gt0VaoQN: {
    brand: "Esprit"
    category: "Clothes",
    brandcategoryIndex: "EspritClothes",
    categorybrandIndex: "ClothesEsprit",
    brandnameIndex: "EspritPants",
    namebrandIndex: "PantsEsprit",
    gender: "Boys",
    name: "Pants",
}

In the above example, you can query by :

  • Category > Brand (and the other way)
  • Name > Brand (and the other way)

Just make sure you add those fields to your Firebase index in the rules section, and that you maintain them anytime the items are modified.

(this process of adding a cost of redundant data at the benefit of performance, which is forced by Firebase in this case, is called denormalization)

Pandaiolo
  • 11,165
  • 5
  • 38
  • 70
  • So if you covered all scenarios/indexes would that query all of them? ie. lets say a user can filter out all of these items by selection, could I display the matched results of the brand, category, gender and name as long as I have every scenario covered? Is this really bad practice? I'm currently moving my parse database to Firebase and in Parse I used the query where on 5 different categories. – Grace Mar 30 '17 at 22:56