0

I am creating a Firestore project that contains group chats/forums where users can filter the posts based on these fields:

  • Number of likes
  • If the post contains attachments (photos and/or files)
  • 4 Tags (Questions, Exams, Assignments, Notes)

Users can apply any or all of these filters at the same time.

Each Post document has the following fields:

  1. Number of Clips
  2. A different boolean for each of the tags
  3. A list of the files in the post
  4. The text of the post

My method for creating these queries looks like this:

  setQueryFilters() {
      var queryPosts = db.collection('posts').where('course_id', '==', this.course);

      if (this.filterByNotes) {
        queryPosts = queryPosts.where('notesTag', '==', true);
      }

      if (this.filterByExams) {
        queryPosts = queryPosts.where('examsTag', '==', true);
      }

      if (this.filterByAssignments) {
        queryPosts = queryPosts.where('assignmentsTag', '==', true);
      }

      if (this.filterByQuestions) {
        queryPosts = queryPosts.where('questionsTag', '==', true);
      }

      if (this.filterByFiles) {
        queryPosts = queryPosts.where('files', '!=', []);
      }

      if (this.filterByClips) {
        queryPosts = queryPosts.orderBy('clips', 'desc');
      } else {
        queryPosts = queryPosts.orderBy('created_at', 'desc');
      }

      return queryPosts;
    },

Since there are 6 different filters that can be applied, I have hundreds of potential different queries, and all of them (except a few) require me to create compound indexes in firebase. I'm not even sure if firebase will allow me to create this many indexes. Is there a better way to do this?

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
Sdanson
  • 125
  • 2
  • 5

1 Answers1

1

The Firebase CLI lets you deploy security rules and indexes based on a JSON file in your project workspace. You can write code to generate the JSON that describes the indexes you want to create, then deploy them with one command from the CLI. You should not have any problems creating all the required indexes.

See also:

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441