From 2024884f49334e7eaf64adc425da77b773204b42 Mon Sep 17 00:00:00 2001 From: Mark Wotton Date: Thu, 9 Sep 2021 17:46:46 +0700 Subject: Reordered fuzzy search (#179) * add test demonstrating problem * add a reordered fuzzy-search mode that presents shorter matches first, rather than using strict chronological ordering. * fix warnings, refactor interface to minspan slightly --- atuin-client/src/database.rs | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) (limited to 'atuin-client/src/database.rs') diff --git a/atuin-client/src/database.rs b/atuin-client/src/database.rs index 6a70ae33..8dff15fd 100644 --- a/atuin-client/src/database.rs +++ b/atuin-client/src/database.rs @@ -14,6 +14,7 @@ use sqlx::sqlite::{ use sqlx::Row; use super::history::History; +use super::ordering; use super::settings::SearchMode; #[async_trait] @@ -281,6 +282,7 @@ impl Database for Sqlite { search_mode: SearchMode, query: &str, ) -> Result> { + let orig_query = query; let query = query.to_string().replace("*", "%"); // allow wildcard char let limit = limit.map_or("".to_owned(), |l| format!("limit {}", l)); @@ -308,7 +310,7 @@ impl Database for Sqlite { .fetch_all(&self.pool) .await?; - Ok(res) + Ok(ordering::reorder_fuzzy(search_mode, orig_query, res)) } async fn query_history(&self, query: &str) -> Result> { @@ -405,4 +407,24 @@ mod test { results = db.search(None, SearchMode::Fuzzy, " ").await.unwrap(); assert_eq!(results.len(), 3); } + + #[tokio::test(flavor = "multi_thread")] + async fn test_search_reordered_fuzzy() { + let mut db = Sqlite::new("sqlite::memory:").await.unwrap(); + // test ordering of results: we should choose the first, even though it happened longer ago. + + new_history_item(&mut db, "curl").await.unwrap(); + new_history_item(&mut db, "corburl").await.unwrap(); + // if fuzzy reordering is on, it should come back in a more sensible order + let mut results = db.search(None, SearchMode::Fuzzy, "curl").await.unwrap(); + assert_eq!(results.len(), 2); + let commands: Vec<&String> = results.iter().map(|a| &a.command).collect(); + assert_eq!(commands, vec!["curl", "corburl"]); + + results = db.search(None, SearchMode::Fuzzy, "xxxx").await.unwrap(); + assert_eq!(results.len(), 0); + + results = db.search(None, SearchMode::Fuzzy, "").await.unwrap(); + assert_eq!(results.len(), 2); + } } -- cgit v1.3.1