From 7be376721c0950205fe8bd45a81fd042272fe226 Mon Sep 17 00:00:00 2001 From: qdequele Date: Wed, 11 Mar 2020 14:42:58 +0100 Subject: [PATCH] global settings update make partial update; fix #516 --- meilisearch-http/src/routes/setting.rs | 26 +----- meilisearch-http/tests/search.rs | 7 -- meilisearch-http/tests/settings.rs | 112 ++++++++++++++++++++++++- 3 files changed, 111 insertions(+), 34 deletions(-) diff --git a/meilisearch-http/src/routes/setting.rs b/meilisearch-http/src/routes/setting.rs index 703607c32..9cdbcb7a3 100644 --- a/meilisearch-http/src/routes/setting.rs +++ b/meilisearch-http/src/routes/setting.rs @@ -1,5 +1,4 @@ use meilisearch_core::settings::{Settings, SettingsUpdate, UpdateState, DEFAULT_RANKING_RULES}; -use serde::Deserialize; use std::collections::{BTreeMap, BTreeSet, HashSet}; use tide::{Request, Response}; @@ -73,36 +72,13 @@ pub async fn get_all(ctx: Request) -> SResult { 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 distinct_attribute: Option, - pub primary_key: Option, - pub searchable_attributes: Option>, - pub displayed_attributes: Option>, - pub stop_words: Option>, - pub synonyms: Option>>, - pub accept_new_fields: Option, -} - pub async fn update_all(mut ctx: Request) -> SResult { ctx.is_allowed(Private)?; let index = ctx.index()?; - let settings_update: UpdateSettings = + let settings: Settings = ctx.body_json().await.map_err(ResponseError::bad_request)?; let db = &ctx.state().db; - let settings = Settings { - ranking_rules: Some(settings_update.ranking_rules), - 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), - synonyms: Some(settings_update.synonyms), - accept_new_fields: Some(settings_update.accept_new_fields), - }; - let mut writer = db.update_write_txn()?; let settings = settings.into_update().map_err(ResponseError::bad_request)?; let update_id = index.settings_update(&mut writer, settings)?; diff --git a/meilisearch-http/tests/search.rs b/meilisearch-http/tests/search.rs index a53aecf89..d044d3531 100644 --- a/meilisearch-http/tests/search.rs +++ b/meilisearch-http/tests/search.rs @@ -644,7 +644,6 @@ fn search_with_settings_basic() { "desc(vote_average)" ], "distinctAttribute": null, - "primaryKey": "id", "searchableAttributes": [ "title", "tagline", @@ -751,7 +750,6 @@ fn search_with_settings_stop_words() { "desc(vote_average)" ], "distinctAttribute": null, - "primaryKey": "id", "searchableAttributes": [ "title", "tagline", @@ -858,7 +856,6 @@ fn search_with_settings_synonyms() { "desc(vote_average)" ], "distinctAttribute": null, - "primaryKey": "id", "searchableAttributes": [ "title", "tagline", @@ -970,7 +967,6 @@ fn search_with_settings_ranking_rules() { "desc(popularity)" ], "distinctAttribute": null, - "primaryKey": "id", "searchableAttributes": [ "title", "tagline", @@ -1077,7 +1073,6 @@ fn search_with_settings_searchable_attributes() { "desc(vote_average)" ], "distinctAttribute": null, - "primaryKey": "id", "searchableAttributes": [ "tagline", "overview", @@ -1183,7 +1178,6 @@ fn search_with_settings_displayed_attributes() { "desc(vote_average)" ], "distinctAttribute": null, - "primaryKey": "id", "searchableAttributes": [ "title", "tagline", @@ -1254,7 +1248,6 @@ fn search_with_settings_searchable_attributes_2() { "desc(vote_average)" ], "distinctAttribute": null, - "primaryKey": "id", "searchableAttributes": [ "tagline", "overview", diff --git a/meilisearch-http/tests/settings.rs b/meilisearch-http/tests/settings.rs index c692e4104..7e2b1275d 100644 --- a/meilisearch-http/tests/settings.rs +++ b/meilisearch-http/tests/settings.rs @@ -192,6 +192,7 @@ fn write_all_and_update() { "exactness", "desc(release_date)", ], + "distinctAttribute": null, "searchableAttributes": [ "title", "description", @@ -204,8 +205,7 @@ fn write_all_and_update() { "rank", "poster", ], - "stopWords": [ - ], + "stopWords": [], "synonyms": { "wolverine": ["xmen", "logan"], "logan": ["wolverine", "xmen"], @@ -321,3 +321,111 @@ fn test_default_settings_2() { assert_json_eq!(body, response, ordered: false); } + +// Test issue https://github.com/meilisearch/MeiliSearch/issues/516 +#[test] +fn write_setting_and_update_partial() { + let mut server = common::Server::with_uid("movies"); + let body = json!({ + "uid": "movies", + }); + server.create_index(body); + + // 2 - Send the settings + + let body = json!({ + "searchableAttributes": [ + "uid", + "movie_id", + "title", + "description", + "poster", + "release_date", + "rank", + ], + "displayedAttributes": [ + "title", + "description", + "poster", + "release_date", + "rank", + ] + }); + + server.update_all_settings(body.clone()); + + // 2 - Send the settings + + let body = json!({ + "rankingRules": [ + "typo", + "words", + "proximity", + "attribute", + "wordsPosition", + "exactness", + "desc(release_date)", + "desc(rank)", + ], + "distinctAttribute": "movie_id", + "stopWords": [ + "the", + "a", + "an", + ], + "synonyms": { + "wolverine": ["xmen", "logan"], + "logan": ["wolverine"], + }, + "acceptNewFields": false, + }); + + server.update_all_settings(body.clone()); + + // 2 - Send the settings + + let expected = json!({ + "rankingRules": [ + "typo", + "words", + "proximity", + "attribute", + "wordsPosition", + "exactness", + "desc(release_date)", + "desc(rank)", + ], + "distinctAttribute": "movie_id", + "searchableAttributes": [ + "uid", + "movie_id", + "title", + "description", + "poster", + "release_date", + "rank", + ], + "displayedAttributes": [ + "title", + "description", + "poster", + "release_date", + "rank", + ], + "stopWords": [ + "the", + "a", + "an", + ], + "synonyms": { + "wolverine": ["xmen", "logan"], + "logan": ["wolverine"], + }, + "acceptNewFields": false, + }); + + let (response, _status_code) = server.get_all_settings(); + + assert_json_eq!(expected, response, ordered: false); + +}