diff --git a/meilisearch-core/src/settings.rs b/meilisearch-core/src/settings.rs index 30d9cd361..efc5ec493 100644 --- a/meilisearch-core/src/settings.rs +++ b/meilisearch-core/src/settings.rs @@ -2,7 +2,7 @@ use std::sync::Mutex; use std::collections::{BTreeMap, BTreeSet, HashSet}; use std::str::FromStr; -use serde::{Deserialize, Serialize}; +use serde::{Deserialize, Deserializer, Serialize}; use once_cell::sync::Lazy; static RANKING_RULE_REGEX: Lazy> = Lazy::new(|| { @@ -14,36 +14,30 @@ static RANKING_RULE_REGEX: Lazy> = Lazy::new(|| { #[derive(Default, Clone, Serialize, Deserialize)] #[serde(rename_all = "camelCase", deny_unknown_fields)] pub struct Settings { - pub ranking_rules: Option>, - pub ranking_distinct: Option, - pub attribute_identifier: Option, - pub attributes_searchable: Option>, - pub attributes_displayed: Option>, - pub stop_words: Option>, - pub synonyms: Option>>, - pub index_new_fields: Option, + #[serde(default, deserialize_with = "deserialize_some")] + pub ranking_rules: Option>>, + #[serde(default, deserialize_with = "deserialize_some")] + pub ranking_distinct: Option>, + #[serde(default, deserialize_with = "deserialize_some")] + pub attribute_identifier: Option>, + #[serde(default, deserialize_with = "deserialize_some")] + pub attributes_searchable: Option>>, + #[serde(default, deserialize_with = "deserialize_some")] + pub attributes_displayed: Option>>, + #[serde(default, deserialize_with = "deserialize_some")] + pub stop_words: Option>>, + #[serde(default, deserialize_with = "deserialize_some")] + pub synonyms: Option>>>, + #[serde(default, deserialize_with = "deserialize_some")] + pub index_new_fields: Option>, } -impl Settings { - pub fn into_cleared(self) -> SettingsUpdate { - let settings = self.clone(); - - let ranking_rules = match settings.ranking_rules { - Some(rules) => Some(RankingRule::from_vec(rules)), - None => None - }; - - SettingsUpdate { - ranking_rules: UpdateState::convert_with_default(ranking_rules, UpdateState::Clear), - ranking_distinct: UpdateState::convert_with_default(settings.ranking_distinct, UpdateState::Clear), - attribute_identifier: UpdateState::convert_with_default(settings.attribute_identifier, UpdateState::Clear), - attributes_searchable: UpdateState::convert_with_default(settings.attributes_searchable, UpdateState::Clear), - attributes_displayed: UpdateState::convert_with_default(settings.attributes_displayed, UpdateState::Clear), - stop_words: UpdateState::convert_with_default(settings.stop_words, UpdateState::Clear), - synonyms: UpdateState::convert_with_default(settings.synonyms, UpdateState::Clear), - index_new_fields: UpdateState::convert_with_default(settings.index_new_fields, UpdateState::Clear), - } - } +// Any value that is present is considered Some value, including null. +fn deserialize_some<'de, T, D>(deserializer: D) -> Result, D::Error> + where T: Deserialize<'de>, + D: Deserializer<'de> +{ + Deserialize::deserialize(deserializer).map(Some) } impl Into for Settings { @@ -51,12 +45,13 @@ impl Into for Settings { let settings = self.clone(); let ranking_rules = match settings.ranking_rules { - Some(rules) => Some(RankingRule::from_vec(rules)), - None => None, + Some(Some(rules)) => UpdateState::Update(RankingRule::from_vec(rules)), + Some(None) => UpdateState::Clear, + None => UpdateState::Nothing, }; SettingsUpdate { - ranking_rules: ranking_rules.into(), + ranking_rules: ranking_rules, ranking_distinct: settings.ranking_distinct.into(), attribute_identifier: settings.attribute_identifier.into(), attributes_searchable: settings.attributes_searchable.into(), @@ -77,10 +72,11 @@ pub enum UpdateState { Nothing, } -impl From> for UpdateState { - fn from(opt: Option) -> UpdateState { +impl From>> for UpdateState { + fn from(opt: Option>) -> UpdateState { match opt { - Some(t) => UpdateState::Update(t), + Some(Some(t)) => UpdateState::Update(t), + Some(None) => UpdateState::Clear, None => UpdateState::Nothing, } } @@ -93,13 +89,6 @@ impl UpdateState { _ => true, } } - - fn convert_with_default(opt: Option, default: UpdateState) -> UpdateState { - match opt { - Some(t) => UpdateState::Update(t), - None => default, - } - } } #[derive(Debug, Clone)] diff --git a/meilisearch-http/src/cors.rs b/meilisearch-http/src/cors.rs index e30ff4d69..0f48e4310 100644 --- a/meilisearch-http/src/cors.rs +++ b/meilisearch-http/src/cors.rs @@ -249,8 +249,8 @@ mod test { const ENDPOINT: &str = "/cors"; - fn app() -> crate::Server<()> { - let mut app = crate::Server::new(); + fn app() -> tide::Server<()> { + let mut app = tide::Server::new(); app.at(ENDPOINT).get(|_| async move { "Hello World" }); app diff --git a/meilisearch-http/src/routes/document.rs b/meilisearch-http/src/routes/document.rs index 1980f0790..4c90c329b 100644 --- a/meilisearch-http/src/routes/document.rs +++ b/meilisearch-http/src/routes/document.rs @@ -146,7 +146,7 @@ async fn update_multiple_documents(mut ctx: Request, is_partial: bool) -> }, }; let settings = Settings { - attribute_identifier: Some(id), + attribute_identifier: Some(Some(id)), ..Settings::default() }; index.settings_update(&mut update_writer, settings.into())?; diff --git a/meilisearch-http/src/routes/index.rs b/meilisearch-http/src/routes/index.rs index 5bb234920..f1bd31f46 100644 --- a/meilisearch-http/src/routes/index.rs +++ b/meilisearch-http/src/routes/index.rs @@ -1,6 +1,7 @@ use chrono::{DateTime, Utc}; use log::error; use meilisearch_core::ProcessedUpdateResult; +use meilisearch_schema::Schema; use rand::seq::SliceRandom; use serde::{Deserialize, Serialize}; use serde_json::json; @@ -94,6 +95,7 @@ pub async fn get_index(ctx: Request) -> SResult { struct IndexCreateRequest { name: Option, uid: Option, + attribute_identifier: Option, } #[derive(Debug, Serialize)] @@ -148,6 +150,10 @@ pub async fn create_index(mut ctx: Request) -> SResult { .updated_at(&writer)? .into_internal_error()?; + if let Some(id) = body.attribute_identifier { + created_index.main.put_schema(&mut writer, &Schema::with_identifier(id))?; + } + writer.commit()?; let response_body = IndexCreateResponse { diff --git a/meilisearch-http/src/routes/setting.rs b/meilisearch-http/src/routes/setting.rs index 6a01e92d7..8c58d324b 100644 --- a/meilisearch-http/src/routes/setting.rs +++ b/meilisearch-http/src/routes/setting.rs @@ -60,27 +60,52 @@ pub async fn get_all(ctx: Request) -> SResult { let index_new_fields = schema.map(|s| s.must_index_new_fields()); let settings = Settings { - ranking_rules, - ranking_distinct, - attribute_identifier, - attributes_searchable, - attributes_displayed, - stop_words, - synonyms, - index_new_fields, + ranking_rules: Some(ranking_rules), + ranking_distinct: Some(ranking_distinct), + attribute_identifier: Some(attribute_identifier), + attributes_searchable: Some(attributes_searchable), + attributes_displayed: Some(attributes_displayed), + stop_words: Some(stop_words), + synonyms: Some(synonyms), + index_new_fields: Some(index_new_fields), }; Ok(tide::Response::new(200).body_json(&settings).unwrap()) } +#[derive(Default, Clone, Deserialize)] +#[serde(rename_all = "camelCase", deny_unknown_fields)] +pub struct UpdateSettings { + pub ranking_rules: Option>, + pub ranking_distinct: Option, + pub attribute_identifier: Option, + pub attributes_searchable: Option>, + pub attributes_displayed: Option>, + pub stop_words: Option>, + pub synonyms: Option>>, + pub index_new_fields: Option, +} + + pub async fn update_all(mut ctx: Request) -> SResult { ctx.is_allowed(SettingsWrite)?; let index = ctx.index()?; - let settings: Settings = ctx.body_json().await.map_err(ResponseError::bad_request)?; + let settings_update: UpdateSettings = ctx.body_json().await.map_err(ResponseError::bad_request)?; let db = &ctx.state().db; + let settings = Settings { + ranking_rules: Some(settings_update.ranking_rules), + ranking_distinct: Some(settings_update.ranking_distinct), + attribute_identifier: Some(settings_update.attribute_identifier), + attributes_searchable: Some(settings_update.attributes_searchable), + attributes_displayed: Some(settings_update.attributes_displayed), + stop_words: Some(settings_update.stop_words), + synonyms: Some(settings_update.synonyms), + index_new_fields: Some(settings_update.index_new_fields), + }; + let mut writer = db.update_write_txn()?; - let update_id = index.settings_update(&mut writer, settings.into_cleared())?; + let update_id = index.settings_update(&mut writer, settings.into())?; writer.commit()?; let response_body = IndexUpdateResponse { update_id }; @@ -153,13 +178,13 @@ pub async fn update_ranking(mut ctx: Request) -> SResult { let db = &ctx.state().db; let settings = Settings { - ranking_rules: settings.ranking_rules, - ranking_distinct: settings.ranking_distinct, + ranking_rules: Some(settings.ranking_rules), + ranking_distinct: Some(settings.ranking_distinct), ..Settings::default() }; let mut writer = db.update_write_txn()?; - let update_id = index.settings_update(&mut writer, settings.into_cleared())?; + let update_id = index.settings_update(&mut writer, settings.into())?; writer.commit()?; let response_body = IndexUpdateResponse { update_id }; @@ -208,12 +233,12 @@ pub async fn update_rules(mut ctx: Request) -> SResult { let db = &ctx.state().db; let settings = Settings { - ranking_rules, + ranking_rules: Some(ranking_rules), ..Settings::default() }; let mut writer = db.update_write_txn()?; - let update_id = index.settings_update(&mut writer, settings.into_cleared())?; + let update_id = index.settings_update(&mut writer, settings.into())?; writer.commit()?; let response_body = IndexUpdateResponse { update_id }; @@ -272,12 +297,12 @@ pub async fn update_distinct(mut ctx: Request) -> SResult { let db = &ctx.state().db; let settings = Settings { - ranking_distinct, + ranking_distinct: Some(ranking_distinct), ..Settings::default() }; let mut writer = db.update_write_txn()?; - let update_id = index.settings_update(&mut writer, settings.into_cleared())?; + let update_id = index.settings_update(&mut writer, settings.into())?; writer.commit()?; let response_body = IndexUpdateResponse { update_id }; @@ -348,14 +373,14 @@ pub async fn update_attributes(mut ctx: Request) -> SResult { let db = &ctx.state().db; let settings = Settings { - attribute_identifier: settings.attribute_identifier, - attributes_searchable: settings.attributes_searchable, - attributes_displayed: settings.attributes_displayed, + attribute_identifier: Some(settings.attribute_identifier), + attributes_searchable: Some(settings.attributes_searchable), + attributes_displayed: Some(settings.attributes_displayed), ..Settings::default() }; let mut writer = db.update_write_txn()?; - let update_id = index.settings_update(&mut writer, settings.into_cleared())?; + let update_id = index.settings_update(&mut writer, settings.into())?; writer.commit()?; let response_body = IndexUpdateResponse { update_id }; @@ -425,12 +450,12 @@ pub async fn update_searchable(mut ctx: Request) -> SResult { let db = &ctx.state().db; let settings = Settings { - attributes_searchable, + attributes_searchable: Some(attributes_searchable), ..Settings::default() }; let mut writer = db.update_write_txn()?; - let update_id = index.settings_update(&mut writer, settings.into_cleared())?; + let update_id = index.settings_update(&mut writer, settings.into())?; writer.commit()?; let response_body = IndexUpdateResponse { update_id }; @@ -478,12 +503,12 @@ pub async fn update_displayed(mut ctx: Request) -> SResult { let db = &ctx.state().db; let settings = Settings { - attributes_displayed, + attributes_displayed: Some(attributes_displayed), ..Settings::default() }; let mut writer = db.update_write_txn()?; - let update_id = index.settings_update(&mut writer, settings.into_cleared())?; + let update_id = index.settings_update(&mut writer, settings.into())?; writer.commit()?; let response_body = IndexUpdateResponse { update_id }; @@ -531,12 +556,12 @@ pub async fn update_index_new_fields(mut ctx: Request) -> SResult