diff --git a/backend/handlers.go b/backend/handlers.go index 90ff6e8f..d3e20770 100644 --- a/backend/handlers.go +++ b/backend/handlers.go @@ -45,7 +45,7 @@ func HandleHealthCheck(w http.ResponseWriter, r *http.Request) { func HandleQPYear(w http.ResponseWriter, r *http.Request) { db := db.GetDB() - result := db.QueryRow(context.Background(), "SELECT MIN(year), MAX(year) FROM iqps_test") + result := db.QueryRow(context.Background(), "SELECT MIN(year), MAX(year) FROM iqps") var minYear, maxYear int err := result.Scan(&minYear, &maxYear) if err != nil { @@ -59,7 +59,7 @@ func HandleQPYear(w http.ResponseWriter, r *http.Request) { func HandleLibraryPapers(w http.ResponseWriter, r *http.Request) { db := db.GetDB() - rows, err := db.Query(context.Background(), "SELECT * FROM iqps_test WHERE from_library = 'true'") + rows, err := db.Query(context.Background(), "SELECT * FROM iqps WHERE from_library = 'true'") if err != nil { sendErrorResponse(w, http.StatusInternalServerError, "Could not Query Question Paper, Try Later!", nil) return @@ -93,16 +93,14 @@ func HandleQPSearch(w http.ResponseWriter, r *http.Request) { // var params []interface{} // params = append(params, course) params := pgx.NamedArgs{ - "query_text": course, - "match_count": 50, + "query_text": course, } exam := r.URL.Query().Get("exam") if exam != "" { query = fmt.Sprintf(`%s WHERE (exam = @exam OR exam = '')`, query) params = pgx.NamedArgs{ - "query_text": course, - "match_count": 50, - "exam": exam, + "query_text": course, + "exam": exam, } } @@ -135,7 +133,7 @@ func HandleQPSearch(w http.ResponseWriter, r *http.Request) { func ListUnapprovedPapers(w http.ResponseWriter, r *http.Request) { db := db.GetDB() - rows, err := db.Query(context.Background(), "SELECT course_code, course_name, year, exam,filelink,id, from_library FROM iqps_test WHERE approve_status = false ORDER BY upload_timestamp ASC") + rows, err := db.Query(context.Background(), "SELECT course_code, course_name, year, exam,filelink,id, from_library FROM iqps WHERE approve_status = false ORDER BY upload_timestamp ASC") if err != nil { sendErrorResponse(w, http.StatusInternalServerError, err.Error(), nil) return @@ -205,8 +203,8 @@ func HandleFileUpload(w http.ResponseWriter, r *http.Request) { defer file.Close() fileType := fileHeader.Header.Get("Content-Type") - config.Get().Logger.Error("HandleFileUpload:", slog.String("Invalid file type", fileType)) if fileType != "application/pdf" { + config.Get().Logger.Error("HandleFileUpload:", slog.String("Invalid file type", fileType)) resp.Status = "failed" resp.Description = "invalid file type. Only PDFs are supported" response = append(response, resp) @@ -298,7 +296,7 @@ func populateDB(filename string) error { exam := qpData[3] fromLibrary := false fileLink := filepath.Join(config.Get().UploadedQPsPath, filename+".pdf") - query := "INSERT INTO qp (course_code, course_name, year, exam, filelink, from_library) VALUES ($1, $2, $3, $4, $5, $6);" + query := "INSERT INTO iqps (course_code, course_name, year, exam, filelink, from_library) VALUES ($1, $2, $3, $4, $5, $6);" _, err := db.Exec(context.Background(), query, courseCode, courseName, year, exam, fileLink, fromLibrary) if err != nil { diff --git a/backend/pkg/db/db.go b/backend/pkg/db/db.go index def24f89..682bef87 100644 --- a/backend/pkg/db/db.go +++ b/backend/pkg/db/db.go @@ -15,7 +15,7 @@ var ( mu sync.Mutex ) -const init_db = `CREATE TABLE IF NOT EXISTS iqps_test ( +const init_db = `CREATE TABLE IF NOT EXISTS iqps ( id SERIAL PRIMARY KEY, course_code TEXT NOT NULL DEFAULT '', course_name TEXT NOT NULL, @@ -28,9 +28,9 @@ const init_db = `CREATE TABLE IF NOT EXISTS iqps_test ( fts_course_details tsvector GENERATED ALWAYS AS (to_tsvector('english', course_code || ' ' || course_name)) stored ); -CREATE INDEX IF NOT EXISTS iqps_test_fts ON iqps_test USING gin (fts_course_details); +CREATE INDEX IF NOT EXISTS iqps_fts ON iqps USING gin (fts_course_details); -create index IF NOT EXISTS idx_course_name_trgm on iqps_test using gin (course_name gin_trgm_ops); +create index IF NOT EXISTS idx_course_name_trgm on iqps using gin (course_name gin_trgm_ops); ` diff --git a/backend/query/queries.go b/backend/query/queries.go index 8fae97ab..47933bfa 100644 --- a/backend/query/queries.go +++ b/backend/query/queries.go @@ -65,7 +65,6 @@ with fuzzy as ( from iqps_test where (course_code || ' ' || course_name) %>> @query_text order by rank_ix - limit least(@match_count, 30) ), full_text as ( select @@ -78,7 +77,6 @@ full_text as ( fts_course_details @@ websearch_to_tsquery(@query_text) AND approve_status = true order by rank_ix - limit least(@match_count, 30) * 2 ), partial_search as ( select id, @@ -90,9 +88,8 @@ partial_search as ( websearch_to_tsquery('simple', @query_text)::text || ':*' ) AND approve_status = true - limit least(@match_count, 30) * 2 -) -select +), result as ( + select iqps_test.id,iqps_test.course_code, iqps_test.course_name, iqps_test.year, iqps_test.exam, iqps_test.filelink, iqps_test.from_library, iqps_test.upload_timestamp, iqps_test.approve_status from fuzzy @@ -104,6 +101,6 @@ order by coalesce(1.0 / (50 + full_text.rank_ix), 0.0) * 1 + coalesce(1.0 / (50 + partial_search.rank_ix), 0.0) * 1 desc -limit - least(@match_count, 30) +) + select * from result `