diff options
| author | Mark Wotton <mwotton@gmail.com> | 2021-09-09 17:46:46 +0700 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-09-09 11:46:46 +0100 |
| commit | 2024884f49334e7eaf64adc425da77b773204b42 (patch) | |
| tree | f5ed33336930230d873f4a06458fb08d6c45cb70 /atuin-client/src/database.rs | |
| parent | Update README.md (diff) | |
| download | atuin-2024884f49334e7eaf64adc425da77b773204b42.zip | |
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
Diffstat (limited to 'atuin-client/src/database.rs')
| -rw-r--r-- | atuin-client/src/database.rs | 24 |
1 files changed, 23 insertions, 1 deletions
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<Vec<History>> { + 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<Vec<History>> { @@ -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); + } } |
