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

FTS FTW #6

Merged
merged 4 commits into from
Feb 16, 2024
Merged
Show file tree
Hide file tree
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
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ iqps.db
crawler/cache
crawler/qp/*.pdf
qp.csv
qp.tar.xz
qp.tar.xz

go.work.sum
2 changes: 1 addition & 1 deletion backend/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ FROM golang:1.21 AS builder
WORKDIR /src
COPY . .
RUN go mod download
RUN CGO_ENABLED=1 GOOS=linux go build -o /app -a -ldflags '-linkmode external -extldflags "-static"' .
RUN CGO_ENABLED=1 GOOS=linux go build -o /app --tags "fts5" -a -ldflags '-linkmode external -extldflags "-static"' .

FROM busybox:1.36-musl

Expand Down
23 changes: 14 additions & 9 deletions backend/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@ type QuestionPaper struct {
FromLibrary bool `json:"from_library"`
}

var db *sql.DB
var staticFilesUrl string
var (
db *sql.DB
staticFilesUrl string
)

const init_db = `
CREATE TABLE IF NOT EXISTS qp (
Expand Down Expand Up @@ -79,7 +81,7 @@ func library(w http.ResponseWriter, r *http.Request) {

var qps []QuestionPaper = make([]QuestionPaper, 0)
for rows.Next() {
var qp = QuestionPaper{}
qp := QuestionPaper{}
err := rows.Scan(&qp.ID, &qp.CourseCode, &qp.CourseName, &qp.Year, &qp.Exam, &qp.FileLink, &qp.FromLibrary)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
Expand All @@ -101,7 +103,9 @@ func search(w http.ResponseWriter, r *http.Request) {
http.Error(w, "course is required", http.StatusBadRequest)
return
}
query := fmt.Sprintf(`SELECT * FROM qp WHERE course_name like '%%%s%%' OR course_code like '%%%s%%'`, course, course)
query := `SELECT * FROM qp WHERE rowid IN (SELECT rowid FROM qp_better WHERE course_name MATCH ?)`
var params []interface{}
params = append(params, course)

year := r.URL.Query().Get("year")
if year != "" {
Expand All @@ -110,15 +114,17 @@ func search(w http.ResponseWriter, r *http.Request) {
http.Error(w, "year must be a number", http.StatusBadRequest)
return
}
query = fmt.Sprintf(`%s AND year = %d`, query, yearInt)
query = fmt.Sprintf(`%s AND year = ?`, query)
params = append(params, strconv.Itoa(yearInt))
}

exam := r.URL.Query().Get("exam")
if exam != "" {
query = fmt.Sprintf(`%s AND exam = '%s'`, query, exam)
query = fmt.Sprintf(`%s AND exam = '?'`, query)
params = append(params, exam)
}

rows, err := db.Query(query)
rows, err := db.Query(query, params...)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
Expand All @@ -127,7 +133,7 @@ func search(w http.ResponseWriter, r *http.Request) {

var qps []QuestionPaper = make([]QuestionPaper, 0)
for rows.Next() {
var qp = QuestionPaper{}
qp := QuestionPaper{}
err := rows.Scan(&qp.ID, &qp.CourseCode, &qp.CourseName, &qp.Year, &qp.Exam, &qp.FileLink, &qp.FromLibrary)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
Expand All @@ -146,7 +152,6 @@ func search(w http.ResponseWriter, r *http.Request) {
}

func main() {

err := godotenv.Load(".env")
if err != nil {
log.Fatal(err)
Expand Down
2 changes: 2 additions & 0 deletions backend/search.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
CREATE VIRTUAL TABLE qp_better USING fts5(course_name, tokenize="porter ascii");
INSERT INTO qp_better SELECT course_name FROM qp;