Skip to content

Commit eacc436

Browse files
committed
Add faceting index settings methods
1 parent 79642ad commit eacc436

File tree

2 files changed

+226
-0
lines changed

2 files changed

+226
-0
lines changed

.code-samples.meilisearch.yaml

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,28 @@ reset_displayed_attributes_1: |-
358358
.reset_displayed_attributes()
359359
.await
360360
.unwrap();
361+
get_faceting_settings_1: |-
362+
let faceting: FacetingSettings = client
363+
.index("books")
364+
.get_faceting()
365+
.await
366+
.unwrap();
367+
update_faceting_settings_1: |-
368+
let mut faceting = FacetingSettings {
369+
max_values_per_facet: 2,
370+
};
371+
372+
let task: TaskInfo = client
373+
.index("books")
374+
.set_faceting(&faceting)
375+
.await
376+
.unwrap();
377+
reset_faceting_settings_1: |-
378+
let task: TaskInfo = client
379+
.index("books")
380+
.reset_faceting()
381+
.await
382+
.unwrap();
361383
get_index_stats_1: |-
362384
let stats: IndexStats = client
363385
.index("movies")
@@ -661,6 +683,18 @@ settings_guide_sortable_1: |-
661683
.set_settings(&settings)
662684
.await
663685
.unwrap();
686+
settings_guide_faceting_1: |-
687+
let faceting = FacetingSettings {
688+
max_values_per_facet: 5,
689+
};
690+
let settings = Settings::new()
691+
.with_faceting(&faceting);
692+
693+
let task: TaskInfo = client
694+
.index("movies")
695+
.set_settings(&settings)
696+
.await
697+
.unwrap();
664698
add_movies_json_1: |-
665699
use meilisearch_sdk::{
666700
indexes::*,
@@ -987,6 +1021,16 @@ getting_started_sorting: |-
9871021
.execute()
9881022
.await
9891023
.unwrap();
1024+
getting_started_faceting: |-
1025+
let mut faceting = FacetingSettings {
1026+
max_values_per_facet: 2,
1027+
};
1028+
1029+
let task: TaskInfo = client
1030+
.index("movies")
1031+
.set_faceting(&faceting)
1032+
.await
1033+
.unwrap();
9901034
getting_started_filtering: |-
9911035
let results: SearchResults<Meteorite> = client
9921036
.index("meteorites")

src/settings.rs

Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,13 @@ use crate::{
77
use serde::{Deserialize, Serialize};
88
use std::collections::HashMap;
99

10+
#[derive(Serialize, Deserialize, Default, Debug, Clone, 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

198219
impl 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: Some(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

Comments
 (0)