From d56968cb234cc234eb8a80fb56e3cfe19f4bc381 Mon Sep 17 00:00:00 2001 From: qdequele Date: Thu, 5 Mar 2020 14:17:15 +0100 Subject: [PATCH] default values of synonyms and stop-words; fix #499 fix #504 --- meilisearch-http/src/routes/mod.rs | 2 +- meilisearch-http/src/routes/setting.rs | 18 +------- meilisearch-http/tests/settings_stop_words.rs | 41 +++++++++++++++++++ 3 files changed, 44 insertions(+), 17 deletions(-) create mode 100644 meilisearch-http/tests/settings_stop_words.rs diff --git a/meilisearch-http/src/routes/mod.rs b/meilisearch-http/src/routes/mod.rs index 6fa5ec1e6..31be1cacf 100644 --- a/meilisearch-http/src/routes/mod.rs +++ b/meilisearch-http/src/routes/mod.rs @@ -105,7 +105,7 @@ pub fn load_routes(app: &mut tide::Server) { .post(|ctx| into_response(synonym::update(ctx))) .delete(|ctx| into_response(synonym::delete(ctx))); - app.at("/indexes/:index/settings/stop_words") + app.at("/indexes/:index/settings/stop-words") .get(|ctx| into_response(stop_words::get(ctx))) .post(|ctx| into_response(stop_words::update(ctx))) .delete(|ctx| into_response(stop_words::delete(ctx))); diff --git a/meilisearch-http/src/routes/setting.rs b/meilisearch-http/src/routes/setting.rs index 19f26ec4c..81cd12a72 100644 --- a/meilisearch-http/src/routes/setting.rs +++ b/meilisearch-http/src/routes/setting.rs @@ -18,34 +18,20 @@ pub async fn get_all(ctx: Request) -> SResult { let stop_words_fst = index.main.stop_words_fst(&reader)?; let stop_words = stop_words_fst.unwrap_or_default().stream().into_strs()?; let stop_words: BTreeSet = stop_words.into_iter().collect(); - let stop_words = if !stop_words.is_empty() { - Some(stop_words) - } else { - None - }; let synonyms_fst = index.main.synonyms_fst(&reader)?.unwrap_or_default(); let synonyms_list = synonyms_fst.stream().into_strs()?; let mut synonyms = BTreeMap::new(); - let index_synonyms = &index.synonyms; - for synonym in synonyms_list { let alternative_list = index_synonyms.synonyms(&reader, synonym.as_bytes())?; - if let Some(list) = alternative_list { let list = list.stream().into_strs()?; synonyms.insert(synonym, list); } } - let synonyms = if !synonyms.is_empty() { - Some(synonyms) - } else { - None - }; - let ranking_rules = index .main .ranking_rules(&reader)? @@ -90,8 +76,8 @@ pub async fn get_all(ctx: Request) -> SResult { distinct_attribute: Some(distinct_attribute), searchable_attributes, displayed_attributes, - stop_words: Some(stop_words), - synonyms: Some(synonyms), + stop_words: Some(Some(stop_words)), + synonyms: Some(Some(synonyms)), accept_new_fields: Some(accept_new_fields), }; diff --git a/meilisearch-http/tests/settings_stop_words.rs b/meilisearch-http/tests/settings_stop_words.rs new file mode 100644 index 000000000..195a2d69e --- /dev/null +++ b/meilisearch-http/tests/settings_stop_words.rs @@ -0,0 +1,41 @@ +use assert_json_diff::assert_json_eq; +use serde_json::json; + +mod common; + +#[test] +fn update_stop_words() { + let mut server = common::Server::with_uid("movies"); + let body = json!({ + "uid": "movies", + "identifier": "id", + }); + server.create_index(body); + + // 1 - Get stop words + + let (response, _status_code) = server.get_stop_words(); + assert_eq!(response.as_array().unwrap().is_empty(), true); + + // 2 - Update stop words + + let body = json!([ + "the", + "a" + ]); + server.update_stop_words(body.clone()); + + // 3 - Get all stop words and compare to the previous one + + let (response, _status_code) = server.get_stop_words(); + assert_json_eq!(body, response, ordered: false); + + // 4 - Delete all stop words + + server.delete_stop_words(); + + // 5 - Get all stop words and check if they are empty + + let (response, _status_code) = server.get_stop_words(); + assert_eq!(response.as_array().unwrap().is_empty(), true); +}