tag search
This commit is contained in:
@@ -311,15 +311,27 @@ async def tag_counts() -> list[dict]:
|
|||||||
async def search_notes(query: str, limit: int = 50) -> list[dict]:
|
async def search_notes(query: str, limit: int = 50) -> list[dict]:
|
||||||
async with aiosqlite.connect(settings.database_path) as db:
|
async with aiosqlite.connect(settings.database_path) as db:
|
||||||
db.row_factory = aiosqlite.Row
|
db.row_factory = aiosqlite.Row
|
||||||
rows = await db.execute(
|
if query.lstrip().startswith("#"):
|
||||||
"SELECT f.note_id AS id, f.title, f.body, n.updated_at,"
|
tag = query.lstrip()[1:].strip()
|
||||||
" n.created_at, u.name AS username, bm25(notes_fts) AS rank"
|
rows = await db.execute(
|
||||||
" FROM notes_fts f"
|
"SELECT n.id, n.title, n.updated_at, n.created_at, u.name AS username"
|
||||||
" JOIN notes n ON n.id = f.note_id AND n.deleted_at IS NULL"
|
" FROM tags t"
|
||||||
" JOIN users u ON u.id = n.user_id"
|
" JOIN note_tags nt ON nt.tag_id = t.id"
|
||||||
" WHERE notes_fts MATCH ? ORDER BY rank LIMIT ?",
|
" JOIN notes n ON n.id = nt.note_id AND n.deleted_at IS NULL"
|
||||||
(query, limit),
|
" 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()]
|
notes = [dict(r) for r in await rows.fetchall()]
|
||||||
for note in notes:
|
for note in notes:
|
||||||
note["tags"] = await get_note_tags(note["id"])
|
note["tags"] = await get_note_tags(note["id"])
|
||||||
|
|||||||
Reference in New Issue
Block a user