From: Dessalines Date: Fri, 19 Nov 2021 20:07:20 +0000 (-0500) Subject: Fixing fuzzy_search to escape like chars. X-Git-Url: http://these/git/%7B%60%24%7BwebArchiveUrl%7D/%22%7B%7D/readmes/%22https:/hacktivis.me/%7Bthis.getImageSrc%28%29%7D?a=commitdiff_plain;h=e58c2048ed1a6d4438928cb854e0be052f53619e;p=lemmy.git Fixing fuzzy_search to escape like chars. --- diff --git a/crates/db_schema/src/impls/person.rs b/crates/db_schema/src/impls/person.rs index a9ba1d2e..b0476480 100644 --- a/crates/db_schema/src/impls/person.rs +++ b/crates/db_schema/src/impls/person.rs @@ -5,7 +5,7 @@ use crate::{ source::person::{Person, PersonForm}, traits::Crud, }; -use diesel::{dsl::*, result::Error, ExpressionMethods, PgConnection, QueryDsl, RunQueryDsl, *}; +use diesel::{dsl::*, result::Error, ExpressionMethods, PgConnection, QueryDsl, RunQueryDsl}; use url::Url; mod safe_type { @@ -194,7 +194,7 @@ impl Person { person .filter(deleted.eq(false)) .filter(local.eq(true)) - .filter(name.ilike(from_name)) + .filter(name.eq(from_name)) .first::(conn) } diff --git a/crates/db_schema/src/lib.rs b/crates/db_schema/src/lib.rs index f30f0bd8..a2a19290 100644 --- a/crates/db_schema/src/lib.rs +++ b/crates/db_schema/src/lib.rs @@ -70,7 +70,7 @@ pub fn from_opt_str_to_opt_enum(opt: &Option) -> O } pub fn fuzzy_search(q: &str) -> String { - let replaced = q.replace(" ", "%"); + let replaced = q.replace("%", "\\%").replace("_", "\\_").replace(" ", "%"); format!("%{}%", replaced) } @@ -154,8 +154,11 @@ mod tests { #[test] fn test_fuzzy_search() { - let test = "This is a fuzzy search"; - assert_eq!(fuzzy_search(test), "%This%is%a%fuzzy%search%".to_string()); + let test = "This %is% _a_ fuzzy search"; + assert_eq!( + fuzzy_search(test), + "%This%\\%is\\%%\\_a\\_%fuzzy%search%".to_string() + ); } #[test]