@@ -33,6 +33,10 @@ def add_document(self, document: Document) -> str:
3333 "INSERT INTO chunks (document_id, content, embedding) VALUES (?, ?, vector_convert_f32(?))" ,
3434 (document_id , chunk .content , chunk .embedding ),
3535 )
36+ cursor .execute (
37+ "INSERT INTO chunks_fts (rowid, content) VALUES (last_insert_rowid(), ?)" ,
38+ (chunk .content ,),
39+ )
3640
3741 self ._conn .commit ()
3842
@@ -62,11 +66,11 @@ def find_document_by_id_or_uri(self, identifier: str) -> Document | None:
6266 """Find document by ID or URI"""
6367 cursor = self ._conn .cursor ()
6468 cursor .execute (
65- "SELECT id, content, uri, metadata, created_at FROM documents WHERE id = ? OR uri = ?" ,
66- (identifier , identifier )
69+ "SELECT id, content, uri, metadata, created_at FROM documents WHERE id = ? OR uri = ?" ,
70+ (identifier , identifier ),
6771 )
6872 row = cursor .fetchone ()
69-
73+
7074 if row :
7175 doc_id , content , uri , metadata , created_at = row
7276 return Document (
@@ -81,17 +85,21 @@ def find_document_by_id_or_uri(self, identifier: str) -> Document | None:
8185 def remove_document (self , document_id : str ) -> bool :
8286 """Remove document and its chunks by document ID"""
8387 cursor = self ._conn .cursor ()
84-
88+
8589 # Check if document exists
8690 cursor .execute ("SELECT COUNT(*) FROM documents WHERE id = ?" , (document_id ,))
8791 if cursor .fetchone ()[0 ] == 0 :
8892 return False
89-
90- # Remove chunks first (foreign key constraint)
93+
94+ # Remove chunks first
95+ cursor .execute (
96+ "DELETE FROM chunks_fts WHERE rowid IN (SELECT rowid FROM chunks WHERE document_id = ?)" ,
97+ (document_id ,),
98+ )
9199 cursor .execute ("DELETE FROM chunks WHERE document_id = ?" , (document_id ,))
92-
100+
93101 # Remove document
94102 cursor .execute ("DELETE FROM documents WHERE id = ?" , (document_id ,))
95-
103+
96104 self ._conn .commit ()
97105 return True
0 commit comments