From ea4c831de07435bc8618634bc3ee2edfba13c523 Mon Sep 17 00:00:00 2001 From: Kerollmops Date: Tue, 24 Aug 2021 15:46:14 +0200 Subject: [PATCH] Integrate the sortable-attributes into the settings --- meilisearch-http/src/index/mod.rs | 3 +++ meilisearch-http/src/index/updates.rs | 14 ++++++++++++++ .../src/index_controller/dump_actor/loaders/v1.rs | 5 +---- meilisearch-http/src/routes/indexes/settings.rs | 8 ++++++++ meilisearch-http/tests/settings/get_settings.rs | 3 ++- 5 files changed, 28 insertions(+), 5 deletions(-) diff --git a/meilisearch-http/src/index/mod.rs b/meilisearch-http/src/index/mod.rs index 827d30753..e4243aadc 100644 --- a/meilisearch-http/src/index/mod.rs +++ b/meilisearch-http/src/index/mod.rs @@ -63,6 +63,8 @@ impl Index { let filterable_attributes = self.filterable_fields(txn)?.into_iter().collect(); + let sortable_attributes = self.sortable_fields(txn)?.into_iter().collect(); + let criteria = self .criteria(txn)? .into_iter() @@ -101,6 +103,7 @@ impl Index { None => Setting::Reset, }, filterable_attributes: Setting::Set(filterable_attributes), + sortable_attributes: Setting::Set(sortable_attributes), ranking_rules: Setting::Set(criteria), stop_words: Setting::Set(stop_words), distinct_attribute: match distinct_field { diff --git a/meilisearch-http/src/index/updates.rs b/meilisearch-http/src/index/updates.rs index abfb55d1d..dd94f2cd2 100644 --- a/meilisearch-http/src/index/updates.rs +++ b/meilisearch-http/src/index/updates.rs @@ -57,6 +57,8 @@ pub struct Settings { #[serde(default, skip_serializing_if = "Setting::is_not_set")] pub filterable_attributes: Setting>, #[serde(default, skip_serializing_if = "Setting::is_not_set")] + pub sortable_attributes: Setting>, + #[serde(default, skip_serializing_if = "Setting::is_not_set")] pub ranking_rules: Setting>, #[serde(default, skip_serializing_if = "Setting::is_not_set")] pub stop_words: Setting>, @@ -75,6 +77,7 @@ impl Settings { displayed_attributes: Setting::Reset, searchable_attributes: Setting::Reset, filterable_attributes: Setting::Reset, + sortable_attributes: Setting::Reset, ranking_rules: Setting::Reset, stop_words: Setting::Reset, synonyms: Setting::Reset, @@ -88,6 +91,7 @@ impl Settings { displayed_attributes, searchable_attributes, filterable_attributes, + sortable_attributes, ranking_rules, stop_words, synonyms, @@ -99,6 +103,7 @@ impl Settings { displayed_attributes, searchable_attributes, filterable_attributes, + sortable_attributes, ranking_rules, stop_words, synonyms, @@ -136,6 +141,7 @@ impl Settings { displayed_attributes, searchable_attributes, filterable_attributes: self.filterable_attributes, + sortable_attributes: self.sortable_attributes, ranking_rules: self.ranking_rules, stop_words: self.stop_words, synonyms: self.synonyms, @@ -253,6 +259,12 @@ impl Index { Setting::NotSet => (), } + match settings.sortable_attributes { + Setting::Set(ref facet_types) => builder.set_sortable_fields(facet_types.clone()), + Setting::Reset => builder.set_sortable_fields(HashSet::new()), + Setting::NotSet => (), + } + match settings.ranking_rules { Setting::Set(ref criteria) => builder.set_criteria(criteria.clone()), Setting::Reset => builder.reset_criteria(), @@ -328,6 +340,7 @@ mod test { displayed_attributes: Setting::Set(vec![String::from("hello")]), searchable_attributes: Setting::Set(vec![String::from("hello")]), filterable_attributes: Setting::NotSet, + sortable_attributes: Setting::NotSet, ranking_rules: Setting::NotSet, stop_words: Setting::NotSet, synonyms: Setting::NotSet, @@ -348,6 +361,7 @@ mod test { displayed_attributes: Setting::Set(vec![String::from("*")]), searchable_attributes: Setting::Set(vec![String::from("hello"), String::from("*")]), filterable_attributes: Setting::NotSet, + sortable_attributes: Setting::NotSet, ranking_rules: Setting::NotSet, stop_words: Setting::NotSet, synonyms: Setting::NotSet, diff --git a/meilisearch-http/src/index_controller/dump_actor/loaders/v1.rs b/meilisearch-http/src/index_controller/dump_actor/loaders/v1.rs index 9bcc4dd7b..6a505c077 100644 --- a/meilisearch-http/src/index_controller/dump_actor/loaders/v1.rs +++ b/meilisearch-http/src/index_controller/dump_actor/loaders/v1.rs @@ -158,15 +158,12 @@ impl From for index_controller::Settings { Some(None) => Setting::Reset, None => Setting::NotSet }, - // we previously had a `Vec` but now we have a `HashMap` - // representing the name of the faceted field + the type of the field. Since the type - // was not known in the V1 of the dump we are just going to assume everything is a - // String filterable_attributes: match settings.attributes_for_faceting { Some(Some(attrs)) => Setting::Set(attrs.into_iter().collect()), Some(None) => Setting::Reset, None => Setting::NotSet }, + sortable_attributes: Setting::NotSet, // we need to convert the old `Vec` into a `BTreeSet` ranking_rules: match settings.ranking_rules { Some(Some(ranking_rules)) => Setting::Set(ranking_rules.into_iter().filter(|criterion| { diff --git a/meilisearch-http/src/routes/indexes/settings.rs b/meilisearch-http/src/routes/indexes/settings.rs index 9cdf515aa..a7ab2f7e0 100644 --- a/meilisearch-http/src/routes/indexes/settings.rs +++ b/meilisearch-http/src/routes/indexes/settings.rs @@ -80,6 +80,13 @@ make_setting_route!( "filterableAttributes" ); +make_setting_route!( + "/sortable-attributes", + std::collections::HashSet, + sortable_attributes, + "sortableAttributes" +); + make_setting_route!( "/displayed-attributes", Vec, @@ -132,6 +139,7 @@ macro_rules! generate_configure { generate_configure!( filterable_attributes, + sortable_attributes, displayed_attributes, searchable_attributes, distinct_attribute, diff --git a/meilisearch-http/tests/settings/get_settings.rs b/meilisearch-http/tests/settings/get_settings.rs index 2816ea38d..3b3ab5735 100644 --- a/meilisearch-http/tests/settings/get_settings.rs +++ b/meilisearch-http/tests/settings/get_settings.rs @@ -42,10 +42,11 @@ 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(), 7); + assert_eq!(settings.keys().len(), 8); assert_eq!(settings["displayedAttributes"], json!(["*"])); assert_eq!(settings["searchableAttributes"], json!(["*"])); assert_eq!(settings["filterableAttributes"], json!([])); + assert_eq!(settings["sortableAttributes"], json!([])); assert_eq!(settings["distinctAttribute"], json!(null)); assert_eq!( settings["rankingRules"],