implement delete single document

This commit is contained in:
mpostma 2021-02-13 10:44:20 +01:00
parent c317af58bc
commit 28b9c158b1
No known key found for this signature in database
GPG Key ID: CBC8A7C1D7A28C3A
4 changed files with 22 additions and 15 deletions

View File

@ -64,21 +64,19 @@ impl Data {
pub async fn clear_documents( pub async fn clear_documents(
&self, &self,
index: impl AsRef<str>, index: impl AsRef<str> + Sync + Send + 'static,
) -> anyhow::Result<UpdateStatus> { ) -> anyhow::Result<UpdateStatus> {
let index_controller = self.index_controller.clone(); 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??; let update = tokio::task::spawn_blocking(move || index_controller.clear_documents(index)).await??;
Ok(update.into()) Ok(update.into())
} }
pub async fn delete_documents( pub async fn delete_documents(
&self, &self,
index: impl AsRef<str>, index: impl AsRef<str> + Sync + Send + 'static,
document_ids: Vec<String>, document_ids: Vec<String>,
) -> anyhow::Result<UpdateStatus> { ) -> anyhow::Result<UpdateStatus> {
let index_controller = self.index_controller.clone(); 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??; let update = tokio::task::spawn_blocking(move || index_controller.delete_documents(index, document_ids)).await??;
Ok(update.into()) Ok(update.into())
} }

View File

@ -187,7 +187,7 @@ impl UpdateHandler {
let mut txn = self.index.write_txn()?; let mut txn = self.index.write_txn()?;
let mut builder = update_builder.delete_documents(&mut txn, &self.index)?; 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); }); ids.iter().for_each(|id| { builder.delete_external_id(id); });
match builder.execute() { match builder.execute() {
@ -215,7 +215,7 @@ impl HandleUpdate<UpdateMeta, UpdateResult, String> for UpdateHandler {
let result = match meta.meta() { let result = match meta.meta() {
DocumentsAddition { method, format } => self.update_documents(*format, *method, content, update_builder), DocumentsAddition { method, format } => self.update_documents(*format, *method, content, update_builder),
ClearDocuments => self.clear_documents(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), Settings(settings) => self.update_settings(settings, update_builder),
Facets(levels) => self.update_facets(levels, update_builder), Facets(levels) => self.update_facets(levels, update_builder),
}; };

View File

@ -133,7 +133,7 @@ pub trait IndexController {
/// Clear all documents in the given index. /// Clear all documents in the given index.
fn clear_documents(&self, index: impl AsRef<str>) -> anyhow::Result<UpdateStatus>; fn clear_documents(&self, index: impl AsRef<str>) -> anyhow::Result<UpdateStatus>;
/// Clear all documents in the given index. /// Delete all documents in `document_ids`.
fn delete_documents(&self, index: impl AsRef<str>, document_ids: Vec<String>) -> anyhow::Result<UpdateStatus>; fn delete_documents(&self, index: impl AsRef<str>, document_ids: Vec<String>) -> anyhow::Result<UpdateStatus>;
/// Updates an index settings. If the index does not exist, it will be created when the update /// Updates an index settings. If the index does not exist, it will be created when the update

View File

@ -30,8 +30,8 @@ type Document = IndexMap<String, Value>;
#[derive(Deserialize)] #[derive(Deserialize)]
struct DocumentParam { struct DocumentParam {
_index_uid: String, index_uid: String,
_document_id: String, document_id: String,
} }
pub fn services(cfg: &mut web::ServiceConfig) { pub fn services(cfg: &mut web::ServiceConfig) {
@ -60,10 +60,19 @@ async fn get_document(
wrap = "Authentication::Private" wrap = "Authentication::Private"
)] )]
async fn delete_document( async fn delete_document(
_data: web::Data<Data>, data: web::Data<Data>,
_path: web::Path<DocumentParam>, path: web::Path<DocumentParam>,
) -> Result<HttpResponse, ResponseError> { ) -> Result<HttpResponse, ResponseError> {
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)] #[derive(Deserialize)]
@ -166,10 +175,10 @@ async fn delete_documents(
) -> Result<HttpResponse, ResponseError> { ) -> Result<HttpResponse, ResponseError> {
let ids = body let ids = body
.iter() .iter()
.map(ToString::to_string) .map(|v| v.as_str().map(String::from).unwrap_or_else(|| v.to_string()))
.collect(); .collect();
match data.delete_documents(&path.index_uid, ids).await { match data.delete_documents(path.index_uid.clone(), ids).await {
Ok(result) => { Ok(result) => {
let json = serde_json::to_string(&result).unwrap(); let json = serde_json::to_string(&result).unwrap();
Ok(HttpResponse::Ok().body(json)) Ok(HttpResponse::Ok().body(json))
@ -186,7 +195,7 @@ async fn clear_all_documents(
data: web::Data<Data>, data: web::Data<Data>,
path: web::Path<IndexParam>, path: web::Path<IndexParam>,
) -> Result<HttpResponse, ResponseError> { ) -> Result<HttpResponse, ResponseError> {
match data.clear_documents(&path.index_uid).await { match data.clear_documents(path.index_uid.clone()).await {
Ok(update) => { Ok(update) => {
let json = serde_json::to_string(&update).unwrap(); let json = serde_json::to_string(&update).unwrap();
Ok(HttpResponse::Ok().body(json)) Ok(HttpResponse::Ok().body(json))