From b127698847812e1fe5b8f4cab40917ecf0169814 Mon Sep 17 00:00:00 2001 From: ljl <17743125563@163.com> Date: Wed, 31 Jan 2024 14:55:03 +0800 Subject: [PATCH] spi-search:fix query own_paths --- spi/spi-search/src/dto/search_item_dto.rs | 1 + .../src/serv/pg/search_pg_item_serv.rs | 34 +++++++++++++++---- 2 files changed, 29 insertions(+), 6 deletions(-) diff --git a/spi/spi-search/src/dto/search_item_dto.rs b/spi/spi-search/src/dto/search_item_dto.rs index 140254307..7bb6837f8 100644 --- a/spi/spi-search/src/dto/search_item_dto.rs +++ b/spi/spi-search/src/dto/search_item_dto.rs @@ -175,6 +175,7 @@ pub struct SearchItemQueryReq { pub owners: Option>, // Match own_path, support prefix match pub own_paths: Option>, + pub rlike_own_paths: Option>, pub create_time_start: Option>, pub create_time_end: Option>, pub update_time_start: Option>, diff --git a/spi/spi-search/src/serv/pg/search_pg_item_serv.rs b/spi/spi-search/src/serv/pg/search_pg_item_serv.rs index 734e717df..2aa0c4c15 100644 --- a/spi/spi-search/src/serv/pg/search_pg_item_serv.rs +++ b/spi/spi-search/src/serv/pg/search_pg_item_serv.rs @@ -347,6 +347,17 @@ pub async fn search(search_req: &mut SearchItemSearchReq, funs: &TardisFunsInst, } } if let Some(own_paths) = &search_req.query.own_paths { + if !own_paths.is_empty() { + where_fragments.push(format!( + "own_paths in ({})", + (0..own_paths.len()).map(|idx| format!("${}", sql_vals.len() + idx + 1)).collect::>().join(",") + )); + for own_path in own_paths { + sql_vals.push(Value::from(format!("{own_path}%"))); + } + } + } + if let Some(own_paths) = &search_req.query.rlike_own_paths { if !own_paths.is_empty() { where_fragments.push(format!( "own_paths LIKE ANY (ARRAY[{}])", @@ -920,12 +931,23 @@ pub async fn query_metrics(query_req: &SearchQueryMetricsReq, funs: &TardisFunsI } if let Some(own_paths) = &query_req.query.own_paths { if !own_paths.is_empty() { - sql_part_wheres.push(format!( - "fact.own_paths LIKE ANY (ARRAY[{}])", - (0..own_paths.len()).map(|idx| format!("${}", params.len() + idx + 1)).collect::>().join(",") + where_fragments.push(format!( + "own_paths in ({})", + (0..own_paths.len()).map(|idx| format!("${}", sql_vals.len() + idx + 1)).collect::>().join(",") )); for own_path in own_paths { - params.push(Value::from(format!("{own_path}%"))); + sql_vals.push(Value::from(format!("{own_path}%"))); + } + } + } + if let Some(own_paths) = &query_req.query.rlike_own_paths { + if !own_paths.is_empty() { + where_fragments.push(format!( + "own_paths LIKE ANY (ARRAY[{}])", + (0..own_paths.len()).map(|idx| format!("${}", sql_vals.len() + idx + 1)).collect::>().join(",") + )); + for own_path in own_paths { + sql_vals.push(Value::from(format!("{own_path}%"))); } } } @@ -1215,8 +1237,8 @@ pub async fn query_metrics(query_req: &SearchQueryMetricsReq, funs: &TardisFunsI sql_adv_query.push(format!( " {} ( {} )", if group_query.group_by_or.unwrap_or(false) { "OR" } else { "AND" }, - sql_and_where.join(if group_query.ext_by_or.unwrap_or(false) { " OR " } else { " AND " })) - ); + sql_and_where.join(if group_query.ext_by_or.unwrap_or(false) { " OR " } else { " AND " }) + )); } } }