diff --git a/.code-samples.meilisearch.yaml b/.code-samples.meilisearch.yaml index 7d96cf6c..528cbed2 100644 --- a/.code-samples.meilisearch.yaml +++ b/.code-samples.meilisearch.yaml @@ -580,8 +580,12 @@ get_faceting_settings_1: |- .await .unwrap(); update_faceting_settings_1: |- + let mut facet_sort_setting = BTreeMap::new(); + facet_sort_setting.insert("*".to_string(), FacetSortValue::ALPHA); + facet_sort_setting.insert("genres".to_string(), FacetSortValue::COUNT); let mut faceting = FacetingSettings { max_values_per_facet: 2, + sort_facet_values_by: Some(facet_sort_setting), }; let task: TaskInfo = client @@ -1228,8 +1232,11 @@ getting_started_sorting: |- .await .unwrap(); getting_started_faceting: |- + let mut facet_sort_setting = BTreeMap::new(); + facet_sort_setting.insert("*".to_string(), FacetSortValue::COUNT); let mut faceting = FacetingSettings { max_values_per_facet: 2, + sort_facet_values_by: Some(facet_sort_setting), }; let task: TaskInfo = client @@ -1631,3 +1638,17 @@ create_snapshot_1: |- .create_snapshot() .await .unwrap(); +facet_search_2: |- + let mut facet_sort_setting = BTreeMap::new(); + facet_sort_setting.insert("genres".to_string(), FacetSortValue::COUNT); + let faceting = FacetingSettings { + max_values_per_facet: 5, + sort_facet_values_by: Some(facet_sort_setting), + }; + + let client = client::new("http://localhost:7700", Some("apiKey")); + let res = client.index("books") + .set_faceting(&faceting) + .execute() + .await + .unwrap(); diff --git a/src/settings.rs b/src/settings.rs index fae8dfe9..1554690b 100644 --- a/src/settings.rs +++ b/src/settings.rs @@ -5,7 +5,7 @@ use crate::{ task_info::TaskInfo, }; use serde::{Deserialize, Serialize}; -use std::collections::HashMap; +use std::collections::{BTreeMap, HashMap}; #[derive(Serialize, Deserialize, Default, Debug, Clone, PartialEq, Eq, Copy)] #[serde(rename_all = "camelCase")] @@ -30,10 +30,20 @@ pub struct TypoToleranceSettings { pub min_word_size_for_typos: Option, } -#[derive(Serialize, Deserialize, Default, Debug, Clone, Eq, PartialEq, Copy)] +#[derive(Debug, Deserialize, Clone, Eq, PartialEq, Serialize)] +pub enum FacetSortValue { + #[serde(rename = "alpha")] + ALPHA, + #[serde(rename = "count")] + COUNT, +} + +#[derive(Serialize, Deserialize, Default, Debug, Clone, Eq, PartialEq)] #[serde(rename_all = "camelCase")] pub struct FacetingSettings { pub max_values_per_facet: usize, + #[serde(skip_serializing_if = "Option::is_none")] + pub sort_facet_values_by: Option>, } /// Struct reprensenting a set of settings. @@ -260,8 +270,12 @@ impl Settings { #[must_use] pub fn with_faceting(self, faceting: &FacetingSettings) -> Settings { + let faceting_clone = FacetingSettings { + max_values_per_facet: faceting.max_values_per_facet, + sort_facet_values_by: faceting.sort_facet_values_by.clone(), + }; Settings { - faceting: Some(*faceting), + faceting: Some(faceting_clone), ..self } } @@ -1200,6 +1214,7 @@ impl Index { /// /// let mut faceting = FacetingSettings { /// max_values_per_facet: 12, + /// sort_facet_values_by: None, /// }; /// /// let task = index.set_faceting(&faceting).await.unwrap(); @@ -1823,54 +1838,136 @@ mod tests { #[meilisearch_test] async fn test_set_faceting_settings(client: Client, index: Index) { - let faceting = FacetingSettings { + let req_faceting = FacetingSettings { + max_values_per_facet: 5, + sort_facet_values_by: None, + }; + let settings = Settings::new().with_faceting(&req_faceting); + + let task_info = index.set_settings(&settings).await.unwrap(); + client.wait_for_task(task_info, None, None).await.unwrap(); + + let res = index.get_faceting().await.unwrap(); + + let mut expected_facet_sort_setting = BTreeMap::new(); + expected_facet_sort_setting.insert("*".to_string(), FacetSortValue::ALPHA); + let expected_faceting = FacetingSettings { + max_values_per_facet: req_faceting.max_values_per_facet, + sort_facet_values_by: Some(expected_facet_sort_setting), + }; + + assert_eq!(expected_faceting, res); + } + + #[meilisearch_test] + async fn test_set_faceting_settings_with_sort_values(client: Client, index: Index) { + let mut req_facet_sort_setting = BTreeMap::new(); + req_facet_sort_setting.insert("genres".to_string(), FacetSortValue::COUNT); + let req_faceting = FacetingSettings { max_values_per_facet: 5, + sort_facet_values_by: Some(req_facet_sort_setting), }; - let settings = Settings::new().with_faceting(&faceting); + let settings = Settings::new().with_faceting(&req_faceting); let task_info = index.set_settings(&settings).await.unwrap(); client.wait_for_task(task_info, None, None).await.unwrap(); let res = index.get_faceting().await.unwrap(); - assert_eq!(faceting, res); + let mut expected_facet_sort_setting = BTreeMap::new(); + expected_facet_sort_setting.insert("*".to_string(), FacetSortValue::ALPHA); + expected_facet_sort_setting.insert("genres".to_string(), FacetSortValue::COUNT); + let expected_faceting = FacetingSettings { + max_values_per_facet: req_faceting.max_values_per_facet, + sort_facet_values_by: Some(expected_facet_sort_setting), + }; + + assert_eq!(expected_faceting, res); } #[meilisearch_test] async fn test_get_faceting(index: Index) { - let faceting = FacetingSettings { + let req_faceting = FacetingSettings { max_values_per_facet: 100, + sort_facet_values_by: None, }; let res = index.get_faceting().await.unwrap(); - assert_eq!(faceting, res); + let mut expected_facet_sort_setting = BTreeMap::new(); + expected_facet_sort_setting.insert("*".to_string(), FacetSortValue::ALPHA); + let expected_faceting = FacetingSettings { + max_values_per_facet: req_faceting.max_values_per_facet, + sort_facet_values_by: Some(expected_facet_sort_setting), + }; + + assert_eq!(expected_faceting, res); } #[meilisearch_test] async fn test_set_faceting(client: Client, index: Index) { - let faceting = FacetingSettings { + let req_faceting = FacetingSettings { + max_values_per_facet: 5, + sort_facet_values_by: None, + }; + let task_info = index.set_faceting(&req_faceting).await.unwrap(); + client.wait_for_task(task_info, None, None).await.unwrap(); + + let res = index.get_faceting().await.unwrap(); + + let mut expected_facet_sort_setting = BTreeMap::new(); + expected_facet_sort_setting.insert("*".to_string(), FacetSortValue::ALPHA); + let expected_faceting = FacetingSettings { + max_values_per_facet: req_faceting.max_values_per_facet, + sort_facet_values_by: Some(expected_facet_sort_setting), + }; + + assert_eq!(expected_faceting, res); + } + + #[meilisearch_test] + async fn test_set_faceting_with_sort_values(client: Client, index: Index) { + let mut req_facet_sort_setting = BTreeMap::new(); + req_facet_sort_setting.insert("genres".to_string(), FacetSortValue::COUNT); + let req_faceting = FacetingSettings { max_values_per_facet: 5, + sort_facet_values_by: Some(req_facet_sort_setting), }; - let task_info = index.set_faceting(&faceting).await.unwrap(); + let task_info = index.set_faceting(&req_faceting).await.unwrap(); client.wait_for_task(task_info, None, None).await.unwrap(); let res = index.get_faceting().await.unwrap(); - assert_eq!(faceting, res); + let mut expected_facet_sort_setting = BTreeMap::new(); + expected_facet_sort_setting.insert("*".to_string(), FacetSortValue::ALPHA); + expected_facet_sort_setting.insert("genres".to_string(), FacetSortValue::COUNT); + let expected_faceting = FacetingSettings { + max_values_per_facet: req_faceting.max_values_per_facet, + sort_facet_values_by: Some(expected_facet_sort_setting), + }; + + assert_eq!(expected_faceting, res); } #[meilisearch_test] async fn test_reset_faceting(client: Client, index: Index) { let task_info = index.reset_faceting().await.unwrap(); client.wait_for_task(task_info, None, None).await.unwrap(); - let faceting = FacetingSettings { + let req_faceting = FacetingSettings { max_values_per_facet: 100, + sort_facet_values_by: None, }; let res = index.get_faceting().await.unwrap(); - assert_eq!(faceting, res); + let mut expected_facet_sort_setting = BTreeMap::new(); + expected_facet_sort_setting.insert("*".to_string(), FacetSortValue::ALPHA); + let expected_faceting = FacetingSettings { + max_values_per_facet: req_faceting.max_values_per_facet, + sort_facet_values_by: Some(expected_facet_sort_setting), + }; + + assert_eq!(expected_faceting, res); } #[meilisearch_test]