From 561f7ea58c9c329d414862cfa553614dd2c65801 Mon Sep 17 00:00:00 2001 From: Anzhuo-W <45926251+Anzhuo-W@users.noreply.github.com> Date: Sun, 10 Nov 2024 14:01:25 -0500 Subject: [PATCH] Add section filters (#234) add result filters to class sections --- services/searcher.ts | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/services/searcher.ts b/services/searcher.ts index 9b26312e..ec1531b5 100644 --- a/services/searcher.ts +++ b/services/searcher.ts @@ -10,7 +10,6 @@ import { import prisma from "../services/prisma"; import elastic, { Elastic } from "../utils/elastic"; import HydrateSerializer from "../serializers/hydrateSerializer"; -import HydrateCourseSerializer from "../serializers/hydrateCourseSerializer"; import macros from "../utils/macros"; import { EsQuery, @@ -422,6 +421,39 @@ class Searcher { }; } + filterOutSections( + results: SearchResult[], + filters: FilterInput + ): SearchResult[] { + return results + .map((result) => { + if (result.type === "employee") { + return result; + } + + result.sections = result.sections.filter((section) => { + return Object.keys(filters).every((filter) => { + const filterValue = filters[filter]; + switch (filter) { + case "honors": + return section.honors === filterValue; + case "subject": + return (filterValue as string[]).includes(section.subject); + case "campus": + return (filterValue as string[]).includes(section.campus); + case "classType": + return (filterValue as string[]).includes(section.classType); + default: + return true; + } + }); + }); + + return result.sections.length > 0 ? result : null; + }) + .filter((result) => result !== null) as SearchResult[]; + } + generateAgg(filter: string, value: string, count: number): AggCount { const agg: AggCount = { value, count }; // in addition to the subject abbreviation, add subject description for subject filter @@ -548,6 +580,7 @@ class Searcher { results = await new HydrateSerializer().bulkSerialize( searchResults.output ); + results = this.filterOutSections(results, filters); hydrateDuration = Date.now() - startHydrate; }