Skip to content
Advertisement

firestore query optional filters: Do I have to create an index for every possibility?

I undestand that I need an index for longer search querys in firestore. I am querying my docs like this:

  workoutFiter.tags.forEach(
    tag => (query = query.where(`tags.${tag.index}`, '==', true)),
  );

There are curretnly 5 tags in total, so do I have to create an index for every possible combination of tags? Because if so, I think I would have to have 120 (5 * 4 * 3 * 2) indexes. That seems a bit too overcomplicated.

Advertisement

Answer

As mentioned in the documentation, you can create only 200 composite indexes so this way might not be the best in case you want to add more tags and Firestore does not support a array-contains-all operator at the moment.

You can store an array of tags in the document and run a separate query for each tag using array-contains operator as shown below:

const q = query(colRef, where('tags', 'array-contains', 'tagName'));

// or "array-contains-any" to combine up to 10 array-contains clauses

If you need advanced filters, then checkout Algolia that supports the query that you are trying to perform and Firebase Algolia Extension to keep it updated.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement