Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add section filters #234

Merged
merged 1 commit into from
Nov 10, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 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 @@ -39,11 +38,11 @@
CourseSearchResult,
ParsedQuery,
} from "../types/searchTypes";
import { SerializedCourse } from "../types/serializerTypes";

Check warning on line 41 in services/searcher.ts

View workflow job for this annotation

GitHub Actions / Lint & Type checks

'SerializedCourse' is defined but never used. Allowed unused vars must match /^_/u
import { Course, Section } from "../types/types";

Check warning on line 42 in services/searcher.ts

View workflow job for this annotation

GitHub Actions / Lint & Type checks

'Course' is defined but never used. Allowed unused vars must match /^_/u

Check warning on line 42 in services/searcher.ts

View workflow job for this annotation

GitHub Actions / Lint & Type checks

'Section' is defined but never used. Allowed unused vars must match /^_/u

type CourseWithSections = PrismaCourse & { sections: PrismaSection[] };
type SSRSerializerOutput = { [id: string]: CourseSearchResult };

Check warning on line 45 in services/searcher.ts

View workflow job for this annotation

GitHub Actions / Lint & Type checks

'SSRSerializerOutput' is defined but never used. Allowed unused vars must match /^_/u

class Searcher {
elastic: Elastic;
Expand Down Expand Up @@ -422,6 +421,39 @@
};
}

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 @@
results = await new HydrateSerializer().bulkSerialize(
searchResults.output
);
results = this.filterOutSections(results, filters);
hydrateDuration = Date.now() - startHydrate;
}

Expand Down
Loading