From 6997eb67e5926b916050c007b4170c6effb9c3ac Mon Sep 17 00:00:00 2001 From: hmacr Date: Wed, 6 Sep 2023 13:34:17 +0530 Subject: [PATCH 1/2] Allow users to customize facet value sort behaviour --- .code-samples.meilisearch.yaml | 21 ++++++ src/settings.rs | 123 +++++++++++++++++++++++++++++---- 2 files changed, 131 insertions(+), 13 deletions(-) diff --git a/.code-samples.meilisearch.yaml b/.code-samples.meilisearch.yaml index b840d99e..8855f513 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 @@ -1248,8 +1252,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 @@ -1656,3 +1663,17 @@ multi_search_1: |- .execute::() .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(); \ No newline at end of file diff --git a/src/settings.rs b/src/settings.rs index 430f8ea1..6da025d6 100644 --- a/src/settings.rs +++ b/src/settings.rs @@ -3,7 +3,7 @@ use crate::{ Error, Index, 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")] @@ -28,10 +28,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. @@ -238,8 +248,12 @@ impl Settings { } 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 } } @@ -1067,6 +1081,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(); @@ -1529,54 +1544,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] From 70bae60ba709a05d4241c1808727831bb73e4857 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9mentine=20U=2E=20-=20curqui?= Date: Tue, 12 Sep 2023 11:38:21 +0200 Subject: [PATCH 2/2] Update .code-samples.meilisearch.yaml --- .code-samples.meilisearch.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.code-samples.meilisearch.yaml b/.code-samples.meilisearch.yaml index 8855f513..d92f27c0 100644 --- a/.code-samples.meilisearch.yaml +++ b/.code-samples.meilisearch.yaml @@ -1676,4 +1676,4 @@ facet_search_2: |- .set_faceting(&faceting) .execute() .await - .unwrap(); \ No newline at end of file + .unwrap();