diff --git a/src/internal/model/database/articlerepository.go b/src/internal/model/database/articlerepository.go index 777bb36..2d065ed 100644 --- a/src/internal/model/database/articlerepository.go +++ b/src/internal/model/database/articlerepository.go @@ -64,8 +64,8 @@ func (m *ArticleRepository) Search(query string) ([]*model.Article, error) { stmt := ` SELECT a.id, a.title, a.sourceurl, a.content, a.publishdate, a.fetchDate FROM articles a JOIN documents d ON a.document_id = d.id - WHERE to_tsvector('german', d.content) @@ to_tsquery('german', $1) - ORDER BY ts_rank(to_tsvector('german', d.content), to_tsquery('german', $1)) DESC + WHERE d.content_tsv @@ to_tsquery('german', $1) + ORDER BY ts_rank(d.content_tsv, to_tsquery('german', $1)) DESC LIMIT 10 ` diff --git a/src/internal/model/database/articleviewmodelrepository copy.go b/src/internal/model/database/articleviewmodelrepository.go similarity index 94% rename from src/internal/model/database/articleviewmodelrepository copy.go rename to src/internal/model/database/articleviewmodelrepository.go index 384a5aa..6fb687f 100644 --- a/src/internal/model/database/articleviewmodelrepository copy.go +++ b/src/internal/model/database/articleviewmodelrepository.go @@ -69,8 +69,8 @@ func (m *ArticleViewModelRepository) Search(query string) ([]*model.ArticleViewM stmt := ` SELECT a.id, a.title, a.sourceUrl, a.publishDate, d.summary FROM articles a JOIN documents d ON a.document_id = d.id - WHERE to_tsvector('german', d.content) @@ to_tsquery('german', $1) - ORDER BY ts_rank(to_tsvector('german', d.content), to_tsquery('german', $1)) DESC + WHERE d.content_tsv @@ to_tsquery('german', $1) + ORDER BY ts_rank(d.content_tsv, to_tsquery('german', $1)) DESC LIMIT 10 ` diff --git a/src/migrations/20250326154214_precomputed_tsvector.sql b/src/migrations/20250326154214_precomputed_tsvector.sql new file mode 100644 index 0000000..4cf5463 --- /dev/null +++ b/src/migrations/20250326154214_precomputed_tsvector.sql @@ -0,0 +1,48 @@ +-- +goose Up +-- +goose StatementBegin + + + -- add the precomputed column + ALTER TABLE documents ADD COLUMN content_tsv tsvector; + + -- populate the new column with the initial data + UPDATE documents SET content_tsv = to_tsvector('german', content); + + -- Step 3: Create a trigger function to update the tsvector column upon insert or update + CREATE OR REPLACE FUNCTION update_tsvector() + RETURNS TRIGGER AS $$ + BEGIN + NEW.content_tsv := to_tsvector('german', NEW.content); + RETURN NEW; + END; + $$ LANGUAGE plpgsql; + + -- create the trigger on the documents table + CREATE TRIGGER documents_tsvector_update + BEFORE INSERT OR UPDATE ON documents + FOR EACH ROW EXECUTE FUNCTION update_tsvector(); + + -- create an index on the precomputed column for faster searches + CREATE INDEX idx_fts_documents ON documents USING GIN(content_tsv); + + +-- +goose StatementEnd + +-- +goose Down +-- +goose StatementBegin + + + -- drop the trigger that updates the tsvector column + DROP TRIGGER IF EXISTS documents_tsvector_update ON documents; + + -- Step 2: Drop the trigger function + DROP FUNCTION IF EXISTS update_tsvector(); + + -- Step 3: Drop the tsvector column + ALTER TABLE documents DROP COLUMN IF EXISTS content_tsv; + + -- Step 4: Drop the index on the tsvector column + DROP INDEX IF EXISTS idx_fts_documents; + + +-- +goose StatementEnd