0

I'm building filtering functionality and I have hard times making it scalable when it comes to maintaining compound indexes, so I hope to get some advice here.

The use case:

Assume collection of data with the following fields and the filter operation used for each:

  1. "date created" (">" start date, "<" end date),
  2. "first name" ("array-contains"),
  3. "company name" ("==")

User can filter data by either of those in any combination.

Like 1+2 or 2+3 or 1+2+3 etc

The problem:

Each of those queries require a compound index - 4 indexes in this case (1+2, 2+3, 1+3, 1+2+3)

But when we add 4th or even 5th property to filter by, the number of compound keys explodes exponentially. And then we need to add another collection...

Having the limit of 200 compound keys in firestore, it seems like this is not a viable option.

My thoughts

I was hoping to create a single compound index that includes all of the properties and then to provide some sort of a special value, kind of "any value" for those props the user don't want to filter by, but I can't figure out how to do that and not sure if it's even possible.

Kind of analogous to 'endAt(queryText + "\uf8ff")' in a sense that was used in RTDB when you need to filter data there (reference link).

Any suggestions on how to make filtering scalable? Or is it a bottleneck of firestore?

vir us
  • 9,920
  • 6
  • 57
  • 66
  • 1
    Does this [answer](https://stackoverflow.com/questions/74022144/firestore-accessing-more-composite-custom-indexes-with-a-simpler-query) help? – Alex Mamo Mar 11 '23 at 08:54
  • @AlexMamo thanks, auto-merging is something I wasn't aware of and it definitely helps. In my case we are using "==" and "array-contains" which after a quick test seem to work just fine. However we also have ">" "<" and it seems like the auto-merging approach doesn't apply here. Any thoughts on how to possible reducing the number of indexes for queries with inequality operations? – vir us Mar 11 '23 at 20:25
  • 1
    In the case of inequality operations, most likely you should consider keeping only the relevant filters and all of them. – Alex Mamo Mar 13 '23 at 15:44

0 Answers0