Skip to content
This repository has been archived by the owner on Apr 4, 2023. It is now read-only.

Introduce the reset_sortable_fields Settings method #330

Merged
merged 3 commits into from
Aug 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 28 additions & 7 deletions http-ui/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,9 @@ struct Settings {
#[serde(default, skip_serializing_if = "Setting::is_not_set")]
filterable_attributes: Setting<HashSet<String>>,

#[serde(default, skip_serializing_if = "Setting::is_not_set")]
sortable_attributes: Setting<HashSet<String>>,

#[serde(default, skip_serializing_if = "Setting::is_not_set")]
criteria: Setting<Vec<String>>,

Expand Down Expand Up @@ -448,6 +451,15 @@ async fn main() -> anyhow::Result<()> {
Setting::NotSet => (),
}

// We transpose the settings JSON struct into a real setting update.
match settings.sortable_attributes {
Setting::Set(sortable_attributes) => {
builder.set_sortable_fields(sortable_attributes)
}
Setting::Reset => builder.reset_sortable_fields(),
Setting::NotSet => (),
}

// We transpose the settings JSON struct into a real setting update.
match settings.criteria {
Setting::Set(criteria) => builder.set_criteria(criteria),
Expand Down Expand Up @@ -1029,6 +1041,7 @@ mod tests {
displayed_attributes: Setting::Set(vec!["name".to_string()]),
searchable_attributes: Setting::Set(vec!["age".to_string()]),
filterable_attributes: Setting::Set(hashset! { "age".to_string() }),
sortable_attributes: Setting::Set(hashset! { "age".to_string() }),
criteria: Setting::Set(vec!["age:asc".to_string()]),
stop_words: Setting::Set(btreeset! { "and".to_string() }),
synonyms: Setting::Set(hashmap! { "alex".to_string() => vec!["alexey".to_string()] }),
Expand All @@ -1037,7 +1050,7 @@ mod tests {
assert_tokens(
&settings,
&[
Token::Struct { name: "Settings", len: 6 },
Token::Struct { name: "Settings", len: 7 },
Token::Str("displayedAttributes"),
Token::Some,
Token::Seq { len: Some(1) },
Expand All @@ -1048,12 +1061,16 @@ mod tests {
Token::Seq { len: Some(1) },
Token::Str("age"),
Token::SeqEnd,
Token::Str("facetedAttributes"),
Token::Str("filterableAttributes"),
Token::Some,
Token::Map { len: Some(1) },
Token::Seq { len: Some(1) },
Token::Str("age"),
Token::Str("integer"),
Token::MapEnd,
Token::SeqEnd,
Token::Str("sortableAttributes"),
Token::Some,
Token::Seq { len: Some(1) },
Token::Str("age"),
Token::SeqEnd,
Token::Str("criteria"),
Token::Some,
Token::Seq { len: Some(1) },
Expand Down Expand Up @@ -1083,6 +1100,7 @@ mod tests {
displayed_attributes: Setting::Reset,
searchable_attributes: Setting::Reset,
filterable_attributes: Setting::Reset,
sortable_attributes: Setting::Reset,
criteria: Setting::Reset,
stop_words: Setting::Reset,
synonyms: Setting::Reset,
Expand All @@ -1091,12 +1109,14 @@ mod tests {
assert_tokens(
&settings,
&[
Token::Struct { name: "Settings", len: 6 },
Token::Struct { name: "Settings", len: 7 },
Token::Str("displayedAttributes"),
Token::None,
Token::Str("searchableAttributes"),
Token::None,
Token::Str("facetedAttributes"),
Token::Str("filterableAttributes"),
Token::None,
Token::Str("sortableAttributes"),
Token::None,
Token::Str("criteria"),
Token::None,
Expand All @@ -1115,6 +1135,7 @@ mod tests {
displayed_attributes: Setting::NotSet,
searchable_attributes: Setting::NotSet,
filterable_attributes: Setting::NotSet,
sortable_attributes: Setting::NotSet,
criteria: Setting::NotSet,
stop_words: Setting::NotSet,
synonyms: Setting::NotSet,
Expand Down
4 changes: 4 additions & 0 deletions milli/src/update/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,10 @@ impl<'a, 't, 'u, 'i> Settings<'a, 't, 'u, 'i> {
self.sortable_fields = Setting::Set(names);
}

pub fn reset_sortable_fields(&mut self) {
self.sortable_fields = Setting::Reset;
}

pub fn reset_criteria(&mut self) {
self.criteria = Setting::Reset;
}
Expand Down