From f75f22e0263c4e76913cae37b5e56d0fa424a267 Mon Sep 17 00:00:00 2001 From: ManyTheFish Date: Mon, 8 Jan 2024 11:09:37 +0100 Subject: [PATCH 1/2] Display default value when proximityPrecision is not set --- meilisearch-types/src/settings.rs | 20 ++++++++++++++++-- meilisearch/tests/dumps/mod.rs | 24 +++++++++++----------- meilisearch/tests/settings/get_settings.rs | 1 + 3 files changed, 31 insertions(+), 14 deletions(-) diff --git a/meilisearch-types/src/settings.rs b/meilisearch-types/src/settings.rs index 244fbffa2..7606fa44a 100644 --- a/meilisearch-types/src/settings.rs +++ b/meilisearch-types/src/settings.rs @@ -37,6 +37,17 @@ where .serialize(s) } +fn serialize_with_default(field: &Setting, s: S) -> std::result::Result +where + T: Default + Serialize, + S: Serializer, +{ + match field { + Setting::Set(value) => value.serialize(s), + Setting::Reset | Setting::NotSet => T::default().serialize(s), + } +} + #[derive(Clone, Default, Debug, Serialize, PartialEq, Eq)] pub struct Checked; @@ -186,7 +197,11 @@ pub struct Settings { #[serde(default, skip_serializing_if = "Setting::is_not_set")] #[deserr(default, error = DeserrJsonError)] pub distinct_attribute: Setting, - #[serde(default, skip_serializing_if = "Setting::is_not_set")] + #[serde( + default, + serialize_with = "serialize_with_default", + skip_serializing_if = "Setting::is_not_set" + )] #[deserr(default, error = DeserrJsonError)] pub proximity_precision: Setting, #[serde(default, skip_serializing_if = "Setting::is_not_set")] @@ -735,10 +750,11 @@ impl From for Criterion { } } -#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserr, Serialize, Deserialize)] +#[derive(Default, Debug, Clone, Copy, PartialEq, Eq, Deserr, Serialize, Deserialize)] #[serde(deny_unknown_fields, rename_all = "camelCase")] #[deserr(error = DeserrJsonError, rename_all = camelCase, deny_unknown_fields)] pub enum ProximityPrecisionView { + #[default] ByWord, ByAttribute, } diff --git a/meilisearch/tests/dumps/mod.rs b/meilisearch/tests/dumps/mod.rs index 5ad0ed247..90b9b91da 100644 --- a/meilisearch/tests/dumps/mod.rs +++ b/meilisearch/tests/dumps/mod.rs @@ -59,7 +59,7 @@ async fn import_dump_v1_movie_raw() { "dictionary": [], "synonyms": {}, "distinctAttribute": null, - "proximityPrecision": null, + "proximityPrecision": "byWord", "typoTolerance": { "enabled": true, "minWordSizeForTypos": { @@ -221,7 +221,7 @@ async fn import_dump_v1_movie_with_settings() { "dictionary": [], "synonyms": {}, "distinctAttribute": null, - "proximityPrecision": null, + "proximityPrecision": "byWord", "typoTolerance": { "enabled": true, "minWordSizeForTypos": { @@ -369,7 +369,7 @@ async fn import_dump_v1_rubygems_with_settings() { "dictionary": [], "synonyms": {}, "distinctAttribute": null, - "proximityPrecision": null, + "proximityPrecision": "byWord", "typoTolerance": { "enabled": true, "minWordSizeForTypos": { @@ -503,7 +503,7 @@ async fn import_dump_v2_movie_raw() { "dictionary": [], "synonyms": {}, "distinctAttribute": null, - "proximityPrecision": null, + "proximityPrecision": "byWord", "typoTolerance": { "enabled": true, "minWordSizeForTypos": { @@ -649,7 +649,7 @@ async fn import_dump_v2_movie_with_settings() { "dictionary": [], "synonyms": {}, "distinctAttribute": null, - "proximityPrecision": null, + "proximityPrecision": "byWord", "typoTolerance": { "enabled": true, "minWordSizeForTypos": { @@ -794,7 +794,7 @@ async fn import_dump_v2_rubygems_with_settings() { "dictionary": [], "synonyms": {}, "distinctAttribute": null, - "proximityPrecision": null, + "proximityPrecision": "byWord", "typoTolerance": { "enabled": true, "minWordSizeForTypos": { @@ -928,7 +928,7 @@ async fn import_dump_v3_movie_raw() { "dictionary": [], "synonyms": {}, "distinctAttribute": null, - "proximityPrecision": null, + "proximityPrecision": "byWord", "typoTolerance": { "enabled": true, "minWordSizeForTypos": { @@ -1074,7 +1074,7 @@ async fn import_dump_v3_movie_with_settings() { "dictionary": [], "synonyms": {}, "distinctAttribute": null, - "proximityPrecision": null, + "proximityPrecision": "byWord", "typoTolerance": { "enabled": true, "minWordSizeForTypos": { @@ -1219,7 +1219,7 @@ async fn import_dump_v3_rubygems_with_settings() { "dictionary": [], "synonyms": {}, "distinctAttribute": null, - "proximityPrecision": null, + "proximityPrecision": "byWord", "typoTolerance": { "enabled": true, "minWordSizeForTypos": { @@ -1353,7 +1353,7 @@ async fn import_dump_v4_movie_raw() { "dictionary": [], "synonyms": {}, "distinctAttribute": null, - "proximityPrecision": null, + "proximityPrecision": "byWord", "typoTolerance": { "enabled": true, "minWordSizeForTypos": { @@ -1499,7 +1499,7 @@ async fn import_dump_v4_movie_with_settings() { "dictionary": [], "synonyms": {}, "distinctAttribute": null, - "proximityPrecision": null, + "proximityPrecision": "byWord", "typoTolerance": { "enabled": true, "minWordSizeForTypos": { @@ -1644,7 +1644,7 @@ async fn import_dump_v4_rubygems_with_settings() { "dictionary": [], "synonyms": {}, "distinctAttribute": null, - "proximityPrecision": null, + "proximityPrecision": "byWord", "typoTolerance": { "enabled": true, "minWordSizeForTypos": { diff --git a/meilisearch/tests/settings/get_settings.rs b/meilisearch/tests/settings/get_settings.rs index 9ab53c51e..79689c1b5 100644 --- a/meilisearch/tests/settings/get_settings.rs +++ b/meilisearch/tests/settings/get_settings.rs @@ -84,6 +84,7 @@ async fn get_settings() { }) ); assert_eq!(settings["embedders"], json!({})); + assert_eq!(settings["proximityPrecision"], json!("byWord")); } #[actix_rt::test] From e27b850b0989609e7e97af8023e915168db57a55 Mon Sep 17 00:00:00 2001 From: ManyTheFish Date: Mon, 8 Jan 2024 14:03:47 +0100 Subject: [PATCH 2/2] move the default display strategy on setting getter function --- meilisearch-types/src/settings.rs | 22 ++-------------------- 1 file changed, 2 insertions(+), 20 deletions(-) diff --git a/meilisearch-types/src/settings.rs b/meilisearch-types/src/settings.rs index 7606fa44a..9613cb9f3 100644 --- a/meilisearch-types/src/settings.rs +++ b/meilisearch-types/src/settings.rs @@ -37,17 +37,6 @@ where .serialize(s) } -fn serialize_with_default(field: &Setting, s: S) -> std::result::Result -where - T: Default + Serialize, - S: Serializer, -{ - match field { - Setting::Set(value) => value.serialize(s), - Setting::Reset | Setting::NotSet => T::default().serialize(s), - } -} - #[derive(Clone, Default, Debug, Serialize, PartialEq, Eq)] pub struct Checked; @@ -197,11 +186,7 @@ pub struct Settings { #[serde(default, skip_serializing_if = "Setting::is_not_set")] #[deserr(default, error = DeserrJsonError)] pub distinct_attribute: Setting, - #[serde( - default, - serialize_with = "serialize_with_default", - skip_serializing_if = "Setting::is_not_set" - )] + #[serde(default, skip_serializing_if = "Setting::is_not_set")] #[deserr(default, error = DeserrJsonError)] pub proximity_precision: Setting, #[serde(default, skip_serializing_if = "Setting::is_not_set")] @@ -641,10 +626,7 @@ pub fn settings( Some(field) => Setting::Set(field), None => Setting::Reset, }, - proximity_precision: match proximity_precision { - Some(precision) => Setting::Set(precision), - None => Setting::Reset, - }, + proximity_precision: Setting::Set(proximity_precision.unwrap_or_default()), synonyms: Setting::Set(synonyms), typo_tolerance: Setting::Set(typo_tolerance), faceting: Setting::Set(faceting),