default values of synonyms and stop-words; fix #499 fix #504

This commit is contained in:
qdequele 2020-03-05 14:17:15 +01:00 committed by Quentin de Quelen
parent c5b6e641a4
commit d56968cb23
3 changed files with 44 additions and 17 deletions

View File

@ -105,7 +105,7 @@ pub fn load_routes(app: &mut tide::Server<Data>) {
.post(|ctx| into_response(synonym::update(ctx))) .post(|ctx| into_response(synonym::update(ctx)))
.delete(|ctx| into_response(synonym::delete(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))) .get(|ctx| into_response(stop_words::get(ctx)))
.post(|ctx| into_response(stop_words::update(ctx))) .post(|ctx| into_response(stop_words::update(ctx)))
.delete(|ctx| into_response(stop_words::delete(ctx))); .delete(|ctx| into_response(stop_words::delete(ctx)));

View File

@ -18,34 +18,20 @@ pub async fn get_all(ctx: Request<Data>) -> SResult<Response> {
let stop_words_fst = index.main.stop_words_fst(&reader)?; 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 = stop_words_fst.unwrap_or_default().stream().into_strs()?;
let stop_words: BTreeSet<String> = stop_words.into_iter().collect(); let stop_words: BTreeSet<String> = 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_fst = index.main.synonyms_fst(&reader)?.unwrap_or_default();
let synonyms_list = synonyms_fst.stream().into_strs()?; let synonyms_list = synonyms_fst.stream().into_strs()?;
let mut synonyms = BTreeMap::new(); let mut synonyms = BTreeMap::new();
let index_synonyms = &index.synonyms; let index_synonyms = &index.synonyms;
for synonym in synonyms_list { for synonym in synonyms_list {
let alternative_list = index_synonyms.synonyms(&reader, synonym.as_bytes())?; let alternative_list = index_synonyms.synonyms(&reader, synonym.as_bytes())?;
if let Some(list) = alternative_list { if let Some(list) = alternative_list {
let list = list.stream().into_strs()?; let list = list.stream().into_strs()?;
synonyms.insert(synonym, list); synonyms.insert(synonym, list);
} }
} }
let synonyms = if !synonyms.is_empty() {
Some(synonyms)
} else {
None
};
let ranking_rules = index let ranking_rules = index
.main .main
.ranking_rules(&reader)? .ranking_rules(&reader)?
@ -90,8 +76,8 @@ pub async fn get_all(ctx: Request<Data>) -> SResult<Response> {
distinct_attribute: Some(distinct_attribute), distinct_attribute: Some(distinct_attribute),
searchable_attributes, searchable_attributes,
displayed_attributes, displayed_attributes,
stop_words: Some(stop_words), stop_words: Some(Some(stop_words)),
synonyms: Some(synonyms), synonyms: Some(Some(synonyms)),
accept_new_fields: Some(accept_new_fields), accept_new_fields: Some(accept_new_fields),
}; };

View File

@ -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);
}