@@ -7,6 +7,13 @@ use crate::{
77use serde:: { Deserialize , Serialize } ;
88use std:: collections:: HashMap ;
99
10+ #[ derive( Serialize , Deserialize , Default , Debug , Clone , Eq , PartialEq ) ]
11+ #[ serde( rename_all = "camelCase" ) ]
12+ pub struct FacetingSettings {
13+ #[ serde( ) ]
14+ pub max_values_per_facet : usize ,
15+ }
16+
1017/// Struct reprensenting a set of settings.
1118/// You can build this struct using the builder syntax.
1219///
@@ -58,6 +65,9 @@ pub struct Settings {
5865 /// Fields displayed in the returned documents
5966 #[ serde( skip_serializing_if = "Option::is_none" ) ]
6067 pub displayed_attributes : Option < Vec < String > > ,
68+ /// Faceting settings
69+ #[ serde( skip_serializing_if = "Option::is_none" ) ]
70+ pub faceting : Option < FacetingSettings > ,
6171}
6272
6373#[ allow( missing_docs) ]
@@ -73,6 +83,7 @@ impl Settings {
7383 distinct_attribute : None ,
7484 searchable_attributes : None ,
7585 displayed_attributes : None ,
86+ faceting : None ,
7687 }
7788 }
7889 pub fn with_synonyms < S , U , V > ( self , synonyms : HashMap < S , U > ) -> Settings
@@ -193,6 +204,16 @@ impl Settings {
193204 ..self
194205 }
195206 }
207+
208+ pub fn with_faceting (
209+ self ,
210+ faceting : & FacetingSettings ,
211+ ) -> Settings {
212+ Settings {
213+ faceting : Some ( faceting. clone ( ) ) ,
214+ ..self
215+ }
216+ }
196217}
197218
198219impl Index {
@@ -454,6 +475,35 @@ impl Index {
454475 . await
455476 }
456477
478+ /// Get [faceting](https://docs.meilisearch.com/reference/api/settings.html#faceting) settings of the [Index].
479+ ///
480+ /// ```
481+ /// # use meilisearch_sdk::{client::*, indexes::*};
482+ /// #
483+ /// # let MEILISEARCH_HOST = option_env!("MEILISEARCH_HOST").unwrap_or("http://localhost:7700");
484+ /// # let MEILISEARCH_API_KEY = option_env!("MEILISEARCH_API_KEY").unwrap_or("masterKey");
485+ /// #
486+ /// # futures::executor::block_on(async move {
487+ /// let client = Client::new(MEILISEARCH_HOST, MEILISEARCH_API_KEY);
488+ /// # client.create_index("get_faceting", None).await.unwrap().wait_for_completion(&client, None, None).await.unwrap();
489+ /// let index = client.index("get_faceting");
490+ /// let faceting = index.get_faceting().await.unwrap();
491+ /// # index.delete().await.unwrap().wait_for_completion(&client, None, None).await.unwrap();
492+ /// # });
493+ /// ```
494+ pub async fn get_faceting ( & self ) -> Result < FacetingSettings , Error > {
495+ request :: < ( ) , FacetingSettings > (
496+ & format ! (
497+ "{}/indexes/{}/settings/faceting" ,
498+ self . client. host, self . uid
499+ ) ,
500+ & self . client . api_key ,
501+ Method :: Get ( ( ) ) ,
502+ 200 ,
503+ )
504+ . await
505+ }
506+
457507 /// Update [settings](../settings/struct.Settings.html) of the [Index].
458508 /// Updates in the settings are partial. This means that any parameters corresponding to a `None` value will be left unchanged.
459509 ///
@@ -816,6 +866,45 @@ impl Index {
816866 . await
817867 }
818868
869+ /// Update [faceting](https://docs.meilisearch.com/reference/api/settings.html#faceting) settings of the [Index].
870+ ///
871+ /// # Example
872+ ///
873+ /// ```
874+ /// # use meilisearch_sdk::{client::*, indexes::*, settings::Settings, settings::FacetingSettings};
875+ /// #
876+ /// # let MEILISEARCH_HOST = option_env!("MEILISEARCH_HOST").unwrap_or("http://localhost:7700");
877+ /// # let MEILISEARCH_API_KEY = option_env!("MEILISEARCH_API_KEY").unwrap_or("masterKey");
878+ /// #
879+ /// # futures::executor::block_on(async move {
880+ /// let client = Client::new(MEILISEARCH_HOST, MEILISEARCH_API_KEY);
881+ /// # client.create_index("set_faceting", None).await.unwrap().wait_for_completion(&client, None, None).await.unwrap();
882+ /// let mut index = client.index("set_faceting");
883+ ///
884+ /// let mut faceting = FacetingSettings {
885+ /// max_values_per_facet: 12,
886+ /// };
887+ ///
888+ /// let task = index.set_faceting(&faceting).await.unwrap();
889+ /// # index.delete().await.unwrap().wait_for_completion(&client, None, None).await.unwrap();
890+ /// # });
891+ /// ```
892+ pub async fn set_faceting (
893+ & self ,
894+ faceting : & FacetingSettings ,
895+ ) -> Result < TaskInfo , Error > {
896+ request :: < & FacetingSettings , TaskInfo > (
897+ & format ! (
898+ "{}/indexes/{}/settings/faceting" ,
899+ self . client. host, self . uid
900+ ) ,
901+ & self . client . api_key ,
902+ Method :: Patch ( faceting) ,
903+ 202 ,
904+ )
905+ . await
906+ }
907+
819908 /// Reset [Settings] of the [Index].
820909 /// All settings will be reset to their [default value](https://docs.meilisearch.com/reference/api/settings.html#reset-settings).
821910 ///
@@ -1102,4 +1191,97 @@ impl Index {
11021191 )
11031192 . await
11041193 }
1194+
1195+ /// Reset [faceting](https://docs.meilisearch.com/reference/api/settings.html#faceting) settings of the [Index].
1196+ ///
1197+ /// # Example
1198+ ///
1199+ /// ```
1200+ /// # use meilisearch_sdk::{client::*, indexes::*, settings::Settings};
1201+ /// #
1202+ /// # let MEILISEARCH_HOST = option_env!("MEILISEARCH_HOST").unwrap_or("http://localhost:7700");
1203+ /// # let MEILISEARCH_API_KEY = option_env!("MEILISEARCH_API_KEY").unwrap_or("masterKey");
1204+ /// #
1205+ /// # futures::executor::block_on(async move {
1206+ /// let client = Client::new(MEILISEARCH_HOST, MEILISEARCH_API_KEY);
1207+ /// # client.create_index("reset_faceting", None).await.unwrap().wait_for_completion(&client, None, None).await.unwrap();
1208+ /// let mut index = client.index("reset_faceting");
1209+ ///
1210+ /// let task = index.reset_faceting().await.unwrap();
1211+ /// # index.delete().await.unwrap().wait_for_completion(&client, None, None).await.unwrap();
1212+ /// # });
1213+ /// ```
1214+ pub async fn reset_faceting ( & self ) -> Result < TaskInfo , Error > {
1215+ request :: < ( ) , TaskInfo > (
1216+ & format ! (
1217+ "{}/indexes/{}/settings/faceting" ,
1218+ self . client. host, self . uid
1219+ ) ,
1220+ & self . client . api_key ,
1221+ Method :: Delete ,
1222+ 202 ,
1223+ )
1224+ . await
1225+ }
1226+ }
1227+
1228+
1229+ #[ cfg( test) ]
1230+ mod tests {
1231+ use super :: * ;
1232+
1233+ use crate :: client:: * ;
1234+ use meilisearch_test_macro:: meilisearch_test;
1235+
1236+ #[ meilisearch_test]
1237+ async fn test_set_faceting_settings ( client : Client , index : Index ) {
1238+ let faceting = FacetingSettings {
1239+ max_values_per_facet : 5 ,
1240+ } ;
1241+ let settings = Settings :: new ( )
1242+ . with_faceting ( & faceting) ;
1243+
1244+ let task_info = index. set_settings ( & settings) . await . unwrap ( ) ;
1245+ client. wait_for_task ( task_info, None , None ) . await . unwrap ( ) ;
1246+
1247+ let res = index. get_faceting ( ) . await . unwrap ( ) ;
1248+
1249+ assert_eq ! ( faceting, res) ;
1250+ }
1251+
1252+ #[ meilisearch_test]
1253+ async fn test_get_faceting ( index : Index ) {
1254+ let res = index. get_faceting ( ) . await . unwrap ( ) ;
1255+ let faceting = FacetingSettings {
1256+ max_values_per_facet : 100 ,
1257+ } ;
1258+
1259+ assert_eq ! ( faceting, res) ;
1260+ }
1261+
1262+ #[ meilisearch_test]
1263+ async fn test_set_faceting ( client : Client , index : Index ) {
1264+ let faceting = FacetingSettings {
1265+ max_values_per_facet : 5 ,
1266+ } ;
1267+ let task_info = index. set_faceting ( & faceting) . await . unwrap ( ) ;
1268+ client. wait_for_task ( task_info, None , None ) . await . unwrap ( ) ;
1269+
1270+ let res = index. get_faceting ( ) . await . unwrap ( ) ;
1271+
1272+ assert_eq ! ( faceting, res) ;
1273+ }
1274+
1275+ #[ meilisearch_test]
1276+ async fn test_reset_faceting ( client : Client , index : Index ) {
1277+ let task_info = index. reset_faceting ( ) . await . unwrap ( ) ;
1278+ client. wait_for_task ( task_info, None , None ) . await . unwrap ( ) ;
1279+
1280+ let faceting = FacetingSettings {
1281+ max_values_per_facet : 100 ,
1282+ } ;
1283+ let res = index. get_faceting ( ) . await . unwrap ( ) ;
1284+
1285+ assert_eq ! ( faceting, res) ;
1286+ }
11051287}
0 commit comments