diff --git a/backend/notes.py b/backend/notes.py index df864ad..2763cf2 100644 --- a/backend/notes.py +++ b/backend/notes.py @@ -311,15 +311,27 @@ async def tag_counts() -> list[dict]: async def search_notes(query: str, limit: int = 50) -> list[dict]: async with aiosqlite.connect(settings.database_path) as db: db.row_factory = aiosqlite.Row - rows = await db.execute( - "SELECT f.note_id AS id, f.title, f.body, n.updated_at," - " n.created_at, u.name AS username, bm25(notes_fts) AS rank" - " FROM notes_fts f" - " JOIN notes n ON n.id = f.note_id AND n.deleted_at IS NULL" - " JOIN users u ON u.id = n.user_id" - " WHERE notes_fts MATCH ? ORDER BY rank LIMIT ?", - (query, limit), - ) + if query.lstrip().startswith("#"): + tag = query.lstrip()[1:].strip() + rows = await db.execute( + "SELECT n.id, n.title, n.updated_at, n.created_at, u.name AS username" + " FROM tags t" + " JOIN note_tags nt ON nt.tag_id = t.id" + " JOIN notes n ON n.id = nt.note_id AND n.deleted_at IS NULL" + " JOIN users u ON u.id = n.user_id" + " WHERE t.name = ? ORDER BY n.updated_at DESC LIMIT ?", + (tag, limit), + ) + else: + rows = await db.execute( + "SELECT f.note_id AS id, f.title, f.body, n.updated_at," + " n.created_at, u.name AS username, bm25(notes_fts) AS rank" + " FROM notes_fts f" + " JOIN notes n ON n.id = f.note_id AND n.deleted_at IS NULL" + " JOIN users u ON u.id = n.user_id" + " WHERE notes_fts MATCH ? ORDER BY rank LIMIT ?", + (query, limit), + ) notes = [dict(r) for r in await rows.fetchall()] for note in notes: note["tags"] = await get_note_tags(note["id"])