feat: add fts indexing

This commit is contained in:
2026-04-15 08:31:06 -04:00
parent ac343a5477
commit 562f4bb073
11 changed files with 393 additions and 83 deletions

76
db/fts.go Normal file
View File

@@ -0,0 +1,76 @@
package db
import (
"context"
"strings"
)
// UpsertFileContent replaces the FTS content for a given file_id.
func (q *Queries) UpsertFileContent(ctx context.Context, fileID int64, content string) error {
// Delete existing content for this file
if _, err := q.db.ExecContext(ctx, `DELETE FROM file_contents WHERE file_id = ?`, fileID); err != nil {
return err
}
// Insert new content
_, err := q.db.ExecContext(ctx, `INSERT INTO file_contents (file_id, content) VALUES (?, ?)`, fileID, content)
return err
}
// DeleteFileContentByFileID removes FTS content for a file.
func (q *Queries) DeleteFileContentByFileID(ctx context.Context, fileID int64) error {
_, err := q.db.ExecContext(ctx, `DELETE FROM file_contents WHERE file_id = ?`, fileID)
return err
}
// DeleteStaleFileContents removes FTS content for files not in the given path list.
func (q *Queries) DeleteStaleFileContents(ctx context.Context, paths []string) error {
if len(paths) == 0 {
_, err := q.db.ExecContext(ctx, `DELETE FROM file_contents`)
return err
}
placeholders := make([]string, len(paths))
args := make([]interface{}, len(paths))
for i, p := range paths {
placeholders[i] = "?"
args[i] = p
}
query := `DELETE FROM file_contents WHERE file_id NOT IN (
SELECT id FROM files WHERE path IN (` + strings.Join(placeholders, ",") + `)
)`
_, err := q.db.ExecContext(ctx, query, args...)
return err
}
// SearchResult holds a single FTS search hit.
type SearchResult struct {
FileID int64
Path string
Snippet string
}
// SearchFileContents performs a full-text search across all file contents.
// Returns matching file paths with a snippet of the match context.
func (q *Queries) SearchFileContents(ctx context.Context, query string, limit int) ([]SearchResult, error) {
rows, err := q.db.QueryContext(ctx, `
SELECT fc.file_id, f.path, snippet(file_contents, 1, '>>>', '<<<', '...', 20)
FROM file_contents fc
JOIN files f ON f.id = fc.file_id
WHERE file_contents MATCH ?
ORDER BY rank
LIMIT ?
`, query, limit)
if err != nil {
return nil, err
}
defer rows.Close()
var results []SearchResult
for rows.Next() {
var r SearchResult
if err := rows.Scan(&r.FileID, &r.Path, &r.Snippet); err != nil {
return nil, err
}
results = append(results, r)
}
return results, rows.Err()
}

View File

@@ -17,6 +17,11 @@ type File struct {
IndexedAt sql.NullTime
}
type FileContent struct {
FileID string
Content string
}
type Symbol struct {
ID int64
FileID int64

View File

@@ -32,3 +32,11 @@ CREATE INDEX idx_symbols_exported ON symbols(exported, kind);
CREATE INDEX idx_files_path ON files(path);
CREATE INDEX idx_files_language ON files(language);
CREATE INDEX idx_files_package ON files(package);
-- FTS5 virtual table for full-text search of file contents.
-- content is stored here (not external content), keyed by file_id.
CREATE VIRTUAL TABLE file_contents USING fts5(
file_id UNINDEXED,
content,
tokenize='porter unicode61'
);