fix slow search

This commit is contained in:
2025-03-26 17:02:57 +01:00
parent 1c25f67657
commit 923f1d5e73
3 changed files with 52 additions and 4 deletions

View File

@@ -64,8 +64,8 @@ func (m *ArticleRepository) Search(query string) ([]*model.Article, error) {
stmt := ` stmt := `
SELECT a.id, a.title, a.sourceurl, a.content, a.publishdate, a.fetchDate 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 FROM articles a JOIN documents d ON a.document_id = d.id
WHERE to_tsvector('german', d.content) @@ to_tsquery('german', $1) WHERE d.content_tsv @@ to_tsquery('german', $1)
ORDER BY ts_rank(to_tsvector('german', d.content), to_tsquery('german', $1)) DESC ORDER BY ts_rank(d.content_tsv, to_tsquery('german', $1)) DESC
LIMIT 10 LIMIT 10
` `

View File

@@ -69,8 +69,8 @@ func (m *ArticleViewModelRepository) Search(query string) ([]*model.ArticleViewM
stmt := ` stmt := `
SELECT a.id, a.title, a.sourceUrl, a.publishDate, d.summary SELECT a.id, a.title, a.sourceUrl, a.publishDate, d.summary
FROM articles a JOIN documents d ON a.document_id = d.id FROM articles a JOIN documents d ON a.document_id = d.id
WHERE to_tsvector('german', d.content) @@ to_tsquery('german', $1) WHERE d.content_tsv @@ to_tsquery('german', $1)
ORDER BY ts_rank(to_tsvector('german', d.content), to_tsquery('german', $1)) DESC ORDER BY ts_rank(d.content_tsv, to_tsquery('german', $1)) DESC
LIMIT 10 LIMIT 10
` `

View File

@@ -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