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:
- Number of Clips
- A different boolean for each of the tags
- A list of the files in the post
- 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?