From 6a32432b01d9469c7c57790fde475bec39766589 Mon Sep 17 00:00:00 2001 From: qdequele Date: Mon, 27 Jan 2020 18:33:40 +0100 Subject: [PATCH] add /settings/index-new-fields routes --- meilisearch-http/src/routes/mod.rs | 3 ++ meilisearch-http/src/routes/setting.rs | 43 ++++++++++++++++++++++++++ 2 files changed, 46 insertions(+) diff --git a/meilisearch-http/src/routes/mod.rs b/meilisearch-http/src/routes/mod.rs index 61681294c..40305e369 100644 --- a/meilisearch-http/src/routes/mod.rs +++ b/meilisearch-http/src/routes/mod.rs @@ -136,6 +136,9 @@ pub fn load_routes(app: &mut tide::Server) { .post(|ctx| into_response(setting::update_displayed(ctx))) .delete(|ctx| into_response(setting::delete_displayed(ctx))); }); + router.at("/index-new-fields") + .get(|ctx| into_response(setting::get_index_new_fields(ctx))) + .post(|ctx| into_response(setting::update_index_new_fields(ctx))); router .at("/synonyms") diff --git a/meilisearch-http/src/routes/setting.rs b/meilisearch-http/src/routes/setting.rs index 27b0eff3e..33b26b877 100644 --- a/meilisearch-http/src/routes/setting.rs +++ b/meilisearch-http/src/routes/setting.rs @@ -544,3 +544,46 @@ pub async fn delete_displayed(ctx: Request) -> SResult { let response_body = IndexUpdateResponse { update_id }; Ok(tide::Response::new(202).body_json(&response_body).unwrap()) } + +#[derive(Default, Clone, Serialize, Deserialize)] +#[serde(rename_all = "camelCase", deny_unknown_fields)] +pub struct IndexNewFieldsSettings { + pub index_new_fields: Option, +} + +pub async fn get_index_new_fields(ctx: Request) -> SResult { + ctx.is_allowed(SettingsRead)?; + let index = ctx.index()?; + let db = &ctx.state().db; + let reader = db.main_read_txn()?; + + let schema = index.main.schema(&reader)?; + + let index_new_fields = schema.map(|s| s.must_index_new_fields()); + + let settings = IndexNewFieldsSettings { + index_new_fields, + }; + + Ok(tide::Response::new(200).body_json(&settings).unwrap()) +} + +pub async fn update_index_new_fields(mut ctx: Request) -> SResult { + ctx.is_allowed(SettingsWrite)?; + let index = ctx.index()?; + let settings: IndexNewFieldsSettings = + ctx.body_json().await.map_err(ResponseError::bad_request)?; + let db = &ctx.state().db; + + let settings = Settings { + index_new_fields: settings.index_new_fields, + ..Settings::default() + }; + + let mut writer = db.update_write_txn()?; + let update_id = index.settings_update(&mut writer, settings.into_cleared())?; + writer.commit()?; + + let response_body = IndexUpdateResponse { update_id }; + Ok(tide::Response::new(202).body_json(&response_body).unwrap()) +}