From 036977bfe446bbc0cf93a728dad2cf76d39d2289 Mon Sep 17 00:00:00 2001 From: Quentin de Quelen Date: Sat, 18 Jan 2020 19:30:25 +0100 Subject: [PATCH] add the possibility to totally clear the schema --- meilisearch-core/src/store/main.rs | 16 ++++----- .../src/update/settings_update.rs | 17 +++++++--- meilisearch-http/tests/settings.rs | 34 +++++++++++++++++-- 3 files changed, 52 insertions(+), 15 deletions(-) diff --git a/meilisearch-core/src/store/main.rs b/meilisearch-core/src/store/main.rs index 131753d6b..573686bbc 100644 --- a/meilisearch-core/src/store/main.rs +++ b/meilisearch-core/src/store/main.rs @@ -96,23 +96,23 @@ impl Main { } pub fn put_schema(self, writer: &mut heed::RwTxn, schema: &Schema) -> ZResult<()> { - self.main - .put::<_, Str, SerdeBincode>(writer, SCHEMA_KEY, schema) + self.main.put::<_, Str, SerdeBincode>(writer, SCHEMA_KEY, schema) } pub fn schema(self, reader: &heed::RoTxn) -> ZResult> { - self.main - .get::<_, Str, SerdeBincode>(reader, SCHEMA_KEY) + self.main.get::<_, Str, SerdeBincode>(reader, SCHEMA_KEY) + } + + pub fn delete_schema(self, writer: &mut heed::RwTxn) -> ZResult { + self.main.delete::<_, Str>(writer, SCHEMA_KEY) } pub fn put_ranked_map(self, writer: &mut heed::RwTxn, ranked_map: &RankedMap) -> ZResult<()> { - self.main - .put::<_, Str, SerdeBincode>(writer, RANKED_MAP_KEY, &ranked_map) + self.main.put::<_, Str, SerdeBincode>(writer, RANKED_MAP_KEY, &ranked_map) } pub fn ranked_map(self, reader: &heed::RoTxn) -> ZResult> { - self.main - .get::<_, Str, SerdeBincode>(reader, RANKED_MAP_KEY) + self.main.get::<_, Str, SerdeBincode>(reader, RANKED_MAP_KEY) } pub fn put_synonyms_fst(self, writer: &mut heed::RwTxn, fst: &fst::Set) -> ZResult<()> { diff --git a/meilisearch-core/src/update/settings_update.rs b/meilisearch-core/src/update/settings_update.rs index 499bdca35..087ac36ed 100644 --- a/meilisearch-core/src/update/settings_update.rs +++ b/meilisearch-core/src/update/settings_update.rs @@ -63,10 +63,6 @@ pub fn apply_settings_update( _ => (), } - if let UpdateState::Update(id) = settings.attribute_identifier { - schema.set_identifier(id)?; - }; - match settings.attributes_searchable.clone() { UpdateState::Update(v) => schema.update_indexed(v)?, UpdateState::Clear => { @@ -122,7 +118,18 @@ pub fn apply_settings_update( } }; - index.main.put_schema(writer, &schema)?; + match settings.attribute_identifier.clone() { + UpdateState::Update(v) => { + schema.set_identifier(v)?; + index.main.put_schema(writer, &schema)?; + }, + UpdateState::Clear => { + index.main.delete_schema(writer)?; + }, + _ => { + index.main.put_schema(writer, &schema)?; + }, + }; match settings.stop_words { UpdateState::Update(stop_words) => { diff --git a/meilisearch-http/tests/settings.rs b/meilisearch-http/tests/settings.rs index eab43741f..7646be40d 100644 --- a/meilisearch-http/tests/settings.rs +++ b/meilisearch-http/tests/settings.rs @@ -78,6 +78,8 @@ fn write_all_and_retreive() { block_on(sleep(Duration::from_secs(1))); + // 3 - Get all settings and compare to the previous one + let req = http::Request::get("/indexes/movies/settings").body(Body::empty()).unwrap(); let res = server.simulate(req).unwrap(); assert_eq!(res.status(), 200); @@ -86,8 +88,36 @@ fn write_all_and_retreive() { block_on(res.into_body().read_to_end(&mut buf)).unwrap(); let res_value: Value = serde_json::from_slice(&buf).unwrap(); - println!("json1: {:?}", json); - println!("json2: {:?}", res_value); + assert_json_eq!(json, res_value, ordered: false); + + // 4 - Delete all settings + + let req = http::Request::delete("/indexes/movies/settings").body(Body::empty()).unwrap(); + let res = server.simulate(req).unwrap(); + assert_eq!(res.status(), 202); + + block_on(sleep(Duration::from_secs(1))); + + // 5 - Get all settings and check if they are empty + + let req = http::Request::get("/indexes/movies/settings").body(Body::empty()).unwrap(); + let res = server.simulate(req).unwrap(); + assert_eq!(res.status(), 200); + + let mut buf = Vec::new(); + block_on(res.into_body().read_to_end(&mut buf)).unwrap(); + let res_value: Value = serde_json::from_slice(&buf).unwrap(); + + let json = json!({ + "ranking_rules": null, + "ranking_distinct": null, + "attribute_identifier": null, + "attributes_searchable": null, + "attributes_displayed": null, + "attributes_ranked": null, + "stop_words": null, + "synonyms": null, + }); assert_json_eq!(json, res_value, ordered: false); }