Files
2026-10-28 20:44:22 +07:00

28 lines
1001 B
PL/PgSQL

CREATE TABLE IF NOT EXISTS chunks (
id text PRIMARY KEY,
doc_id text NOT NULL,
title text NOT NULL DEFAULT '',
text text NOT NULL,
meta jsonb NOT NULL DEFAULT '{}'::jsonb,
tsv tsvector,
embedding vector(384)
);
CREATE INDEX IF NOT EXISTS chunks_doc_idx ON chunks (doc_id);
CREATE INDEX IF NOT EXISTS chunks_tsv_idx ON chunks USING GIN (tsv);
CREATE INDEX IF NOT EXISTS chunks_embedding_idx
ON chunks USING hnsw (embedding vector_cosine_ops)
WITH (m = 16, ef_construction = 64);
-- The tsvector is derived from the text, never written by hand.
CREATE OR REPLACE FUNCTION chunks_tsv_update() RETURNS trigger AS $$
BEGIN
NEW.tsv := to_tsvector('english', coalesce(NEW.title, '') || ' ' || NEW.text);
RETURN NEW;
END
$$ LANGUAGE plpgsql;
DROP TRIGGER IF EXISTS chunks_tsv_trigger ON chunks;
CREATE TRIGGER chunks_tsv_trigger BEFORE INSERT OR UPDATE ON chunks
FOR EACH ROW EXECUTE FUNCTION chunks_tsv_update();