Skip to content

Commit

Permalink
Add section filters (#234)
Browse files Browse the repository at this point in the history
add result filters to class sections
  • Loading branch information
Anzhuo-W authored Nov 10, 2024
1 parent 6cd914e commit 561f7ea
Showing 1 changed file with 34 additions and 1 deletion.
35 changes: 34 additions & 1 deletion services/searcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -548,6 +580,7 @@ class Searcher {
results = await new HydrateSerializer().bulkSerialize(
searchResults.output
);
results = this.filterOutSections(results, filters);
hydrateDuration = Date.now() - startHydrate;
}

Expand Down

0 comments on commit 561f7ea

Please sign in to comment.