Skip to content

Commit

Permalink
switched fts to rpc with pgroonga, Closes #28
Browse files Browse the repository at this point in the history
  • Loading branch information
ImJustChew committed Nov 1, 2023
1 parent d877d92 commit 00abbf9
Show file tree
Hide file tree
Showing 4 changed files with 823 additions and 14 deletions.
29 changes: 29 additions & 0 deletions database/indexes/courses.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
create index courses_name_zh ON courses USING pgroonga(name_zh);
create index courses_teacher_zh ON courses USING pgroonga(teacher_zh);
create index courses_venues ON courses USING pgroonga(venues);
create index courses_name_en ON courses USING pgroonga(name_en);
create index courses_teacher_en ON courses USING pgroonga(teacher_en);
create index courses_department ON courses USING pgroonga(department);
create index courses_course ON courses USING pgroonga(course);
create index courses_raw_id ON courses USING pgroonga(raw_id);

CREATE OR REPLACE FUNCTION search_courses(keyword text)
RETURNS SETOF courses AS
$func$
BEGIN
IF trim(keyword) = '' THEN
-- Keyword is blank, return all courses
RETURN QUERY SELECT * FROM courses;
ELSE
-- Keyword is not blank, perform the search
RETURN QUERY SELECT * FROM courses
WHERE name_zh &@~ keyword
OR teacher_zh &@~ keyword
OR venues &@~ keyword
OR name_en &@~ keyword
OR teacher_en &@~ keyword
OR raw_id &@ keyword;
END IF;
END
$func$
LANGUAGE plpgsql;
12 changes: 4 additions & 8 deletions src/app/[lang]/courses/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -113,13 +113,9 @@ const CoursePage: NextPage = () => {
setLoading(true);
//Query for courses
try {
let temp = supabase
.from('courses')
.select('*', { count: 'exact' })
if (filters.textSearch) {
temp = temp
.textSearch('multilang_search', `'${filters.textSearch.split(' ').join("' & '")}'`)
}
let temp = supabase.rpc('search_courses', {
keyword: filters.textSearch,
}, { count: 'exact' })
if (filters.level.length)
temp = temp
.or(filters.level.map(level => `and(course.gte.${level}000,course.lte.${level}999)`).join(','))
Expand Down Expand Up @@ -171,7 +167,7 @@ const CoursePage: NextPage = () => {
if (error) console.error(error);
else {
console.log(courses)
setCourses(courses!);
setCourses(courses as CourseDefinition[]);
setHeadIndex(index);
}
}
Expand Down
9 changes: 4 additions & 5 deletions src/components/Timetable/CourseSearchbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,14 @@ const CourseSearchbar = ({ onAddCourse }: { onAddCourse: (course: CourseDefiniti
console.log(text)
try {
setLoading(true);
let temp = supabase
.from('courses')
.select('*')
if(text.length > 0) temp = temp.textSearch('multilang_search', `'${text.split(' ').join("' & '")}'`)
let temp = supabase.rpc('search_courses', {
keyword: text
});
const { data = [], error } = await temp
.order('raw_id', { ascending: true })
.limit(10);
if(error) console.error(error);
else setOptions(data ?? []);
else setOptions(data as CourseDefinition[] ?? []);
} catch (e) {
console.error(e);
}
Expand Down
Loading

0 comments on commit 00abbf9

Please sign in to comment.