Integrate the sortable-attributes into the settings

This commit is contained in:
Kerollmops 2021-08-24 15:46:14 +02:00
parent 51387b2c80
commit ea4c831de0
No known key found for this signature in database
GPG Key ID: 92ADA4E935E71FA4
5 changed files with 28 additions and 5 deletions

View File

@ -63,6 +63,8 @@ impl Index {
let filterable_attributes = self.filterable_fields(txn)?.into_iter().collect(); let filterable_attributes = self.filterable_fields(txn)?.into_iter().collect();
let sortable_attributes = self.sortable_fields(txn)?.into_iter().collect();
let criteria = self let criteria = self
.criteria(txn)? .criteria(txn)?
.into_iter() .into_iter()
@ -101,6 +103,7 @@ impl Index {
None => Setting::Reset, None => Setting::Reset,
}, },
filterable_attributes: Setting::Set(filterable_attributes), filterable_attributes: Setting::Set(filterable_attributes),
sortable_attributes: Setting::Set(sortable_attributes),
ranking_rules: Setting::Set(criteria), ranking_rules: Setting::Set(criteria),
stop_words: Setting::Set(stop_words), stop_words: Setting::Set(stop_words),
distinct_attribute: match distinct_field { distinct_attribute: match distinct_field {

View File

@ -57,6 +57,8 @@ pub struct Settings<T> {
#[serde(default, skip_serializing_if = "Setting::is_not_set")] #[serde(default, skip_serializing_if = "Setting::is_not_set")]
pub filterable_attributes: Setting<HashSet<String>>, pub filterable_attributes: Setting<HashSet<String>>,
#[serde(default, skip_serializing_if = "Setting::is_not_set")] #[serde(default, skip_serializing_if = "Setting::is_not_set")]
pub sortable_attributes: Setting<HashSet<String>>,
#[serde(default, skip_serializing_if = "Setting::is_not_set")]
pub ranking_rules: Setting<Vec<String>>, pub ranking_rules: Setting<Vec<String>>,
#[serde(default, skip_serializing_if = "Setting::is_not_set")] #[serde(default, skip_serializing_if = "Setting::is_not_set")]
pub stop_words: Setting<BTreeSet<String>>, pub stop_words: Setting<BTreeSet<String>>,
@ -75,6 +77,7 @@ impl Settings<Checked> {
displayed_attributes: Setting::Reset, displayed_attributes: Setting::Reset,
searchable_attributes: Setting::Reset, searchable_attributes: Setting::Reset,
filterable_attributes: Setting::Reset, filterable_attributes: Setting::Reset,
sortable_attributes: Setting::Reset,
ranking_rules: Setting::Reset, ranking_rules: Setting::Reset,
stop_words: Setting::Reset, stop_words: Setting::Reset,
synonyms: Setting::Reset, synonyms: Setting::Reset,
@ -88,6 +91,7 @@ impl Settings<Checked> {
displayed_attributes, displayed_attributes,
searchable_attributes, searchable_attributes,
filterable_attributes, filterable_attributes,
sortable_attributes,
ranking_rules, ranking_rules,
stop_words, stop_words,
synonyms, synonyms,
@ -99,6 +103,7 @@ impl Settings<Checked> {
displayed_attributes, displayed_attributes,
searchable_attributes, searchable_attributes,
filterable_attributes, filterable_attributes,
sortable_attributes,
ranking_rules, ranking_rules,
stop_words, stop_words,
synonyms, synonyms,
@ -136,6 +141,7 @@ impl Settings<Unchecked> {
displayed_attributes, displayed_attributes,
searchable_attributes, searchable_attributes,
filterable_attributes: self.filterable_attributes, filterable_attributes: self.filterable_attributes,
sortable_attributes: self.sortable_attributes,
ranking_rules: self.ranking_rules, ranking_rules: self.ranking_rules,
stop_words: self.stop_words, stop_words: self.stop_words,
synonyms: self.synonyms, synonyms: self.synonyms,
@ -253,6 +259,12 @@ impl Index {
Setting::NotSet => (), 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 { match settings.ranking_rules {
Setting::Set(ref criteria) => builder.set_criteria(criteria.clone()), Setting::Set(ref criteria) => builder.set_criteria(criteria.clone()),
Setting::Reset => builder.reset_criteria(), Setting::Reset => builder.reset_criteria(),
@ -328,6 +340,7 @@ mod test {
displayed_attributes: Setting::Set(vec![String::from("hello")]), displayed_attributes: Setting::Set(vec![String::from("hello")]),
searchable_attributes: Setting::Set(vec![String::from("hello")]), searchable_attributes: Setting::Set(vec![String::from("hello")]),
filterable_attributes: Setting::NotSet, filterable_attributes: Setting::NotSet,
sortable_attributes: Setting::NotSet,
ranking_rules: Setting::NotSet, ranking_rules: Setting::NotSet,
stop_words: Setting::NotSet, stop_words: Setting::NotSet,
synonyms: Setting::NotSet, synonyms: Setting::NotSet,
@ -348,6 +361,7 @@ mod test {
displayed_attributes: Setting::Set(vec![String::from("*")]), displayed_attributes: Setting::Set(vec![String::from("*")]),
searchable_attributes: Setting::Set(vec![String::from("hello"), String::from("*")]), searchable_attributes: Setting::Set(vec![String::from("hello"), String::from("*")]),
filterable_attributes: Setting::NotSet, filterable_attributes: Setting::NotSet,
sortable_attributes: Setting::NotSet,
ranking_rules: Setting::NotSet, ranking_rules: Setting::NotSet,
stop_words: Setting::NotSet, stop_words: Setting::NotSet,
synonyms: Setting::NotSet, synonyms: Setting::NotSet,

View File

@ -158,15 +158,12 @@ impl From<Settings> for index_controller::Settings<Unchecked> {
Some(None) => Setting::Reset, Some(None) => Setting::Reset,
None => Setting::NotSet None => Setting::NotSet
}, },
// we previously had a `Vec<String>` but now we have a `HashMap<String, String>`
// 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 { filterable_attributes: match settings.attributes_for_faceting {
Some(Some(attrs)) => Setting::Set(attrs.into_iter().collect()), Some(Some(attrs)) => Setting::Set(attrs.into_iter().collect()),
Some(None) => Setting::Reset, Some(None) => Setting::Reset,
None => Setting::NotSet None => Setting::NotSet
}, },
sortable_attributes: Setting::NotSet,
// we need to convert the old `Vec<String>` into a `BTreeSet<String>` // we need to convert the old `Vec<String>` into a `BTreeSet<String>`
ranking_rules: match settings.ranking_rules { ranking_rules: match settings.ranking_rules {
Some(Some(ranking_rules)) => Setting::Set(ranking_rules.into_iter().filter(|criterion| { Some(Some(ranking_rules)) => Setting::Set(ranking_rules.into_iter().filter(|criterion| {

View File

@ -80,6 +80,13 @@ make_setting_route!(
"filterableAttributes" "filterableAttributes"
); );
make_setting_route!(
"/sortable-attributes",
std::collections::HashSet<String>,
sortable_attributes,
"sortableAttributes"
);
make_setting_route!( make_setting_route!(
"/displayed-attributes", "/displayed-attributes",
Vec<String>, Vec<String>,
@ -132,6 +139,7 @@ macro_rules! generate_configure {
generate_configure!( generate_configure!(
filterable_attributes, filterable_attributes,
sortable_attributes,
displayed_attributes, displayed_attributes,
searchable_attributes, searchable_attributes,
distinct_attribute, distinct_attribute,

View File

@ -42,10 +42,11 @@ async fn get_settings() {
let (response, code) = index.settings().await; let (response, code) = index.settings().await;
assert_eq!(code, 200); assert_eq!(code, 200);
let settings = response.as_object().unwrap(); 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["displayedAttributes"], json!(["*"]));
assert_eq!(settings["searchableAttributes"], json!(["*"])); assert_eq!(settings["searchableAttributes"], json!(["*"]));
assert_eq!(settings["filterableAttributes"], json!([])); assert_eq!(settings["filterableAttributes"], json!([]));
assert_eq!(settings["sortableAttributes"], json!([]));
assert_eq!(settings["distinctAttribute"], json!(null)); assert_eq!(settings["distinctAttribute"], json!(null));
assert_eq!( assert_eq!(
settings["rankingRules"], settings["rankingRules"],