From a067a1b16b5a661652a454ef2b0245d786619014 Mon Sep 17 00:00:00 2001 From: qdequele Date: Tue, 25 Feb 2020 15:51:37 +0100 Subject: [PATCH 1/9] replace index_new_fields to accept_new_fields; fix #475 --- meilisearch-core/src/settings.rs | 8 +++---- .../src/update/settings_update.rs | 6 ++--- meilisearch-http/src/routes/mod.rs | 4 ++-- meilisearch-http/src/routes/setting.rs | 22 +++++++++---------- meilisearch-schema/src/schema.rs | 14 ++++++------ 5 files changed, 27 insertions(+), 27 deletions(-) diff --git a/meilisearch-core/src/settings.rs b/meilisearch-core/src/settings.rs index 01790bd8a..d0f9718f5 100644 --- a/meilisearch-core/src/settings.rs +++ b/meilisearch-core/src/settings.rs @@ -26,7 +26,7 @@ pub struct Settings { #[serde(default, deserialize_with = "deserialize_some")] pub synonyms: Option>>>, #[serde(default, deserialize_with = "deserialize_some")] - pub index_new_fields: Option>, + pub accept_new_fields: Option>, } // Any value that is present is considered Some value, including null. @@ -55,7 +55,7 @@ impl Settings { displayed_attributes: settings.displayed_attributes.into(), stop_words: settings.stop_words.into(), synonyms: settings.synonyms.into(), - index_new_fields: settings.index_new_fields.into(), + accept_new_fields: settings.accept_new_fields.into(), }) } } @@ -161,7 +161,7 @@ pub struct SettingsUpdate { pub displayed_attributes: UpdateState>, pub stop_words: UpdateState>, pub synonyms: UpdateState>>, - pub index_new_fields: UpdateState, + pub accept_new_fields: UpdateState, } impl Default for SettingsUpdate { @@ -174,7 +174,7 @@ impl Default for SettingsUpdate { displayed_attributes: UpdateState::Nothing, stop_words: UpdateState::Nothing, synonyms: UpdateState::Nothing, - index_new_fields: UpdateState::Nothing, + accept_new_fields: UpdateState::Nothing, } } } diff --git a/meilisearch-core/src/update/settings_update.rs b/meilisearch-core/src/update/settings_update.rs index e733adac7..b100ba777 100644 --- a/meilisearch-core/src/update/settings_update.rs +++ b/meilisearch-core/src/update/settings_update.rs @@ -68,12 +68,12 @@ pub fn apply_settings_update( UpdateState::Nothing => (), } - match settings.index_new_fields { + match settings.accept_new_fields { UpdateState::Update(v) => { - schema.set_index_new_fields(v); + schema.set_accept_new_fields(v); }, UpdateState::Clear => { - schema.set_index_new_fields(true); + schema.set_accept_new_fields(true); }, UpdateState::Nothing => (), } diff --git a/meilisearch-http/src/routes/mod.rs b/meilisearch-http/src/routes/mod.rs index 24e7f2203..a12d226c8 100644 --- a/meilisearch-http/src/routes/mod.rs +++ b/meilisearch-http/src/routes/mod.rs @@ -101,8 +101,8 @@ pub fn load_routes(app: &mut tide::Server) { .delete(|ctx| into_response(setting::delete_displayed(ctx))); app.at("/indexes/:index/settings/index-new-field") - .get(|ctx| into_response(setting::get_index_new_fields(ctx))) - .post(|ctx| into_response(setting::update_index_new_fields(ctx))); + .get(|ctx| into_response(setting::get_accept_new_fields(ctx))) + .post(|ctx| into_response(setting::update_accept_new_fields(ctx))); app.at("/indexes/:index/settings/synonyms") .get(|ctx| into_response(synonym::get(ctx))) diff --git a/meilisearch-http/src/routes/setting.rs b/meilisearch-http/src/routes/setting.rs index 277170fad..b70636217 100644 --- a/meilisearch-http/src/routes/setting.rs +++ b/meilisearch-http/src/routes/setting.rs @@ -77,7 +77,7 @@ pub async fn get_all(ctx: Request) -> SResult { Some(attrs) } }); - let index_new_fields = schema.map(|s| s.index_new_fields()); + let accept_new_fields = schema.map(|s| s.accept_new_fields()); let settings = Settings { ranking_rules: Some(ranking_rules), @@ -86,7 +86,7 @@ pub async fn get_all(ctx: Request) -> SResult { displayed_attributes, stop_words: Some(stop_words), synonyms: Some(synonyms), - index_new_fields: Some(index_new_fields), + accept_new_fields: Some(accept_new_fields), }; Ok(tide::Response::new(200).body_json(&settings).unwrap()) @@ -102,7 +102,7 @@ pub struct UpdateSettings { pub displayed_attributes: Option>, pub stop_words: Option>, pub synonyms: Option>>, - pub index_new_fields: Option, + pub accept_new_fields: Option, } pub async fn update_all(mut ctx: Request) -> SResult { @@ -119,7 +119,7 @@ pub async fn update_all(mut ctx: Request) -> SResult { displayed_attributes: Some(settings_update.displayed_attributes), stop_words: Some(settings_update.stop_words), synonyms: Some(settings_update.synonyms), - index_new_fields: Some(settings_update.index_new_fields), + accept_new_fields: Some(settings_update.accept_new_fields), }; let mut writer = db.update_write_txn()?; @@ -144,7 +144,7 @@ pub async fn delete_all(ctx: Request) -> SResult { displayed_attributes: UpdateState::Clear, stop_words: UpdateState::Clear, synonyms: UpdateState::Clear, - index_new_fields: UpdateState::Clear, + accept_new_fields: UpdateState::Clear, }; let update_id = index.settings_update(&mut writer, settings)?; @@ -385,7 +385,7 @@ pub async fn delete_displayed(ctx: Request) -> SResult { Ok(tide::Response::new(202).body_json(&response_body)?) } -pub async fn get_index_new_fields(ctx: Request) -> SResult { +pub async fn get_accept_new_fields(ctx: Request) -> SResult { ctx.is_allowed(Private)?; let index = ctx.index()?; let db = &ctx.state().db; @@ -393,22 +393,22 @@ pub async fn get_index_new_fields(ctx: Request) -> SResult { let schema = index.main.schema(&reader)?; - let index_new_fields = schema.map(|s| s.index_new_fields()); + let accept_new_fields = schema.map(|s| s.accept_new_fields()); Ok(tide::Response::new(200) - .body_json(&index_new_fields) + .body_json(&accept_new_fields) .unwrap()) } -pub async fn update_index_new_fields(mut ctx: Request) -> SResult { +pub async fn update_accept_new_fields(mut ctx: Request) -> SResult { ctx.is_allowed(Private)?; let index = ctx.index()?; - let index_new_fields: Option = + let accept_new_fields: Option = ctx.body_json().await.map_err(ResponseError::bad_request)?; let db = &ctx.state().db; let settings = Settings { - index_new_fields: Some(index_new_fields), + accept_new_fields: Some(accept_new_fields), ..Settings::default() }; diff --git a/meilisearch-schema/src/schema.rs b/meilisearch-schema/src/schema.rs index 5a3858f0d..d66693bc9 100644 --- a/meilisearch-schema/src/schema.rs +++ b/meilisearch-schema/src/schema.rs @@ -13,7 +13,7 @@ pub struct Schema { indexed: Vec, indexed_map: HashMap, - index_new_fields: bool, + accept_new_fields: bool, } impl Schema { @@ -28,7 +28,7 @@ impl Schema { displayed: HashSet::new(), indexed: Vec::new(), indexed_map: HashMap::new(), - index_new_fields: true, + accept_new_fields: true, } } @@ -68,7 +68,7 @@ impl Schema { Ok(id) } None => { - if self.index_new_fields { + if self.accept_new_fields { self.set_indexed(name)?; self.set_displayed(name) } else { @@ -190,11 +190,11 @@ impl Schema { Ok(()) } - pub fn index_new_fields(&self) -> bool { - self.index_new_fields + pub fn accept_new_fields(&self) -> bool { + self.accept_new_fields } - pub fn set_index_new_fields(&mut self, value: bool) { - self.index_new_fields = value; + pub fn set_accept_new_fields(&mut self, value: bool) { + self.accept_new_fields = value; } } From 2eb6f81c589dae67fdcaa0773a11617d1046da44 Mon Sep 17 00:00:00 2001 From: qdequele Date: Tue, 25 Feb 2020 16:10:34 +0100 Subject: [PATCH 2/9] rename ranking_distinct to distinct_attribute; fix #474 --- meilisearch-core/src/settings.rs | 8 ++++---- meilisearch-core/src/store/main.rs | 14 ++++++------- .../src/update/settings_update.rs | 6 +++--- meilisearch-http/src/routes/mod.rs | 2 +- meilisearch-http/src/routes/setting.rs | 20 +++++++++---------- 5 files changed, 25 insertions(+), 25 deletions(-) diff --git a/meilisearch-core/src/settings.rs b/meilisearch-core/src/settings.rs index d0f9718f5..b97a14874 100644 --- a/meilisearch-core/src/settings.rs +++ b/meilisearch-core/src/settings.rs @@ -16,7 +16,7 @@ pub struct Settings { #[serde(default, deserialize_with = "deserialize_some")] pub ranking_rules: Option>>, #[serde(default, deserialize_with = "deserialize_some")] - pub ranking_distinct: Option>, + pub distinct_attribute: Option>, #[serde(default, deserialize_with = "deserialize_some")] pub searchable_attributes: Option>>, #[serde(default, deserialize_with = "deserialize_some")] @@ -49,7 +49,7 @@ impl Settings { Ok(SettingsUpdate { ranking_rules, - ranking_distinct: settings.ranking_distinct.into(), + distinct_attribute: settings.distinct_attribute.into(), identifier: UpdateState::Nothing, searchable_attributes: settings.searchable_attributes.into(), displayed_attributes: settings.displayed_attributes.into(), @@ -155,7 +155,7 @@ impl RankingRule { #[derive(Debug, Clone, Serialize, Deserialize)] pub struct SettingsUpdate { pub ranking_rules: UpdateState>, - pub ranking_distinct: UpdateState, + pub distinct_attribute: UpdateState, pub identifier: UpdateState, pub searchable_attributes: UpdateState>, pub displayed_attributes: UpdateState>, @@ -168,7 +168,7 @@ impl Default for SettingsUpdate { fn default() -> Self { Self { ranking_rules: UpdateState::Nothing, - ranking_distinct: UpdateState::Nothing, + distinct_attribute: UpdateState::Nothing, identifier: UpdateState::Nothing, searchable_attributes: UpdateState::Nothing, displayed_attributes: UpdateState::Nothing, diff --git a/meilisearch-core/src/store/main.rs b/meilisearch-core/src/store/main.rs index 2e13d52ec..a156cee4b 100644 --- a/meilisearch-core/src/store/main.rs +++ b/meilisearch-core/src/store/main.rs @@ -12,7 +12,7 @@ use crate::settings::RankingRule; const CREATED_AT_KEY: &str = "created-at"; const RANKING_RULES_KEY: &str = "ranking-rules"; -const RANKING_DISTINCT_KEY: &str = "ranking-distinct"; +const DISTINCT_ATTRIBUTE_KEY: &str = "distinct-attribute"; const STOP_WORDS_KEY: &str = "stop-words"; const SYNONYMS_KEY: &str = "synonyms"; const CUSTOMS_KEY: &str = "customs"; @@ -200,19 +200,19 @@ impl Main { self.main.delete::<_, Str>(writer, RANKING_RULES_KEY) } - pub fn ranking_distinct(&self, reader: &heed::RoTxn) -> ZResult> { - if let Some(value) = self.main.get::<_, Str, Str>(reader, RANKING_DISTINCT_KEY)? { + pub fn distinct_attribute(&self, reader: &heed::RoTxn) -> ZResult> { + if let Some(value) = self.main.get::<_, Str, Str>(reader, DISTINCT_ATTRIBUTE_KEY)? { return Ok(Some(value.to_owned())) } return Ok(None) } - pub fn put_ranking_distinct(self, writer: &mut heed::RwTxn, value: &str) -> ZResult<()> { - self.main.put::<_, Str, Str>(writer, RANKING_DISTINCT_KEY, value) + pub fn put_distinct_attribute(self, writer: &mut heed::RwTxn, value: &str) -> ZResult<()> { + self.main.put::<_, Str, Str>(writer, DISTINCT_ATTRIBUTE_KEY, value) } - pub fn delete_ranking_distinct(self, writer: &mut heed::RwTxn) -> ZResult { - self.main.delete::<_, Str>(writer, RANKING_DISTINCT_KEY) + pub fn delete_distinct_attribute(self, writer: &mut heed::RwTxn) -> ZResult { + self.main.delete::<_, Str>(writer, DISTINCT_ATTRIBUTE_KEY) } pub fn put_customs(self, writer: &mut heed::RwTxn, customs: &[u8]) -> ZResult<()> { diff --git a/meilisearch-core/src/update/settings_update.rs b/meilisearch-core/src/update/settings_update.rs index b100ba777..aa7d3f505 100644 --- a/meilisearch-core/src/update/settings_update.rs +++ b/meilisearch-core/src/update/settings_update.rs @@ -58,12 +58,12 @@ pub fn apply_settings_update( UpdateState::Nothing => (), } - match settings.ranking_distinct { + match settings.distinct_attribute { UpdateState::Update(v) => { - index.main.put_ranking_distinct(writer, &v)?; + index.main.put_distinct_attribute(writer, &v)?; }, UpdateState::Clear => { - index.main.delete_ranking_distinct(writer)?; + index.main.delete_distinct_attribute(writer)?; }, UpdateState::Nothing => (), } diff --git a/meilisearch-http/src/routes/mod.rs b/meilisearch-http/src/routes/mod.rs index a12d226c8..5caa765f5 100644 --- a/meilisearch-http/src/routes/mod.rs +++ b/meilisearch-http/src/routes/mod.rs @@ -82,7 +82,7 @@ pub fn load_routes(app: &mut tide::Server) { .post(|ctx| into_response(setting::update_rules(ctx))) .delete(|ctx| into_response(setting::delete_rules(ctx))); - app.at("/indexes/:index/settings/ranking-distinct") + app.at("/indexes/:index/settings/distinct-attribute") .get(|ctx| into_response(setting::get_distinct(ctx))) .post(|ctx| into_response(setting::update_distinct(ctx))) .delete(|ctx| into_response(setting::delete_distinct(ctx))); diff --git a/meilisearch-http/src/routes/setting.rs b/meilisearch-http/src/routes/setting.rs index b70636217..f106a69ea 100644 --- a/meilisearch-http/src/routes/setting.rs +++ b/meilisearch-http/src/routes/setting.rs @@ -50,7 +50,7 @@ pub async fn get_all(ctx: Request) -> SResult { Some(rules) => Some(rules.iter().map(|r| r.to_string()).collect()), None => None, }; - let ranking_distinct = index.main.ranking_distinct(&reader)?; + let distinct_attribute = index.main.distinct_attribute(&reader)?; let schema = index.main.schema(&reader)?; @@ -81,7 +81,7 @@ pub async fn get_all(ctx: Request) -> SResult { let settings = Settings { ranking_rules: Some(ranking_rules), - ranking_distinct: Some(ranking_distinct), + distinct_attribute: Some(distinct_attribute), searchable_attributes, displayed_attributes, stop_words: Some(stop_words), @@ -96,7 +96,7 @@ pub async fn get_all(ctx: Request) -> SResult { #[serde(rename_all = "camelCase", deny_unknown_fields)] pub struct UpdateSettings { pub ranking_rules: Option>, - pub ranking_distinct: Option, + pub distinct_attribute: Option, pub identifier: Option, pub searchable_attributes: Option>, pub displayed_attributes: Option>, @@ -114,7 +114,7 @@ pub async fn update_all(mut ctx: Request) -> SResult { let settings = Settings { ranking_rules: Some(settings_update.ranking_rules), - ranking_distinct: Some(settings_update.ranking_distinct), + distinct_attribute: Some(settings_update.distinct_attribute), searchable_attributes: Some(settings_update.searchable_attributes), displayed_attributes: Some(settings_update.displayed_attributes), stop_words: Some(settings_update.stop_words), @@ -138,7 +138,7 @@ pub async fn delete_all(ctx: Request) -> SResult { let settings = SettingsUpdate { ranking_rules: UpdateState::Clear, - ranking_distinct: UpdateState::Clear, + distinct_attribute: UpdateState::Clear, identifier: UpdateState::Clear, searchable_attributes: UpdateState::Clear, displayed_attributes: UpdateState::Clear, @@ -214,22 +214,22 @@ pub async fn get_distinct(ctx: Request) -> SResult { let db = &ctx.state().db; let reader = db.main_read_txn()?; - let ranking_distinct = index.main.ranking_distinct(&reader)?; + let distinct_attribute = index.main.distinct_attribute(&reader)?; Ok(tide::Response::new(200) - .body_json(&ranking_distinct) + .body_json(&distinct_attribute) .unwrap()) } pub async fn update_distinct(mut ctx: Request) -> SResult { ctx.is_allowed(Private)?; let index = ctx.index()?; - let ranking_distinct: Option = + let distinct_attribute: Option = ctx.body_json().await.map_err(ResponseError::bad_request)?; let db = &ctx.state().db; let settings = Settings { - ranking_distinct: Some(ranking_distinct), + distinct_attribute: Some(distinct_attribute), ..Settings::default() }; @@ -248,7 +248,7 @@ pub async fn delete_distinct(ctx: Request) -> SResult { let mut writer = db.update_write_txn()?; let settings = SettingsUpdate { - ranking_distinct: UpdateState::Clear, + distinct_attribute: UpdateState::Clear, ..SettingsUpdate::default() }; From aa95c26e071a2c4fe9b87e30e116b3e7c01a3d1d Mon Sep 17 00:00:00 2001 From: qdequele Date: Wed, 26 Feb 2020 16:05:02 +0100 Subject: [PATCH 3/9] update tests --- meilisearch-http/tests/common.rs | 4 ++-- meilisearch-http/tests/search.rs | 28 ++++++++++++++-------------- meilisearch-http/tests/settings.rs | 18 +++++++++--------- 3 files changed, 25 insertions(+), 25 deletions(-) diff --git a/meilisearch-http/tests/common.rs b/meilisearch-http/tests/common.rs index a9bb0848b..c30888cbe 100644 --- a/meilisearch-http/tests/common.rs +++ b/meilisearch-http/tests/common.rs @@ -66,7 +66,7 @@ pub fn enrich_server_with_movies_settings( "_exactness", "dsc(vote_average)", ], - "rankingDistinct": null, + "distinctAttribute": null, "searchableAttributes": [ "title", "tagline", @@ -92,7 +92,7 @@ pub fn enrich_server_with_movies_settings( ], "stopWords": null, "synonyms": null, - "indexNewFields": false, + "acceptNewFields": false, }); let body = json.to_string().into_bytes(); diff --git a/meilisearch-http/tests/search.rs b/meilisearch-http/tests/search.rs index eb510c90b..42fd901e2 100644 --- a/meilisearch-http/tests/search.rs +++ b/meilisearch-http/tests/search.rs @@ -635,7 +635,7 @@ fn search_with_settings_basic() { "_exactness", "dsc(vote_average)" ], - "rankingDistinct": null, + "distinctAttribute": null, "identifier": "id", "searchableAttributes": [ "title", @@ -662,7 +662,7 @@ fn search_with_settings_basic() { ], "stopWords": null, "synonyms": null, - "indexNewFields": false, + "acceptNewFields": false, }); common::update_config(&mut server, config); @@ -741,7 +741,7 @@ fn search_with_settings_stop_words() { "_exactness", "dsc(vote_average)" ], - "rankingDistinct": null, + "distinctAttribute": null, "identifier": "id", "searchableAttributes": [ "title", @@ -768,7 +768,7 @@ fn search_with_settings_stop_words() { ], "stopWords": ["the"], "synonyms": null, - "indexNewFields": false, + "acceptNewFields": false, }); common::update_config(&mut server, config); @@ -848,7 +848,7 @@ fn search_with_settings_synonyms() { "_exactness", "dsc(vote_average)" ], - "rankingDistinct": null, + "distinctAttribute": null, "identifier": "id", "searchableAttributes": [ "title", @@ -880,7 +880,7 @@ fn search_with_settings_synonyms() { "Iron Man" ] }, - "indexNewFields": false, + "acceptNewFields": false, }); common::update_config(&mut server, config); @@ -960,7 +960,7 @@ fn search_with_settings_ranking_rules() { "_exactness", "dsc(popularity)" ], - "rankingDistinct": null, + "distinctAttribute": null, "identifier": "id", "searchableAttributes": [ "title", @@ -987,7 +987,7 @@ fn search_with_settings_ranking_rules() { ], "stopWords": null, "synonyms": null, - "indexNewFields": false, + "acceptNewFields": false, }); common::update_config(&mut server, config); @@ -1067,7 +1067,7 @@ fn search_with_settings_searchable_attributes() { "_exactness", "dsc(vote_average)" ], - "rankingDistinct": null, + "distinctAttribute": null, "identifier": "id", "searchableAttributes": [ "tagline", @@ -1093,7 +1093,7 @@ fn search_with_settings_searchable_attributes() { ], "stopWords": null, "synonyms": null, - "indexNewFields": false, + "acceptNewFields": false, }); common::update_config(&mut server, config); @@ -1173,7 +1173,7 @@ fn search_with_settings_displayed_attributes() { "_exactness", "dsc(vote_average)" ], - "rankingDistinct": null, + "distinctAttribute": null, "identifier": "id", "searchableAttributes": [ "title", @@ -1194,7 +1194,7 @@ fn search_with_settings_displayed_attributes() { ], "stopWords": null, "synonyms": null, - "indexNewFields": false, + "acceptNewFields": false, }); common::update_config(&mut server, config); @@ -1244,7 +1244,7 @@ fn search_with_settings_searchable_attributes_2() { "_exactness", "dsc(vote_average)" ], - "rankingDistinct": null, + "distinctAttribute": null, "identifier": "id", "searchableAttributes": [ "tagline", @@ -1265,7 +1265,7 @@ fn search_with_settings_searchable_attributes_2() { ], "stopWords": null, "synonyms": null, - "indexNewFields": false, + "acceptNewFields": false, }); common::update_config(&mut server, config); diff --git a/meilisearch-http/tests/settings.rs b/meilisearch-http/tests/settings.rs index 19d4ff092..5062616f9 100644 --- a/meilisearch-http/tests/settings.rs +++ b/meilisearch-http/tests/settings.rs @@ -50,7 +50,7 @@ fn write_all_and_delete() { "dsc(release_date)", "dsc(rank)", ], - "rankingDistinct": "movie_id", + "distinctAttribute": "movie_id", "searchableAttributes": [ "id", "movie_id", @@ -76,7 +76,7 @@ fn write_all_and_delete() { "wolverine": ["xmen", "logan"], "logan": ["wolverine"], }, - "indexNewFields": false, + "acceptNewFields": false, }); let body = json.to_string().into_bytes(); @@ -127,12 +127,12 @@ fn write_all_and_delete() { let json = json!({ "rankingRules": null, - "rankingDistinct": null, + "distinctAttribute": null, "searchableAttributes": null, "displayedAttributes": null, "stopWords": null, "synonyms": null, - "indexNewFields": true, + "acceptNewFields": true, }); assert_json_eq!(json, res_value, ordered: false); @@ -178,7 +178,7 @@ fn write_all_and_update() { "dsc(release_date)", "dsc(rank)", ], - "rankingDistinct": "movie_id", + "distinctAttribute": "movie_id", "searchableAttributes": [ "uid", "movie_id", @@ -204,7 +204,7 @@ fn write_all_and_update() { "wolverine": ["xmen", "logan"], "logan": ["wolverine"], }, - "indexNewFields": false, + "acceptNewFields": false, }); let body = json.to_string().into_bytes(); @@ -261,7 +261,7 @@ fn write_all_and_update() { "wolverine": ["xmen", "logan"], "logan": ["wolverine", "xmen"], }, - "indexNewFields": false, + "acceptNewFields": false, }); let body_update = json_update.to_string().into_bytes(); @@ -296,7 +296,7 @@ fn write_all_and_update() { "_exactness", "dsc(release_date)", ], - "rankingDistinct": null, + "distinctAttribute": null, "searchableAttributes": [ "title", "description", @@ -314,7 +314,7 @@ fn write_all_and_update() { "wolverine": ["xmen", "logan"], "logan": ["wolverine", "xmen"], }, - "indexNewFields": false + "acceptNewFields": false }); assert_json_eq!(res_expected, res_value, ordered: false); From 79e07fa852ca45b16dc0a5c2aeb1a3574764b812 Mon Sep 17 00:00:00 2001 From: qdequele Date: Wed, 26 Feb 2020 17:34:26 +0100 Subject: [PATCH 4/9] reset value of searchable and displayed attributes; fix #473 --- .../src/update/settings_update.rs | 6 ++---- meilisearch-schema/src/fields_map.rs | 6 ++++++ meilisearch-schema/src/schema.rs | 19 +++++++++++++++++++ 3 files changed, 27 insertions(+), 4 deletions(-) diff --git a/meilisearch-core/src/update/settings_update.rs b/meilisearch-core/src/update/settings_update.rs index aa7d3f505..33d1cc11b 100644 --- a/meilisearch-core/src/update/settings_update.rs +++ b/meilisearch-core/src/update/settings_update.rs @@ -84,8 +84,7 @@ pub fn apply_settings_update( must_reindex = true; }, UpdateState::Clear => { - let clear: Vec<&str> = Vec::new(); - schema.update_indexed(clear)?; + schema.set_all_fields_indexed(); must_reindex = true; }, UpdateState::Nothing => (), @@ -93,8 +92,7 @@ pub fn apply_settings_update( match settings.displayed_attributes.clone() { UpdateState::Update(v) => schema.update_displayed(v)?, UpdateState::Clear => { - let clear: Vec<&str> = Vec::new(); - schema.update_displayed(clear)?; + schema.set_all_fields_displayed(); }, UpdateState::Nothing => (), } diff --git a/meilisearch-schema/src/fields_map.rs b/meilisearch-schema/src/fields_map.rs index 6243a554b..6505b33fb 100644 --- a/meilisearch-schema/src/fields_map.rs +++ b/meilisearch-schema/src/fields_map.rs @@ -1,4 +1,5 @@ use std::collections::HashMap; +use std::collections::hash_map::Iter; use serde::{Deserialize, Serialize}; @@ -45,8 +46,13 @@ impl FieldsMap { pub fn name>(&self, id: I) -> Option<&str> { self.id_map.get(&id.into()).map(|s| s.as_str()) } + + pub fn iter(&self) -> Iter<'_, String, FieldId> { + self.name_map.iter() + } } + #[cfg(test)] mod tests { use super::*; diff --git a/meilisearch-schema/src/schema.rs b/meilisearch-schema/src/schema.rs index d66693bc9..da6d615b1 100644 --- a/meilisearch-schema/src/schema.rs +++ b/meilisearch-schema/src/schema.rs @@ -190,6 +190,25 @@ impl Schema { Ok(()) } + pub fn set_all_fields_indexed(&mut self) { + self.indexed.clear(); + self.indexed_map.clear(); + + for (_name, id) in self.fields_map.iter() { + let pos = self.indexed.len() as u16; + self.indexed.push(*id); + self.indexed_map.insert(*id, pos.into()); + } + } + + pub fn set_all_fields_displayed(&mut self) { + self.displayed.clear(); + + for (_name, id) in self.fields_map.iter() { + self.displayed.insert(*id); + } + } + pub fn accept_new_fields(&self) -> bool { self.accept_new_fields } From 3c74e71d4f5abfc60c556fdf874336f7abcd44c7 Mon Sep 17 00:00:00 2001 From: qdequele Date: Wed, 26 Feb 2020 18:24:19 +0100 Subject: [PATCH 5/9] show default ranking rules if user reset them; fix #476 --- meilisearch-core/src/settings.rs | 24 ++++++++++++++---------- meilisearch-http/src/routes/setting.rs | 23 +++++++++++++---------- 2 files changed, 27 insertions(+), 20 deletions(-) diff --git a/meilisearch-core/src/settings.rs b/meilisearch-core/src/settings.rs index b97a14874..1bebdce53 100644 --- a/meilisearch-core/src/settings.rs +++ b/meilisearch-core/src/settings.rs @@ -5,6 +5,10 @@ use std::iter::IntoIterator; use serde::{Deserialize, Deserializer, Serialize}; use once_cell::sync::Lazy; +use self::RankingRule::*; + +pub const DEFAULT_RANKING_RULES: [RankingRule; 6] = [Typo, Words, Proximity, Attribute, WordsPosition, Exactness]; + static RANKING_RULE_REGEX: Lazy = Lazy::new(|| { let regex = regex::Regex::new(r"(asc|dsc)\(([a-zA-Z0-9-_]*)\)").unwrap(); regex @@ -98,17 +102,17 @@ pub enum RankingRule { Dsc(String), } -impl ToString for RankingRule { - fn to_string(&self) -> String { +impl std::fmt::Display for RankingRule { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { match self { - RankingRule::Typo => "_typo".to_string(), - RankingRule::Words => "_words".to_string(), - RankingRule::Proximity => "_proximity".to_string(), - RankingRule::Attribute => "_attribute".to_string(), - RankingRule::WordsPosition => "_words_position".to_string(), - RankingRule::Exactness => "_exactness".to_string(), - RankingRule::Asc(field) => format!("asc({})", field), - RankingRule::Dsc(field) => format!("dsc({})", field), + RankingRule::Typo => write!(f, "_typo"), + RankingRule::Words => write!(f, "_words"), + RankingRule::Proximity => write!(f, "_proximity"), + RankingRule::Attribute => write!(f, "_attribute"), + RankingRule::WordsPosition => write!(f, "_words_position"), + RankingRule::Exactness => write!(f, "_exactness"), + RankingRule::Asc(field) => write!(f, "asc({})", field), + RankingRule::Dsc(field) => write!(f, "dsc({})", field), } } } diff --git a/meilisearch-http/src/routes/setting.rs b/meilisearch-http/src/routes/setting.rs index f106a69ea..9b8c41a2f 100644 --- a/meilisearch-http/src/routes/setting.rs +++ b/meilisearch-http/src/routes/setting.rs @@ -1,4 +1,4 @@ -use meilisearch_core::settings::{Settings, SettingsUpdate, UpdateState}; +use meilisearch_core::settings::{Settings, SettingsUpdate, UpdateState, DEFAULT_RANKING_RULES}; use serde::Deserialize; use std::collections::{BTreeMap, BTreeSet, HashSet}; use tide::{Request, Response}; @@ -46,10 +46,12 @@ pub async fn get_all(ctx: Request) -> SResult { None }; - let ranking_rules = match index.main.ranking_rules(&reader)? { - Some(rules) => Some(rules.iter().map(|r| r.to_string()).collect()), - None => None, - }; + let ranking_rules = index.main.ranking_rules(&reader)? + .unwrap_or(DEFAULT_RANKING_RULES.to_vec()) + .into_iter() + .map(|r| r.to_string()) + .collect(); + let distinct_attribute = index.main.distinct_attribute(&reader)?; let schema = index.main.schema(&reader)?; @@ -80,7 +82,7 @@ pub async fn get_all(ctx: Request) -> SResult { let accept_new_fields = schema.map(|s| s.accept_new_fields()); let settings = Settings { - ranking_rules: Some(ranking_rules), + ranking_rules: Some(Some(ranking_rules)), distinct_attribute: Some(distinct_attribute), searchable_attributes, displayed_attributes, @@ -161,10 +163,11 @@ pub async fn get_rules(ctx: Request) -> SResult { let db = &ctx.state().db; let reader = db.main_read_txn()?; - let ranking_rules: Option> = match index.main.ranking_rules(&reader)? { - Some(rules) => Some(rules.iter().map(|r| r.to_string()).collect()), - None => None, - }; + let ranking_rules = index.main.ranking_rules(&reader)? + .unwrap_or(DEFAULT_RANKING_RULES.to_vec()) + .into_iter() + .map(|r| r.to_string()) + .collect::>(); Ok(tide::Response::new(200).body_json(&ranking_rules).unwrap()) } From bb5d931f160ac355e44b084d2ffc1f0b2a559edb Mon Sep 17 00:00:00 2001 From: qdequele Date: Wed, 26 Feb 2020 18:32:15 +0100 Subject: [PATCH 6/9] rename criterions on settings route; fix #480 --- meilisearch-core/src/settings.rs | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/meilisearch-core/src/settings.rs b/meilisearch-core/src/settings.rs index 1bebdce53..0a9cdbb57 100644 --- a/meilisearch-core/src/settings.rs +++ b/meilisearch-core/src/settings.rs @@ -105,12 +105,12 @@ pub enum RankingRule { impl std::fmt::Display for RankingRule { fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { match self { - RankingRule::Typo => write!(f, "_typo"), - RankingRule::Words => write!(f, "_words"), - RankingRule::Proximity => write!(f, "_proximity"), - RankingRule::Attribute => write!(f, "_attribute"), - RankingRule::WordsPosition => write!(f, "_words_position"), - RankingRule::Exactness => write!(f, "_exactness"), + RankingRule::Typo => write!(f, "typo"), + RankingRule::Words => write!(f, "words"), + RankingRule::Proximity => write!(f, "proximity"), + RankingRule::Attribute => write!(f, "attribute"), + RankingRule::WordsPosition => write!(f, "words_position"), + RankingRule::Exactness => write!(f, "exactness"), RankingRule::Asc(field) => write!(f, "asc({})", field), RankingRule::Dsc(field) => write!(f, "dsc({})", field), } @@ -122,12 +122,12 @@ impl FromStr for RankingRule { fn from_str(s: &str) -> Result { let rule = match s { - "_typo" => RankingRule::Typo, - "_words" => RankingRule::Words, - "_proximity" => RankingRule::Proximity, - "_attribute" => RankingRule::Attribute, - "_words_position" => RankingRule::WordsPosition, - "_exactness" => RankingRule::Exactness, + "typo" => RankingRule::Typo, + "words" => RankingRule::Words, + "proximity" => RankingRule::Proximity, + "attribute" => RankingRule::Attribute, + "words_position" => RankingRule::WordsPosition, + "exactness" => RankingRule::Exactness, _ => { let captures = RANKING_RULE_REGEX.captures(s).ok_or(RankingRuleConversionError)?; match (captures.get(1).map(|m| m.as_str()), captures.get(2)) { From f182afc50b7bf3644cb77b83067e677913ff6a0b Mon Sep 17 00:00:00 2001 From: qdequele Date: Wed, 26 Feb 2020 18:47:03 +0100 Subject: [PATCH 7/9] update tests --- meilisearch-core/src/database.rs | 12 +-- meilisearch-http/tests/common.rs | 12 +-- meilisearch-http/tests/search.rs | 84 +++++++++---------- meilisearch-http/tests/settings.rs | 77 +++++++++++------ .../tests/settings_ranking_rules.rs | 57 +++++++------ 5 files changed, 136 insertions(+), 106 deletions(-) diff --git a/meilisearch-core/src/database.rs b/meilisearch-core/src/database.rs index f66d292f0..8771807f8 100644 --- a/meilisearch-core/src/database.rs +++ b/meilisearch-core/src/database.rs @@ -1059,12 +1059,12 @@ mod tests { let data = r#" { "rankingRules": [ - "_typo", - "_words", - "_proximity", - "_attribute", - "_words_position", - "_exactness", + "typo", + "words", + "proximity", + "attribute", + "words_position", + "exactness", "dsc(release_date)" ], "searchableAttributes": ["name", "release_date"], diff --git a/meilisearch-http/tests/common.rs b/meilisearch-http/tests/common.rs index c30888cbe..3e7ca0608 100644 --- a/meilisearch-http/tests/common.rs +++ b/meilisearch-http/tests/common.rs @@ -57,13 +57,13 @@ pub fn enrich_server_with_movies_settings( ) -> Result<(), Box> { let json = json!({ "rankingRules": [ - "_typo", - "_words", - "_proximity", - "_attribute", - "_words_position", + "typo", + "words", + "proximity", + "attribute", + "words_position", "dsc(popularity)", - "_exactness", + "exactness", "dsc(vote_average)", ], "distinctAttribute": null, diff --git a/meilisearch-http/tests/search.rs b/meilisearch-http/tests/search.rs index 42fd901e2..20e2d9b63 100644 --- a/meilisearch-http/tests/search.rs +++ b/meilisearch-http/tests/search.rs @@ -626,13 +626,13 @@ fn search_with_settings_basic() { let config = json!({ "rankingRules": [ - "_typo", - "_words", - "_proximity", - "_attribute", - "_words_position", + "typo", + "words", + "proximity", + "attribute", + "words_position", "dsc(popularity)", - "_exactness", + "exactness", "dsc(vote_average)" ], "distinctAttribute": null, @@ -732,13 +732,13 @@ fn search_with_settings_stop_words() { let config = json!({ "rankingRules": [ - "_typo", - "_words", - "_proximity", - "_attribute", - "_words_position", + "typo", + "words", + "proximity", + "attribute", + "words_position", "dsc(popularity)", - "_exactness", + "exactness", "dsc(vote_average)" ], "distinctAttribute": null, @@ -839,13 +839,13 @@ fn search_with_settings_synonyms() { let config = json!({ "rankingRules": [ - "_typo", - "_words", - "_proximity", - "_attribute", - "_words_position", + "typo", + "words", + "proximity", + "attribute", + "words_position", "dsc(popularity)", - "_exactness", + "exactness", "dsc(vote_average)" ], "distinctAttribute": null, @@ -951,13 +951,13 @@ fn search_with_settings_ranking_rules() { let config = json!({ "rankingRules": [ - "_typo", - "_words", - "_proximity", - "_attribute", - "_words_position", + "typo", + "words", + "proximity", + "attribute", + "words_position", "asc(vote_average)", - "_exactness", + "exactness", "dsc(popularity)" ], "distinctAttribute": null, @@ -1058,13 +1058,13 @@ fn search_with_settings_searchable_attributes() { let config = json!({ "rankingRules": [ - "_typo", - "_words", - "_proximity", - "_attribute", - "_words_position", + "typo", + "words", + "proximity", + "attribute", + "words_position", "dsc(popularity)", - "_exactness", + "exactness", "dsc(vote_average)" ], "distinctAttribute": null, @@ -1164,13 +1164,13 @@ fn search_with_settings_displayed_attributes() { let config = json!({ "rankingRules": [ - "_typo", - "_words", - "_proximity", - "_attribute", - "_words_position", + "typo", + "words", + "proximity", + "attribute", + "words_position", "dsc(popularity)", - "_exactness", + "exactness", "dsc(vote_average)" ], "distinctAttribute": null, @@ -1235,13 +1235,13 @@ fn search_with_settings_searchable_attributes_2() { let config = json!({ "rankingRules": [ - "_typo", - "_words", - "_proximity", - "_attribute", - "_words_position", + "typo", + "words", + "proximity", + "attribute", + "words_position", "dsc(popularity)", - "_exactness", + "exactness", "dsc(vote_average)" ], "distinctAttribute": null, diff --git a/meilisearch-http/tests/settings.rs b/meilisearch-http/tests/settings.rs index 5062616f9..eccde51c9 100644 --- a/meilisearch-http/tests/settings.rs +++ b/meilisearch-http/tests/settings.rs @@ -41,12 +41,12 @@ fn write_all_and_delete() { let json = json!({ "rankingRules": [ - "_typo", - "_words", - "_proximity", - "_attribute", - "_words_position", - "_exactness", + "typo", + "words", + "proximity", + "attribute", + "words_position", + "exactness", "dsc(release_date)", "dsc(rank)", ], @@ -126,10 +126,33 @@ fn write_all_and_delete() { let res_value: Value = serde_json::from_slice(&buf).unwrap(); let json = json!({ - "rankingRules": null, + "rankingRules": [ + "typo", + "words", + "proximity", + "attribute", + "words_position", + "exactness" + ], "distinctAttribute": null, - "searchableAttributes": null, - "displayedAttributes": null, + "searchableAttributes": [ + "id", + "release_date", + "poster", + "description", + "title", + "movie_id", + "rank" + ], + "displayedAttributes": [ + "movie_id", + "description", + "poster", + "id", + "release_date", + "rank", + "title" + ], "stopWords": null, "synonyms": null, "acceptNewFields": true, @@ -169,12 +192,12 @@ fn write_all_and_update() { let json = json!({ "rankingRules": [ - "_typo", - "_words", - "_proximity", - "_attribute", - "_words_position", - "_exactness", + "typo", + "words", + "proximity", + "attribute", + "words_position", + "exactness", "dsc(release_date)", "dsc(rank)", ], @@ -235,12 +258,12 @@ fn write_all_and_update() { let json_update = json!({ "rankingRules": [ - "_typo", - "_words", - "_proximity", - "_attribute", - "_words_position", - "_exactness", + "typo", + "words", + "proximity", + "attribute", + "words_position", + "exactness", "dsc(release_date)", ], "searchableAttributes": [ @@ -288,12 +311,12 @@ fn write_all_and_update() { let res_expected = json!({ "rankingRules": [ - "_typo", - "_words", - "_proximity", - "_attribute", - "_words_position", - "_exactness", + "typo", + "words", + "proximity", + "attribute", + "words_position", + "exactness", "dsc(release_date)", ], "distinctAttribute": null, diff --git a/meilisearch-http/tests/settings_ranking_rules.rs b/meilisearch-http/tests/settings_ranking_rules.rs index b310df3cd..73ef8c988 100644 --- a/meilisearch-http/tests/settings_ranking_rules.rs +++ b/meilisearch-http/tests/settings_ranking_rules.rs @@ -39,12 +39,12 @@ fn write_all_and_delete() { // 2 - Send the settings let json = json!([ - "_typo", - "_words", - "_proximity", - "_attribute", - "_words_position", - "_exactness", + "typo", + "words", + "proximity", + "attribute", + "words_position", + "exactness", "dsc(release_date)", "dsc(rank)", ]); @@ -95,7 +95,14 @@ fn write_all_and_delete() { block_on(res.into_body().read_to_end(&mut buf)).unwrap(); let res_value: Value = serde_json::from_slice(&buf).unwrap(); - let json = json!(null); + let json = json!([ + "typo", + "words", + "proximity", + "attribute", + "words_position", + "exactness" + ]); assert_json_eq!(json, res_value, ordered: false); } @@ -130,12 +137,12 @@ fn write_all_and_update() { // 2 - Send the settings let json = json!([ - "_typo", - "_words", - "_proximity", - "_attribute", - "_words_position", - "_exactness", + "typo", + "words", + "proximity", + "attribute", + "words_position", + "exactness", "dsc(release_date)", "dsc(rank)", ]); @@ -167,12 +174,12 @@ fn write_all_and_update() { // 4 - Update all settings let json_update = json!([ - "_typo", - "_words", - "_proximity", - "_attribute", - "_words_position", - "_exactness", + "typo", + "words", + "proximity", + "attribute", + "words_position", + "exactness", "dsc(release_date)", ]); @@ -199,12 +206,12 @@ fn write_all_and_update() { let res_value: Value = serde_json::from_slice(&buf).unwrap(); let res_expected = json!([ - "_typo", - "_words", - "_proximity", - "_attribute", - "_words_position", - "_exactness", + "typo", + "words", + "proximity", + "attribute", + "words_position", + "exactness", "dsc(release_date)", ]); From dda08d60d25897ae5ee8a2d56c8c805997172ff7 Mon Sep 17 00:00:00 2001 From: qdequele Date: Wed, 26 Feb 2020 18:49:17 +0100 Subject: [PATCH 8/9] cargo fmt --- meilisearch-core/src/settings.rs | 12 +++++----- .../src/update/settings_update.rs | 4 ++-- meilisearch-http/src/data.rs | 6 ++--- meilisearch-http/src/helpers/tide.rs | 22 ++++++++++-------- meilisearch-http/src/main.rs | 21 +++++++++++------ meilisearch-http/src/routes/document.rs | 2 +- meilisearch-http/src/routes/index.rs | 18 +++++++++------ meilisearch-http/src/routes/key.rs | 13 +++++------ meilisearch-http/src/routes/mod.rs | 23 ++++++++----------- meilisearch-http/src/routes/search.rs | 2 +- meilisearch-http/src/routes/setting.rs | 14 +++++++---- meilisearch-http/tests/common.rs | 2 +- meilisearch-schema/src/fields_map.rs | 1 - meilisearch-schema/src/schema.rs | 4 ++-- 14 files changed, 77 insertions(+), 67 deletions(-) diff --git a/meilisearch-core/src/settings.rs b/meilisearch-core/src/settings.rs index 0a9cdbb57..5e230cdba 100644 --- a/meilisearch-core/src/settings.rs +++ b/meilisearch-core/src/settings.rs @@ -105,12 +105,12 @@ pub enum RankingRule { impl std::fmt::Display for RankingRule { fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { match self { - RankingRule::Typo => write!(f, "typo"), - RankingRule::Words => write!(f, "words"), - RankingRule::Proximity => write!(f, "proximity"), - RankingRule::Attribute => write!(f, "attribute"), - RankingRule::WordsPosition => write!(f, "words_position"), - RankingRule::Exactness => write!(f, "exactness"), + RankingRule::Typo => f.write_str("typo"), + RankingRule::Words => f.write_str("words"), + RankingRule::Proximity => f.write_str("proximity"), + RankingRule::Attribute => f.write_str("attribute"), + RankingRule::WordsPosition => f.write_str("wordsPosition"), + RankingRule::Exactness => f.write_str("exactness"), RankingRule::Asc(field) => write!(f, "asc({})", field), RankingRule::Dsc(field) => write!(f, "dsc({})", field), } diff --git a/meilisearch-core/src/update/settings_update.rs b/meilisearch-core/src/update/settings_update.rs index 33d1cc11b..d0f10eb80 100644 --- a/meilisearch-core/src/update/settings_update.rs +++ b/meilisearch-core/src/update/settings_update.rs @@ -84,7 +84,7 @@ pub fn apply_settings_update( must_reindex = true; }, UpdateState::Clear => { - schema.set_all_fields_indexed(); + schema.set_all_fields_as_indexed(); must_reindex = true; }, UpdateState::Nothing => (), @@ -92,7 +92,7 @@ pub fn apply_settings_update( match settings.displayed_attributes.clone() { UpdateState::Update(v) => schema.update_displayed(v)?, UpdateState::Clear => { - schema.set_all_fields_displayed(); + schema.set_all_fields_as_displayed(); }, UpdateState::Nothing => (), } diff --git a/meilisearch-http/src/data.rs b/meilisearch-http/src/data.rs index 3cd6a514b..5d05c02dd 100644 --- a/meilisearch-http/src/data.rs +++ b/meilisearch-http/src/data.rs @@ -6,8 +6,8 @@ use chrono::{DateTime, Utc}; use heed::types::{SerdeBincode, Str}; use log::error; use meilisearch_core::{Database, Error as MError, MResult, MainT, UpdateT}; -use sysinfo::Pid; use sha2::Digest; +use sysinfo::Pid; use crate::option::Opt; use crate::routes::index::index_update_callback; @@ -117,9 +117,7 @@ impl DataInner { // convert attributes to their names let frequency: HashMap<_, _> = fields_frequency .into_iter() - .filter_map(|(a, c)| { - schema.name(a).map(|name| (name.to_string(), c)) - }) + .filter_map(|(a, c)| schema.name(a).map(|name| (name.to_string(), c))) .collect(); index diff --git a/meilisearch-http/src/helpers/tide.rs b/meilisearch-http/src/helpers/tide.rs index 96003ce91..ce3bbe82a 100644 --- a/meilisearch-http/src/helpers/tide.rs +++ b/meilisearch-http/src/helpers/tide.rs @@ -6,7 +6,7 @@ use tide::Request; pub enum ACL { Admin, Private, - Public + Public, } pub trait RequestExt { @@ -23,31 +23,33 @@ impl RequestExt for Request { match acl { ACL::Admin => { if user_api_key == self.state().api_keys.master.as_deref() { - return Ok(()) + return Ok(()); } - }, + } ACL::Private => { if user_api_key == self.state().api_keys.master.as_deref() { - return Ok(()) + return Ok(()); } if user_api_key == self.state().api_keys.private.as_deref() { - return Ok(()) + return Ok(()); } - }, + } ACL::Public => { if user_api_key == self.state().api_keys.master.as_deref() { - return Ok(()) + return Ok(()); } if user_api_key == self.state().api_keys.private.as_deref() { - return Ok(()) + return Ok(()); } if user_api_key == self.state().api_keys.public.as_deref() { - return Ok(()) + return Ok(()); } } } - Err(ResponseError::InvalidToken(user_api_key.unwrap_or("Need a token").to_owned())) + Err(ResponseError::InvalidToken( + user_api_key.unwrap_or("Need a token").to_owned(), + )) } fn url_param(&self, name: &str) -> SResult { diff --git a/meilisearch-http/src/main.rs b/meilisearch-http/src/main.rs index 7d24e53a0..124bb9c47 100644 --- a/meilisearch-http/src/main.rs +++ b/meilisearch-http/src/main.rs @@ -18,19 +18,21 @@ mod analytics; static ALLOC: jemallocator::Jemalloc = jemallocator::Jemalloc; pub fn main() -> Result<(), MainError> { - let opt = Opt::from_args(); match opt.env.as_ref() { "production" => { if opt.master_key.is_none() { - return Err("In production mode, the environment variable MEILI_MASTER_KEY is mandatory".into()); + return Err( + "In production mode, the environment variable MEILI_MASTER_KEY is mandatory" + .into(), + ); } env_logger::init(); - }, + } "development" => { env_logger::from_env(env_logger::Env::default().default_filter_or("info")).init(); - }, + } _ => unreachable!(), } @@ -58,7 +60,6 @@ pub fn main() -> Result<(), MainError> { Ok(()) } - pub fn print_launch_resume(opt: &Opt, data: &Data) { let ascii_name = r#" 888b d888 d8b 888 d8b .d8888b. 888 @@ -77,8 +78,14 @@ pub fn print_launch_resume(opt: &Opt, data: &Data) { info!("Start server on: {:?}", opt.http_addr); info!("Environment: {:?}", opt.env); info!("Commit SHA: {:?}", env!("VERGEN_SHA").to_string()); - info!("Build date: {:?}", env!("VERGEN_BUILD_TIMESTAMP").to_string()); - info!("Package version: {:?}", env!("CARGO_PKG_VERSION").to_string()); + info!( + "Build date: {:?}", + env!("VERGEN_BUILD_TIMESTAMP").to_string() + ); + info!( + "Package version: {:?}", + env!("CARGO_PKG_VERSION").to_string() + ); if let Some(master_key) = &data.api_keys.master { info!("Master Key: {:?}", master_key); diff --git a/meilisearch-http/src/routes/document.rs b/meilisearch-http/src/routes/document.rs index 1e70db296..e0ab4e88d 100644 --- a/meilisearch-http/src/routes/document.rs +++ b/meilisearch-http/src/routes/document.rs @@ -145,7 +145,7 @@ async fn update_multiple_documents(mut ctx: Request, is_partial: bool) -> None => return Err(ResponseError::bad_request("Could not infer a schema")), }, }; - let settings_update = SettingsUpdate{ + let settings_update = SettingsUpdate { identifier: UpdateState::Update(id), ..SettingsUpdate::default() }; diff --git a/meilisearch-http/src/routes/index.rs b/meilisearch-http/src/routes/index.rs index eb12145d0..6625d24c3 100644 --- a/meilisearch-http/src/routes/index.rs +++ b/meilisearch-http/src/routes/index.rs @@ -42,7 +42,7 @@ pub async fn list_indexes(ctx: Request) -> SResult { let identifier = match index.main.schema(&reader) { Ok(Some(schema)) => Some(schema.identifier().to_owned()), - _ => None + _ => None, }; let index_response = IndexResponse { @@ -89,7 +89,7 @@ pub async fn get_index(ctx: Request) -> SResult { let identifier = match index.main.schema(&reader) { Ok(Some(schema)) => Some(schema.identifier().to_owned()), - _ => None + _ => None, }; let response_body = IndexResponse { @@ -97,7 +97,7 @@ pub async fn get_index(ctx: Request) -> SResult { uid, created_at, updated_at, - identifier + identifier, }; Ok(tide::Response::new(200).body_json(&response_body)?) @@ -220,9 +220,13 @@ pub async fn update_index(mut ctx: Request) -> SResult { if let Some(identifier) = body.identifier { if let Ok(Some(_)) = index.main.schema(&writer) { - return Err(ResponseError::bad_request("The index identifier cannot be updated")); + return Err(ResponseError::bad_request( + "The index identifier cannot be updated", + )); } - index.main.put_schema(&mut writer, &Schema::with_identifier(&identifier))?; + index + .main + .put_schema(&mut writer, &Schema::with_identifier(&identifier))?; } index.main.put_updated_at(&mut writer)?; @@ -235,7 +239,7 @@ pub async fn update_index(mut ctx: Request) -> SResult { let identifier = match index.main.schema(&reader) { Ok(Some(schema)) => Some(schema.identifier().to_owned()), - _ => None + _ => None, }; let response_body = UpdateIndexResponse { @@ -243,7 +247,7 @@ pub async fn update_index(mut ctx: Request) -> SResult { uid: index_uid, created_at, updated_at, - identifier + identifier, }; Ok(tide::Response::new(200).body_json(&response_body)?) diff --git a/meilisearch-http/src/routes/key.rs b/meilisearch-http/src/routes/key.rs index 6ab85645b..fe0feacf4 100644 --- a/meilisearch-http/src/routes/key.rs +++ b/meilisearch-http/src/routes/key.rs @@ -1,18 +1,17 @@ -use tide::{Request, Response}; -use serde_json::json; use crate::error::SResult; use crate::helpers::tide::RequestExt; use crate::helpers::tide::ACL::*; use crate::Data; +use serde_json::json; +use tide::{Request, Response}; pub async fn list(ctx: Request) -> SResult { ctx.is_allowed(Admin)?; let keys = &ctx.state().api_keys; - Ok(tide::Response::new(200) - .body_json(&json!({ - "private": keys.private, - "public": keys.public, - }))?) + Ok(tide::Response::new(200).body_json(&json!({ + "private": keys.private, + "public": keys.public, + }))?) } diff --git a/meilisearch-http/src/routes/mod.rs b/meilisearch-http/src/routes/mod.rs index 5caa765f5..e9188100a 100644 --- a/meilisearch-http/src/routes/mod.rs +++ b/meilisearch-http/src/routes/mod.rs @@ -23,19 +23,15 @@ async fn into_response( } pub fn load_routes(app: &mut tide::Server) { - app.at("/").get(|_| { - async move { - tide::Response::new(200) - .body_string(include_str!("../../public/interface.html").to_string()) - .set_mime(mime::TEXT_HTML_UTF_8) - } + app.at("/").get(|_| async { + tide::Response::new(200) + .body_string(include_str!("../../public/interface.html").to_string()) + .set_mime(mime::TEXT_HTML_UTF_8) }); - app.at("/bulma.min.css").get(|_| { - async { - tide::Response::new(200) - .body_string(include_str!("../../public/bulma.min.css").to_string()) - .set_mime(mime::TEXT_CSS_UTF_8) - } + app.at("/bulma.min.css").get(|_| async { + tide::Response::new(200) + .body_string(include_str!("../../public/bulma.min.css").to_string()) + .set_mime(mime::TEXT_CSS_UTF_8) }); app.at("/indexes") @@ -117,8 +113,7 @@ pub fn load_routes(app: &mut tide::Server) { app.at("/indexes/:index/stats") .get(|ctx| into_response(stats::index_stats(ctx))); - app.at("/keys/") - .get(|ctx| into_response(key::list(ctx))); + app.at("/keys/").get(|ctx| into_response(key::list(ctx))); app.at("/health") .get(|ctx| into_response(health::get_health(ctx))) diff --git a/meilisearch-http/src/routes/search.rs b/meilisearch-http/src/routes/search.rs index bfb8372aa..2dffd9356 100644 --- a/meilisearch-http/src/routes/search.rs +++ b/meilisearch-http/src/routes/search.rs @@ -7,10 +7,10 @@ use rayon::iter::{IntoParallelIterator, ParallelIterator}; use serde::{Deserialize, Serialize}; use tide::{Request, Response}; -use crate::helpers::tide::ACL::*; use crate::error::{ResponseError, SResult}; use crate::helpers::meilisearch::{Error, IndexSearchExt, SearchHit}; use crate::helpers::tide::RequestExt; +use crate::helpers::tide::ACL::*; use crate::Data; #[derive(Deserialize)] diff --git a/meilisearch-http/src/routes/setting.rs b/meilisearch-http/src/routes/setting.rs index 9b8c41a2f..b0b5ce8ea 100644 --- a/meilisearch-http/src/routes/setting.rs +++ b/meilisearch-http/src/routes/setting.rs @@ -46,7 +46,9 @@ pub async fn get_all(ctx: Request) -> SResult { None }; - let ranking_rules = index.main.ranking_rules(&reader)? + let ranking_rules = index + .main + .ranking_rules(&reader)? .unwrap_or(DEFAULT_RANKING_RULES.to_vec()) .into_iter() .map(|r| r.to_string()) @@ -57,7 +59,8 @@ pub async fn get_all(ctx: Request) -> SResult { let schema = index.main.schema(&reader)?; let searchable_attributes = schema.clone().map(|s| { - let attrs = s.indexed_name() + let attrs = s + .indexed_name() .iter() .map(|s| (*s).to_string()) .collect::>(); @@ -69,7 +72,8 @@ pub async fn get_all(ctx: Request) -> SResult { }); let displayed_attributes = schema.clone().map(|s| { - let attrs = s.displayed_name() + let attrs = s + .displayed_name() .iter() .map(|s| (*s).to_string()) .collect::>(); @@ -163,7 +167,9 @@ pub async fn get_rules(ctx: Request) -> SResult { let db = &ctx.state().db; let reader = db.main_read_txn()?; - let ranking_rules = index.main.ranking_rules(&reader)? + let ranking_rules = index + .main + .ranking_rules(&reader)? .unwrap_or(DEFAULT_RANKING_RULES.to_vec()) .into_iter() .map(|r| r.to_string()) diff --git a/meilisearch-http/tests/common.rs b/meilisearch-http/tests/common.rs index 3e7ca0608..2ab31ab15 100644 --- a/meilisearch-http/tests/common.rs +++ b/meilisearch-http/tests/common.rs @@ -179,7 +179,7 @@ pub fn wait_update_id(server: &mut TestBackend>, update_id: u64) { let response: Value = serde_json::from_slice(&buf).unwrap(); if response["status"] == "processed" { - return + return; } block_on(sleep(Duration::from_secs(1))); } diff --git a/meilisearch-schema/src/fields_map.rs b/meilisearch-schema/src/fields_map.rs index 6505b33fb..76d98f4da 100644 --- a/meilisearch-schema/src/fields_map.rs +++ b/meilisearch-schema/src/fields_map.rs @@ -52,7 +52,6 @@ impl FieldsMap { } } - #[cfg(test)] mod tests { use super::*; diff --git a/meilisearch-schema/src/schema.rs b/meilisearch-schema/src/schema.rs index da6d615b1..7a4180cb5 100644 --- a/meilisearch-schema/src/schema.rs +++ b/meilisearch-schema/src/schema.rs @@ -190,7 +190,7 @@ impl Schema { Ok(()) } - pub fn set_all_fields_indexed(&mut self) { + pub fn set_all_fields_as_indexed(&mut self) { self.indexed.clear(); self.indexed_map.clear(); @@ -201,7 +201,7 @@ impl Schema { } } - pub fn set_all_fields_displayed(&mut self) { + pub fn set_all_fields_as_displayed(&mut self) { self.displayed.clear(); for (_name, id) in self.fields_map.iter() { From 47009615ee0ddbf6fe9d3b9903b1470ff81861c2 Mon Sep 17 00:00:00 2001 From: qdequele Date: Thu, 27 Feb 2020 14:31:08 +0100 Subject: [PATCH 9/9] rename words_position to wordsPosition; fix #483 --- meilisearch-core/src/database.rs | 2 +- meilisearch-core/src/settings.rs | 2 +- meilisearch-http/tests/common.rs | 2 +- meilisearch-http/tests/search.rs | 14 +++++++------- meilisearch-http/tests/settings.rs | 10 +++++----- meilisearch-http/tests/settings_ranking_rules.rs | 10 +++++----- 6 files changed, 20 insertions(+), 20 deletions(-) diff --git a/meilisearch-core/src/database.rs b/meilisearch-core/src/database.rs index 8771807f8..3326eb9cd 100644 --- a/meilisearch-core/src/database.rs +++ b/meilisearch-core/src/database.rs @@ -1063,7 +1063,7 @@ mod tests { "words", "proximity", "attribute", - "words_position", + "wordsPosition", "exactness", "dsc(release_date)" ], diff --git a/meilisearch-core/src/settings.rs b/meilisearch-core/src/settings.rs index 5e230cdba..11344a833 100644 --- a/meilisearch-core/src/settings.rs +++ b/meilisearch-core/src/settings.rs @@ -126,7 +126,7 @@ impl FromStr for RankingRule { "words" => RankingRule::Words, "proximity" => RankingRule::Proximity, "attribute" => RankingRule::Attribute, - "words_position" => RankingRule::WordsPosition, + "wordsPosition" => RankingRule::WordsPosition, "exactness" => RankingRule::Exactness, _ => { let captures = RANKING_RULE_REGEX.captures(s).ok_or(RankingRuleConversionError)?; diff --git a/meilisearch-http/tests/common.rs b/meilisearch-http/tests/common.rs index 2ab31ab15..605caa833 100644 --- a/meilisearch-http/tests/common.rs +++ b/meilisearch-http/tests/common.rs @@ -61,7 +61,7 @@ pub fn enrich_server_with_movies_settings( "words", "proximity", "attribute", - "words_position", + "wordsPosition", "dsc(popularity)", "exactness", "dsc(vote_average)", diff --git a/meilisearch-http/tests/search.rs b/meilisearch-http/tests/search.rs index 20e2d9b63..b03ff4a0d 100644 --- a/meilisearch-http/tests/search.rs +++ b/meilisearch-http/tests/search.rs @@ -630,7 +630,7 @@ fn search_with_settings_basic() { "words", "proximity", "attribute", - "words_position", + "wordsPosition", "dsc(popularity)", "exactness", "dsc(vote_average)" @@ -736,7 +736,7 @@ fn search_with_settings_stop_words() { "words", "proximity", "attribute", - "words_position", + "wordsPosition", "dsc(popularity)", "exactness", "dsc(vote_average)" @@ -843,7 +843,7 @@ fn search_with_settings_synonyms() { "words", "proximity", "attribute", - "words_position", + "wordsPosition", "dsc(popularity)", "exactness", "dsc(vote_average)" @@ -955,7 +955,7 @@ fn search_with_settings_ranking_rules() { "words", "proximity", "attribute", - "words_position", + "wordsPosition", "asc(vote_average)", "exactness", "dsc(popularity)" @@ -1062,7 +1062,7 @@ fn search_with_settings_searchable_attributes() { "words", "proximity", "attribute", - "words_position", + "wordsPosition", "dsc(popularity)", "exactness", "dsc(vote_average)" @@ -1168,7 +1168,7 @@ fn search_with_settings_displayed_attributes() { "words", "proximity", "attribute", - "words_position", + "wordsPosition", "dsc(popularity)", "exactness", "dsc(vote_average)" @@ -1239,7 +1239,7 @@ fn search_with_settings_searchable_attributes_2() { "words", "proximity", "attribute", - "words_position", + "wordsPosition", "dsc(popularity)", "exactness", "dsc(vote_average)" diff --git a/meilisearch-http/tests/settings.rs b/meilisearch-http/tests/settings.rs index eccde51c9..09a8c96bd 100644 --- a/meilisearch-http/tests/settings.rs +++ b/meilisearch-http/tests/settings.rs @@ -45,7 +45,7 @@ fn write_all_and_delete() { "words", "proximity", "attribute", - "words_position", + "wordsPosition", "exactness", "dsc(release_date)", "dsc(rank)", @@ -131,7 +131,7 @@ fn write_all_and_delete() { "words", "proximity", "attribute", - "words_position", + "wordsPosition", "exactness" ], "distinctAttribute": null, @@ -196,7 +196,7 @@ fn write_all_and_update() { "words", "proximity", "attribute", - "words_position", + "wordsPosition", "exactness", "dsc(release_date)", "dsc(rank)", @@ -262,7 +262,7 @@ fn write_all_and_update() { "words", "proximity", "attribute", - "words_position", + "wordsPosition", "exactness", "dsc(release_date)", ], @@ -315,7 +315,7 @@ fn write_all_and_update() { "words", "proximity", "attribute", - "words_position", + "wordsPosition", "exactness", "dsc(release_date)", ], diff --git a/meilisearch-http/tests/settings_ranking_rules.rs b/meilisearch-http/tests/settings_ranking_rules.rs index 73ef8c988..f221aacfb 100644 --- a/meilisearch-http/tests/settings_ranking_rules.rs +++ b/meilisearch-http/tests/settings_ranking_rules.rs @@ -43,7 +43,7 @@ fn write_all_and_delete() { "words", "proximity", "attribute", - "words_position", + "wordsPosition", "exactness", "dsc(release_date)", "dsc(rank)", @@ -100,7 +100,7 @@ fn write_all_and_delete() { "words", "proximity", "attribute", - "words_position", + "wordsPosition", "exactness" ]); @@ -141,7 +141,7 @@ fn write_all_and_update() { "words", "proximity", "attribute", - "words_position", + "wordsPosition", "exactness", "dsc(release_date)", "dsc(rank)", @@ -178,7 +178,7 @@ fn write_all_and_update() { "words", "proximity", "attribute", - "words_position", + "wordsPosition", "exactness", "dsc(release_date)", ]); @@ -210,7 +210,7 @@ fn write_all_and_update() { "words", "proximity", "attribute", - "words_position", + "wordsPosition", "exactness", "dsc(release_date)", ]);