From 01675aa0a70e49304b575538ded352a5a3925e07 Mon Sep 17 00:00:00 2001 From: GitHub Date: Sat, 11 Nov 2023 18:54:19 +0800 Subject: [PATCH] remove duplicate search result --- src/controller/inn.rs | 4 +--- src/controller/mod.rs | 2 +- src/controller/tantivy.rs | 13 +++++++++---- src/main.rs | 10 ++-------- 4 files changed, 13 insertions(+), 16 deletions(-) diff --git a/src/controller/inn.rs b/src/controller/inn.rs index 2e0b111..98fb003 100644 --- a/src/controller/inn.rs +++ b/src/controller/inn.rs @@ -723,9 +723,7 @@ pub(crate) async fn edit_post_post( claim.update_last_write(&DB)?; if inn.inn_type.as_str() != "Private" { - let is_update: &[u8] = if old_pid == 0 { &[] } else { &[0] }; - DB.open_tree("tan")? - .insert(format!("post{}", pid), is_update)?; + DB.open_tree("tan")?.insert(format!("post{}", pid), &[])?; } let target = format!("/post/{iid}/{pid}"); diff --git a/src/controller/mod.rs b/src/controller/mod.rs index 44ae1c5..65db1f4 100644 --- a/src/controller/mod.rs +++ b/src/controller/mod.rs @@ -15,7 +15,7 @@ //! | "user_uploads" | `uid#img_id` | `image_hash.ext` | //! | default | "imgs_count" | N | //! | "home_pages" | `uid` | `u8` | -//! | "tan" | `ctype#id` | `&[]`or &[0] | +//! | "tan" | `ctype#id` | `&[]` | //! //! ### notification //! | tree | key | value | diff --git a/src/controller/tantivy.rs b/src/controller/tantivy.rs index 16d0f13..181cd89 100644 --- a/src/controller/tantivy.rs +++ b/src/controller/tantivy.rs @@ -84,7 +84,7 @@ pub(crate) async fn search( } }; - let mut out_searchs = Vec::with_capacity(20); + let mut ids = HashSet::with_capacity(20); if !search.is_empty() { let (query, err) = SEARCHER.query_parser.parse_query_lenient(&query); if !err.is_empty() { @@ -99,9 +99,14 @@ pub(crate) async fn search( for (_score, doc_address) in top_docs { let doc = searcher.doc(doc_address)?; let id = doc.get_first(FIELDS.id).unwrap().as_text().unwrap(); - if let Some(out) = OutSearch::get(id, &DB) { - out_searchs.push(out); - } + ids.insert(id.to_owned()); + } + } + + let mut out_searchs = Vec::with_capacity(20); + for id in ids { + if let Some(out) = OutSearch::get(&id, &DB) { + out_searchs.push(out); } } diff --git a/src/main.rs b/src/main.rs index 1812a94..5d581c4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -67,18 +67,12 @@ async fn main() -> Result<(), AppError> { let mut subscriber = DB.open_tree("tan").unwrap().watch_prefix(vec![]); while let Some(event) = (&mut subscriber).await { let (k, op_type) = match event { - sled::Event::Insert { key, value } => { - if value.len() == 1 { - (key, "update") - } else { - (key, "add") - } - } + sled::Event::Insert { key, value: _ } => (key, "add"), sled::Event::Remove { key } => (key, "delete"), }; let id = String::from_utf8_lossy(&k); - if op_type == "update" || op_type == "add" { + if op_type == "add" { tan.add_doc(&id, &DB).unwrap(); }