From 8e1d15bade8592f5cd2848e77e72295316f5a726 Mon Sep 17 00:00:00 2001 From: Condorra Date: Thu, 23 Feb 2023 21:33:26 +1100 Subject: [PATCH] Allow partial matches on alias. --- blastmud_game/src/db.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/blastmud_game/src/db.rs b/blastmud_game/src/db.rs index 16da240e..5313b433 100644 --- a/blastmud_game/src/db.rs +++ b/blastmud_game/src/db.rs @@ -526,7 +526,7 @@ impl DBTrans { search.from_item.item_code); let (offset, mut query) = parse_offset(search.query); - let mut param_no: usize = 6; + let mut param_no: usize = 5; let mut extra_order: String = String::new(); let mut extra_where: String = String::new(); @@ -554,14 +554,12 @@ impl DBTrans { .replace("%", "") .to_lowercase() + "%"; let offset_sql = offset.map(|x| (if x >= 1 { x - 1 } else { x}) as i64).unwrap_or(0); - let query_json = serde_json::to_value(query.to_lowercase())?; let query_len = query.len() as i32; let limit = search.limit as i64; let mut params: Vec<&(dyn ToSql + Sync)> = vec!( &query_wildcard, &offset_sql, &query_len, - &query_json, &limit ); @@ -609,12 +607,14 @@ impl DBTrans { Ok(Arc::new(self.pg_trans()?.query( &format!( - "WITH {} SELECT details, aliases FROM relevant_items WHERE \ + "WITH {} SELECT details FROM relevant_items \ + WHERE \ ((lower(details->>'display') LIKE $1) \ OR (lower(details ->>'display_less_explicit') LIKE $1) \ - OR aliases @> $4) {} \ + OR EXISTS (SELECT 1 FROM jsonb_array_elements(aliases) AS al(alias) WHERE \ + LOWER(alias#>>'{{}}') LIKE $1)) {} \ ORDER BY {} ABS(length(details->>'display')-$3) ASC \ - LIMIT $5 OFFSET $2", &cte_str, &extra_where, &extra_order), + LIMIT $4 OFFSET $2", &cte_str, &extra_where, &extra_order), ¶ms ).await?.into_iter() .filter_map(|i| serde_json::from_value(i.get("details")).ok())