From efec2e670fcffb5f9a4ce130ea8bd3b42015702c Mon Sep 17 00:00:00 2001 From: Joe Ardent Date: Sat, 3 Feb 2024 15:04:05 -0800 Subject: [PATCH] fix fts table defs --- migrations/20240203053645_fts.up.sql | 19 +++++++++++++++---- src/watches/handlers.rs | 12 +++++++----- 2 files changed, 22 insertions(+), 9 deletions(-) diff --git a/migrations/20240203053645_fts.up.sql b/migrations/20240203053645_fts.up.sql index bb4b26d..f5c22b3 100644 --- a/migrations/20240203053645_fts.up.sql +++ b/migrations/20240203053645_fts.up.sql @@ -1,5 +1,16 @@ -create virtual table if not exists star_search using fts5(name, id); -insert into star_search(name, id) select name, id from stars; +create virtual table if not exists star_search using fts5 (name, id UNINDEXED, tokenize = 'trigram', content = 'stars', content_rowid=rowid); +create trigger if not exists stars_update_search after insert on stars begin + insert into star_search (rowid, name, id) values (new.rowid, new.name, new.id); +end; +create trigger if not exists stars_delete_search after delete on stars begin + insert into star_search (star_search, rowid, name, id) values ('delete', old.rowid, old.name, old.id); +end; + +create virtual table if not exists watch_search using fts5 (title, id UNINDEXED, tokenize = 'trigram', content = 'watches', content_rowid=rowid); +create trigger if not exists watches_update_search after insert on watches begin + insert into watch_search (rowid, title, id) values (new.rowid, new.title, new.id); +end; +create trigger if not exists watches_delete_search after delete on watches begin + insert into watch_search (watch_search, rowid, title, id) values ('delete', old.rowid, old.title, old.id); +end; -create virtual table if not exists watch_search using fts5(title, id); -insert into watch_search(title, id) select title, id from watches; diff --git a/src/watches/handlers.rs b/src/watches/handlers.rs index 2f8098f..6762ddd 100644 --- a/src/watches/handlers.rs +++ b/src/watches/handlers.rs @@ -250,11 +250,13 @@ pub async fn get_search_watch( let query = if search_query == SearchQuery::default() { query_as(DEFAULT_WATCHES_QUERY) } else if let Some(title) = search_query.title { - let q = format!("%{title}%"); - query_as("select * from watches where title like ?").bind(q) - } else if let Some(search) = search_query.search { - let q = format!("%{search}"); - query_as("select * from watches where title like ?").bind(q) + query_as( + "select * from watches where id in (select id from watch_search where title match ?)", + ) + .bind(title) + } else if let Some(ref search) = search_query.search { + query_as("select * from watches where id in (select id from watch_search where title match ?) outer join (select * from stars where id in (select id from star_search where name match ?)) s") + .bind(search).bind(search) } else { query_as(DEFAULT_WATCHES_QUERY) };