From 2ee9de866afceb68a1c20252ea4011d589e458cd Mon Sep 17 00:00:00 2001 From: Gabriel Kaszewski Date: Tue, 23 Dec 2025 02:18:55 +0100 Subject: [PATCH] feat: Enhance note search to include tag name matching and sort results by updated date. --- notes-infra/src/note_repository.rs | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/notes-infra/src/note_repository.rs b/notes-infra/src/note_repository.rs index d255bdf..34c2b3b 100644 --- a/notes-infra/src/note_repository.rs +++ b/notes-infra/src/note_repository.rs @@ -196,18 +196,29 @@ impl NoteRepository for SqliteNoteRepository { async fn search(&self, user_id: Uuid, query: &str) -> DomainResult> { let user_id_str = user_id.to_string(); - // Use FTS5 for full-text search + let like_query = format!("%{}%", query); + + // Use FTS5 for full-text search OR tag name match let rows: Vec = sqlx::query_as( r#" - SELECT n.id, n.user_id, n.title, n.content, n.color, n.is_pinned, n.is_archived, n.created_at, n.updated_at + SELECT DISTINCT n.id, n.user_id, n.title, n.content, n.color, n.is_pinned, n.is_archived, n.created_at, n.updated_at FROM notes n - INNER JOIN notes_fts fts ON n.rowid = fts.rowid - WHERE n.user_id = ? AND notes_fts MATCH ? - ORDER BY rank + WHERE n.user_id = ? + AND ( + n.rowid IN (SELECT rowid FROM notes_fts WHERE notes_fts MATCH ?) + OR + EXISTS ( + SELECT 1 FROM note_tags nt + JOIN tags t ON nt.tag_id = t.id + WHERE nt.note_id = n.id AND t.name LIKE ? + ) + ) + ORDER BY n.updated_at DESC "# ) .bind(&user_id_str) .bind(query) + .bind(like_query) .fetch_all(&self.pool) .await .map_err(|e| DomainError::RepositoryError(e.to_string()))?;