feat: added fts5 backend
This commit is contained in:
1 parent
d5cae6efa9
commit
98aa7dd68c
6 files changed
+141
-37
No files matched your search
@@ -184,6 +184,68 @@ func (s *sqliteStore) tombstone(ctx context.Context, publicID int64, version int
|
||||
return id, nil
|
||||
}
|
||||
|
||||
func (s *sqliteStore) indexFTS(ctx context.Context, id int64, note string) error {
|
||||
_, err := s.db.ExecContext(ctx,
|
||||
`INSERT INTO flashcard_fts(rowid, note) VALUES (?, ?)`,
|
||||
id, note,
|
||||
)
|
||||
if err != nil {
|
||||
return fmt.Errorf("index fts: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *sqliteStore) deleteFTS(ctx context.Context, id int64) error {
|
||||
_, err := s.db.ExecContext(ctx,
|
||||
`DELETE FROM flashcard_fts WHERE rowid = ?`,
|
||||
id,
|
||||
)
|
||||
if err != nil {
|
||||
return fmt.Errorf("delete fts: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *sqliteStore) searchFTS(ctx context.Context, query string, limit uint64) ([]searchResult, error) {
|
||||
if limit == 0 {
|
||||
limit = 5
|
||||
}
|
||||
|
||||
rows, err := s.db.QueryContext(ctx,
|
||||
`SELECT rowid, rank FROM flashcard_fts WHERE note MATCH ? ORDER BY rank LIMIT ?`,
|
||||
query, limit,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("fts query: %w", err)
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var results []searchResult
|
||||
for rows.Next() {
|
||||
var id int64
|
||||
var rank float64
|
||||
if err := rows.Scan(&id, &rank); err != nil {
|
||||
return nil, fmt.Errorf("scan: %w", err)
|
||||
}
|
||||
entry, err := s.getByID(ctx, id)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("get entry: %w", err)
|
||||
}
|
||||
if entry == nil {
|
||||
continue
|
||||
}
|
||||
results = append(results, searchResult{
|
||||
Entry: *entry,
|
||||
Score: float32(-rank),
|
||||
})
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, fmt.Errorf("rows: %w", err)
|
||||
}
|
||||
|
||||
return results, nil
|
||||
}
|
||||
|
||||
func (s *sqliteStore) close() error {
|
||||
return s.db.Close()
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user