From 28b9c158b15ad19bdfc8d6e2b0c805b0901fdd2f Mon Sep 17 00:00:00 2001 From: mpostma Date: Sat, 13 Feb 2021 10:44:20 +0100 Subject: [PATCH] implement delete single document --- src/data/updates.rs | 6 ++--- .../local_index_controller/update_handler.rs | 4 +-- src/index_controller/mod.rs | 2 +- src/routes/document.rs | 25 +++++++++++++------ 4 files changed, 22 insertions(+), 15 deletions(-) diff --git a/src/data/updates.rs b/src/data/updates.rs index 1f3290e47..9fd2015f6 100644 --- a/src/data/updates.rs +++ b/src/data/updates.rs @@ -64,21 +64,19 @@ impl Data { pub async fn clear_documents( &self, - index: impl AsRef, + index: impl AsRef + Sync + Send + 'static, ) -> anyhow::Result { let index_controller = self.index_controller.clone(); - let index = index.as_ref().to_string(); let update = tokio::task::spawn_blocking(move || index_controller.clear_documents(index)).await??; Ok(update.into()) } pub async fn delete_documents( &self, - index: impl AsRef, + index: impl AsRef + Sync + Send + 'static, document_ids: Vec, ) -> anyhow::Result { let index_controller = self.index_controller.clone(); - let index = index.as_ref().to_string(); let update = tokio::task::spawn_blocking(move || index_controller.delete_documents(index, document_ids)).await??; Ok(update.into()) } diff --git a/src/index_controller/local_index_controller/update_handler.rs b/src/index_controller/local_index_controller/update_handler.rs index 505e9aff0..7b7487ffa 100644 --- a/src/index_controller/local_index_controller/update_handler.rs +++ b/src/index_controller/local_index_controller/update_handler.rs @@ -187,7 +187,7 @@ impl UpdateHandler { let mut txn = self.index.write_txn()?; let mut builder = update_builder.delete_documents(&mut txn, &self.index)?; - // we ignore unexisting document ids + // We ignore unexisting document ids ids.iter().for_each(|id| { builder.delete_external_id(id); }); match builder.execute() { @@ -215,7 +215,7 @@ impl HandleUpdate for UpdateHandler { let result = match meta.meta() { DocumentsAddition { method, format } => self.update_documents(*format, *method, content, update_builder), ClearDocuments => self.clear_documents(update_builder), - DeleteDocuments => self.delete_documents(content, update_builder,), + DeleteDocuments => self.delete_documents(content, update_builder), Settings(settings) => self.update_settings(settings, update_builder), Facets(levels) => self.update_facets(levels, update_builder), }; diff --git a/src/index_controller/mod.rs b/src/index_controller/mod.rs index e811b6478..b881e3268 100644 --- a/src/index_controller/mod.rs +++ b/src/index_controller/mod.rs @@ -133,7 +133,7 @@ pub trait IndexController { /// Clear all documents in the given index. fn clear_documents(&self, index: impl AsRef) -> anyhow::Result; - /// Clear all documents in the given index. + /// Delete all documents in `document_ids`. fn delete_documents(&self, index: impl AsRef, document_ids: Vec) -> anyhow::Result; /// Updates an index settings. If the index does not exist, it will be created when the update diff --git a/src/routes/document.rs b/src/routes/document.rs index 67ca2a02f..874ad722b 100644 --- a/src/routes/document.rs +++ b/src/routes/document.rs @@ -30,8 +30,8 @@ type Document = IndexMap; #[derive(Deserialize)] struct DocumentParam { - _index_uid: String, - _document_id: String, + index_uid: String, + document_id: String, } pub fn services(cfg: &mut web::ServiceConfig) { @@ -60,10 +60,19 @@ async fn get_document( wrap = "Authentication::Private" )] async fn delete_document( - _data: web::Data, - _path: web::Path, + data: web::Data, + path: web::Path, ) -> Result { - todo!() + match data.delete_documents(path.index_uid.clone(), vec![path.document_id.clone()]).await { + Ok(result) => { + let json = serde_json::to_string(&result).unwrap(); + Ok(HttpResponse::Ok().body(json)) + } + Err(e) => { + error!("{}", e); + unimplemented!() + } + } } #[derive(Deserialize)] @@ -166,10 +175,10 @@ async fn delete_documents( ) -> Result { let ids = body .iter() - .map(ToString::to_string) + .map(|v| v.as_str().map(String::from).unwrap_or_else(|| v.to_string())) .collect(); - match data.delete_documents(&path.index_uid, ids).await { + match data.delete_documents(path.index_uid.clone(), ids).await { Ok(result) => { let json = serde_json::to_string(&result).unwrap(); Ok(HttpResponse::Ok().body(json)) @@ -186,7 +195,7 @@ async fn clear_all_documents( data: web::Data, path: web::Path, ) -> Result { - match data.clear_documents(&path.index_uid).await { + match data.clear_documents(path.index_uid.clone()).await { Ok(update) => { let json = serde_json::to_string(&update).unwrap(); Ok(HttpResponse::Ok().body(json))