Skip to content

YC (S26) | Open Computer History | Continuously record your company computer work, map your workflows, help you find work worth automating, and power your agents' context

full text indexing over chunked text

matthew-heartful5a4872084ef3
diff --git a/screenpipe-server/src/migrations/20240812205501_add_fts_index.sql b/screenpipe-server/src/migrations/20240812205501_add_fts_index.sql
new file mode 100644
index 000000000..d1b5148a3
--- /dev/null
+++ b/screenpipe-server/src/migrations/20240812205501_add_fts_index.sql
@@ -0,0 +1,23 @@
+-- Add migration script here
+-- Add FTS virtual table
+CREATE VIRTUAL TABLE IF NOT EXISTS chunked_text_index_fts USING fts5(text_id UNINDEXED, text);
+
+-- Populate FTS table with existing data
+INSERT INTO chunked_text_index_fts(text_id, text)
+SELECT text_id, text FROM chunked_text_index;
+
+-- Create a trigger to keep FTS table updated on INSERT
+CREATE TRIGGER IF NOT EXISTS chunked_text_index_ai AFTER INSERT ON chunked_text_index BEGIN
+ INSERT INTO chunked_text_index_fts(text_id, text) VALUES (new.text_id, new.text);
+END;
+
+-- Create a trigger to keep FTS table updated on DELETE
+CREATE TRIGGER IF NOT EXISTS chunked_text_index_ad AFTER DELETE ON chunked_text_index BEGIN
+ DELETE FROM chunked_text_index_fts WHERE text_id = old.text_id;
+END;
+
+-- Create a trigger to keep FTS table updated on UPDATE
+CREATE TRIGGER IF NOT EXISTS chunked_text_index_au AFTER UPDATE ON chunked_text_index BEGIN
+ DELETE FROM chunked_text_index_fts WHERE text_id = old.text_id;
+ INSERT INTO chunked_text_index_fts(text_id, text) VALUES (new.text_id, new.text);
+END;
\ No newline at end of file