From 8cc3c541179f0a9fc5ecd74c9454bf03b4aaff5d Mon Sep 17 00:00:00 2001 From: ManyTheFish Date: Wed, 6 Dec 2023 14:46:12 +0100 Subject: [PATCH] Add proximityPrecision setting in settings route --- meilisearch-types/src/error.rs | 1 + meilisearch-types/src/settings.rs | 47 ++++++++++++++++++++++ meilisearch/src/routes/indexes/settings.rs | 28 +++++++++++++ meilisearch/tests/settings/get_settings.rs | 2 +- 4 files changed, 77 insertions(+), 1 deletion(-) diff --git a/meilisearch-types/src/error.rs b/meilisearch-types/src/error.rs index 653cb108b..b1dc6b777 100644 --- a/meilisearch-types/src/error.rs +++ b/meilisearch-types/src/error.rs @@ -252,6 +252,7 @@ InvalidSearchShowRankingScoreDetails , InvalidRequest , BAD_REQUEST ; InvalidSearchSort , InvalidRequest , BAD_REQUEST ; InvalidSettingsDisplayedAttributes , InvalidRequest , BAD_REQUEST ; InvalidSettingsDistinctAttribute , InvalidRequest , BAD_REQUEST ; +InvalidSettingsProximityPrecision , InvalidRequest , BAD_REQUEST ; InvalidSettingsFaceting , InvalidRequest , BAD_REQUEST ; InvalidSettingsFilterableAttributes , InvalidRequest , BAD_REQUEST ; InvalidSettingsPagination , InvalidRequest , BAD_REQUEST ; diff --git a/meilisearch-types/src/settings.rs b/meilisearch-types/src/settings.rs index a57dc0e9a..487354b8e 100644 --- a/meilisearch-types/src/settings.rs +++ b/meilisearch-types/src/settings.rs @@ -8,6 +8,7 @@ use std::str::FromStr; use deserr::{DeserializeError, Deserr, ErrorKind, MergeWithError, ValuePointerRef}; use fst::IntoStreamer; +use milli::proximity::ProximityPrecision; use milli::update::Setting; use milli::{Criterion, CriterionError, Index, DEFAULT_VALUES_PER_FACET}; use serde::{Deserialize, Serialize, Serializer}; @@ -186,6 +187,9 @@ pub struct Settings { #[deserr(default, error = DeserrJsonError)] pub distinct_attribute: Setting, #[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")] #[deserr(default, error = DeserrJsonError)] pub typo_tolerance: Setting, #[serde(default, skip_serializing_if = "Setting::is_not_set")] @@ -214,6 +218,7 @@ impl Settings { separator_tokens: Setting::Reset, dictionary: Setting::Reset, distinct_attribute: Setting::Reset, + proximity_precision: Setting::Reset, typo_tolerance: Setting::Reset, faceting: Setting::Reset, pagination: Setting::Reset, @@ -234,6 +239,7 @@ impl Settings { dictionary, synonyms, distinct_attribute, + proximity_precision, typo_tolerance, faceting, pagination, @@ -252,6 +258,7 @@ impl Settings { dictionary, synonyms, distinct_attribute, + proximity_precision, typo_tolerance, faceting, pagination, @@ -296,6 +303,7 @@ impl Settings { separator_tokens: self.separator_tokens, dictionary: self.dictionary, distinct_attribute: self.distinct_attribute, + proximity_precision: self.proximity_precision, typo_tolerance: self.typo_tolerance, faceting: self.faceting, pagination: self.pagination, @@ -390,6 +398,12 @@ pub fn apply_settings_to_builder( Setting::NotSet => (), } + match settings.proximity_precision { + Setting::Set(ref precision) => builder.set_proximity_precision((*precision).into()), + Setting::Reset => builder.reset_proximity_precision(), + Setting::NotSet => (), + } + match settings.typo_tolerance { Setting::Set(ref value) => { match value.enabled { @@ -509,6 +523,8 @@ pub fn settings( let distinct_field = index.distinct_field(rtxn)?.map(String::from); + let proximity_precision = index.proximity_precision(rtxn)?.map(ProximityPrecisionView::from); + let synonyms = index.user_defined_synonyms(rtxn)?; let min_typo_word_len = MinWordSizeTyposSetting { @@ -575,6 +591,10 @@ pub fn settings( Some(field) => Setting::Set(field), None => Setting::Reset, }, + proximity_precision: match proximity_precision { + Some(precision) => Setting::Set(precision), + None => Setting::Reset, + }, synonyms: Setting::Set(synonyms), typo_tolerance: Setting::Set(typo_tolerance), faceting: Setting::Set(faceting), @@ -679,6 +699,31 @@ impl From for Criterion { } } +#[derive(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 { + WordScale, + AttributeScale, +} + +impl From for ProximityPrecisionView { + fn from(value: ProximityPrecision) -> Self { + match value { + ProximityPrecision::WordScale => ProximityPrecisionView::WordScale, + ProximityPrecision::AttributeScale => ProximityPrecisionView::AttributeScale, + } + } +} +impl From for ProximityPrecision { + fn from(value: ProximityPrecisionView) -> Self { + match value { + ProximityPrecisionView::WordScale => ProximityPrecision::WordScale, + ProximityPrecisionView::AttributeScale => ProximityPrecision::AttributeScale, + } + } +} + #[cfg(test)] pub(crate) mod test { use super::*; @@ -698,6 +743,7 @@ pub(crate) mod test { dictionary: Setting::NotSet, synonyms: Setting::NotSet, distinct_attribute: Setting::NotSet, + proximity_precision: Setting::NotSet, typo_tolerance: Setting::NotSet, faceting: Setting::NotSet, pagination: Setting::NotSet, @@ -722,6 +768,7 @@ pub(crate) mod test { dictionary: Setting::NotSet, synonyms: Setting::NotSet, distinct_attribute: Setting::NotSet, + proximity_precision: Setting::NotSet, typo_tolerance: Setting::NotSet, faceting: Setting::NotSet, pagination: Setting::NotSet, diff --git a/meilisearch/src/routes/indexes/settings.rs b/meilisearch/src/routes/indexes/settings.rs index f5d98761d..c22db24f0 100644 --- a/meilisearch/src/routes/indexes/settings.rs +++ b/meilisearch/src/routes/indexes/settings.rs @@ -435,6 +435,30 @@ make_setting_route!( } ); +make_setting_route!( + "/proximity-precision", + put, + meilisearch_types::settings::ProximityPrecisionView, + meilisearch_types::deserr::DeserrJsonError< + meilisearch_types::error::deserr_codes::InvalidSettingsProximityPrecision, + >, + proximity_precision, + "proximityPrecision", + analytics, + |precision: &Option, req: &HttpRequest| { + use serde_json::json; + analytics.publish( + "ProximityPrecision Updated".to_string(), + json!({ + "proximity_precision": { + "set": precision.is_some(), + } + }), + Some(req), + ); + } +); + make_setting_route!( "/ranking-rules", put, @@ -541,6 +565,7 @@ generate_configure!( displayed_attributes, searchable_attributes, distinct_attribute, + proximity_precision, stop_words, separator_tokens, non_separator_tokens, @@ -594,6 +619,9 @@ pub async fn update_all( "distinct_attribute": { "set": new_settings.distinct_attribute.as_ref().set().is_some() }, + "proximity_precision": { + "set": new_settings.proximity_precision.as_ref().set().is_some() + }, "typo_tolerance": { "enabled": new_settings.typo_tolerance .as_ref() diff --git a/meilisearch/tests/settings/get_settings.rs b/meilisearch/tests/settings/get_settings.rs index b5c4644a5..0ea556b94 100644 --- a/meilisearch/tests/settings/get_settings.rs +++ b/meilisearch/tests/settings/get_settings.rs @@ -54,7 +54,7 @@ async fn get_settings() { let (response, code) = index.settings().await; assert_eq!(code, 200); let settings = response.as_object().unwrap(); - assert_eq!(settings.keys().len(), 14); + assert_eq!(settings.keys().len(), 15); assert_eq!(settings["displayedAttributes"], json!(["*"])); assert_eq!(settings["searchableAttributes"], json!(["*"])); assert_eq!(settings["filterableAttributes"], json!([]));