diff --git a/meilisearch-http/src/data/updates.rs b/meilisearch-http/src/data/updates.rs index a784dce99..5810c897c 100644 --- a/meilisearch-http/src/data/updates.rs +++ b/meilisearch-http/src/data/updates.rs @@ -59,13 +59,14 @@ impl Data { pub async fn update_settings( &self, index: impl AsRef + Send + Sync + 'static, - settings: Settings + settings: Settings, + create: bool, ) -> anyhow::Result { if !is_index_uid_valid(index.as_ref()) { bail!("invalid index uid: {:?}", index.as_ref()) } let index_controller = self.index_controller.clone(); - let update = tokio::task::spawn_blocking(move || index_controller.update_settings(index, settings)).await??; + let update = tokio::task::spawn_blocking(move || index_controller.update_settings(index, settings, create)).await??; Ok(update.into()) } diff --git a/meilisearch-http/src/index_controller/local_index_controller/mod.rs b/meilisearch-http/src/index_controller/local_index_controller/mod.rs index d3fa532dc..981bd7cff 100644 --- a/meilisearch-http/src/index_controller/local_index_controller/mod.rs +++ b/meilisearch-http/src/index_controller/local_index_controller/mod.rs @@ -5,7 +5,7 @@ mod update_handler; use std::path::Path; use std::sync::Arc; -use anyhow::{bail, Context}; +use anyhow::{bail, Context, anyhow}; use itertools::Itertools; use milli::Index; @@ -51,9 +51,14 @@ impl IndexController for LocalIndexController { fn update_settings>( &self, index: S, - settings: super::Settings + settings: super::Settings, + create: bool, ) -> anyhow::Result> { - let (_, update_store) = self.indexes.get_or_create_index(&index, self.update_db_size, self.index_db_size)?; + let (_, update_store) = if create { + self.indexes.get_or_create_index(&index, self.update_db_size, self.index_db_size)? + } else { + self.indexes.index(&index)?.ok_or_else(|| anyhow!("Index {:?} doesn't exist", index.as_ref()))? + }; let meta = UpdateMeta::Settings(settings); let pending = update_store.register_update(meta, &[])?; Ok(pending.into()) diff --git a/meilisearch-http/src/index_controller/mod.rs b/meilisearch-http/src/index_controller/mod.rs index b20e43749..2d23ebadf 100644 --- a/meilisearch-http/src/index_controller/mod.rs +++ b/meilisearch-http/src/index_controller/mod.rs @@ -142,8 +142,9 @@ pub trait IndexController { 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 - /// is applied to the index. - fn update_settings>(&self, index_uid: S, settings: Settings) -> anyhow::Result; + /// is applied to the index. `create` specifies whether an index should be created if not + /// existing. + fn update_settings>(&self, index_uid: S, settings: Settings, create: bool) -> anyhow::Result; /// Create an index with the given `index_uid`. fn create_index(&self, index_settings: IndexSettings) -> Result; diff --git a/meilisearch-http/src/routes/settings/mod.rs b/meilisearch-http/src/routes/settings/mod.rs index 00bc4220e..b65729b31 100644 --- a/meilisearch-http/src/routes/settings/mod.rs +++ b/meilisearch-http/src/routes/settings/mod.rs @@ -26,7 +26,7 @@ macro_rules! make_setting_route { $attr: Some(None), ..Default::default() }; - match data.update_settings(index_uid.into_inner(), settings).await { + match data.update_settings(index_uid.into_inner(), settings, false).await { Ok(update_status) => { let json = serde_json::to_string(&update_status).unwrap(); Ok(HttpResponse::Ok().body(json)) @@ -48,7 +48,7 @@ macro_rules! make_setting_route { ..Default::default() }; - match data.update_settings(index_uid.into_inner(), settings).await { + match data.update_settings(index_uid.into_inner(), settings, true).await { Ok(update_status) => { let json = serde_json::to_string(&update_status).unwrap(); Ok(HttpResponse::Ok().body(json)) @@ -137,7 +137,7 @@ async fn update_all( index_uid: web::Path, body: web::Json, ) -> Result { - match data.update_settings(index_uid.into_inner(), body.into_inner()).await { + match data.update_settings(index_uid.into_inner(), body.into_inner(), true).await { Ok(update_result) => { let json = serde_json::to_string(&update_result).unwrap(); Ok(HttpResponse::Ok().body(json)) @@ -170,7 +170,7 @@ async fn delete_all( index_uid: web::Path, ) -> Result { let settings = Settings::cleared(); - match data.update_settings(index_uid.into_inner(), settings).await { + match data.update_settings(index_uid.into_inner(), settings, false).await { Ok(update_result) => { let json = serde_json::to_string(&update_result).unwrap(); Ok(HttpResponse::Ok().body(json)) diff --git a/meilisearch-http/tests/settings/get_settings.rs b/meilisearch-http/tests/settings/get_settings.rs index 0e4d991da..d57c860b0 100644 --- a/meilisearch-http/tests/settings/get_settings.rs +++ b/meilisearch-http/tests/settings/get_settings.rs @@ -51,8 +51,6 @@ async fn test_partial_update() { } #[actix_rt::test] -#[ignore] -// need fix #54 async fn delete_settings_unexisting_index() { let server = Server::new().await; let index = server.index("test"); @@ -132,7 +130,6 @@ macro_rules! test_setting_routes { } #[actix_rt::test] - #[ignore] async fn delete_unexisting_index() { let server = Server::new().await; let url = format!("/indexes/test/settings/{}",