From b3870c1aa5c86487ffaf1a96a18c3826baf17f63 Mon Sep 17 00:00:00 2001 From: ljl <17743125563@163.com> Date: Thu, 2 Nov 2023 14:46:39 +0800 Subject: [PATCH] spi-stats:fix add or modify tsv pinyin. --- spi/spi-search/Cargo.toml | 4 +++- spi/spi-search/src/serv/pg/search_pg_item_serv.rs | 12 ++++++++---- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/spi/spi-search/Cargo.toml b/spi/spi-search/Cargo.toml index 6dba1ddc4..9cd6771a3 100644 --- a/spi/spi-search/Cargo.toml +++ b/spi/spi-search/Cargo.toml @@ -24,8 +24,10 @@ serde.workspace = true tardis = { workspace = true, features = ["reldb-postgres", "web-server", "web-client"] } bios-basic = { path = "../../basic", features = ["default"] } strum = { workerspace = true, features = ["derive"] } +pinyin = { version = "0.10" } [dev-dependencies] tardis = { workspace = true, features = ["test"] } bios-basic = { path = "../../basic", features = ["default", "test"] } -testcontainers-modules ={ workspace = true } \ No newline at end of file +testcontainers-modules ={ workspace = true } +pinyin = { version = "0.10" } 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 d08cf1e18..122c66ee9 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 @@ -1,5 +1,6 @@ use std::collections::HashMap; +use pinyin::{Pinyin, to_pinyin_vec}; use tardis::{ basic::{dto::TardisContext, error::TardisError, result::TardisResult}, chrono::Utc, @@ -29,8 +30,9 @@ pub async fn add(add_req: &mut SearchItemAddReq, _funs: &TardisFunsInst, ctx: &T params.push(Value::from(add_req.kind.to_string())); params.push(Value::from(add_req.key.to_string())); params.push(Value::from(add_req.title.as_str())); - params.push(Value::from(add_req.title.as_str())); + params.push(Value::from(format!("{},{}",add_req.title.as_str(), to_pinyin_vec(add_req.title.as_str(), Pinyin::plain).join(",")))); params.push(Value::from(add_req.content.as_str())); + params.push(Value::from(format!("{},{}",add_req.content.as_str(),to_pinyin_vec(add_req.content.as_str(),Pinyin::plain).join(",")))); params.push(Value::from(add_req.owner.as_ref().unwrap_or(&"".to_string()).as_str())); params.push(Value::from(add_req.own_paths.as_ref().unwrap_or(&"".to_string()).as_str())); params.push(Value::from(if let Some(create_time) = add_req.create_time { create_time } else { Utc::now() })); @@ -52,7 +54,7 @@ pub async fn add(add_req: &mut SearchItemAddReq, _funs: &TardisFunsInst, ctx: &T r#"INSERT INTO {table_name} (kind, key, title, title_tsv,content, content_tsv, owner, own_paths, create_time, update_time, ext, visit_keys) VALUES - ($1, $2, $3, to_tsvector('public.chinese_zh', $4), $5, to_tsvector('public.chinese_zh', $5), $6, $7, $8, $9, $10, {})"#, + ($1, $2, $3, to_tsvector('public.chinese_zh', $4), $5, to_tsvector('public.chinese_zh', $6), $7, $8, $9, $10, $11, {})"#, if add_req.visit_keys.is_some() { "$11" } else { "null" }, ), params, @@ -77,13 +79,15 @@ pub async fn modify(tag: &str, key: &str, modify_req: &mut SearchItemModifyReq, }; if let Some(title) = &modify_req.title { sql_sets.push(format!("title = ${}", params.len() + 1)); - sql_sets.push(format!("title_tsv = to_tsvector('public.chinese_zh', ${})", params.len() + 1)); params.push(Value::from(title)); + sql_sets.push(format!("title_tsv = to_tsvector('public.chinese_zh', ${})", params.len() + 1)); + params.push(Value::from(format!("{},{}",title,to_pinyin_vec(title, Pinyin::plain).join(",")))); }; if let Some(content) = &modify_req.content { sql_sets.push(format!("content = ${}", params.len() + 1)); - sql_sets.push(format!("content_tsv = to_tsvector('public.chinese_zh', ${})", params.len() + 1)); params.push(Value::from(content)); + sql_sets.push(format!("content_tsv = to_tsvector('public.chinese_zh', ${})", params.len() + 1)); + params.push(Value::from(format!("{},{}",content,to_pinyin_vec(content, Pinyin::plain).join(",")))); }; if let Some(owner) = &modify_req.owner { sql_sets.push(format!("owner = ${}", params.len() + 1));