Skip to content

Add faceting index settings methods #344

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 5, 2022
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
44 changes: 44 additions & 0 deletions .code-samples.meilisearch.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,28 @@ reset_displayed_attributes_1: |-
.reset_displayed_attributes()
.await
.unwrap();
get_faceting_settings_1: |-
let faceting: FacetingSettings = client
.index("books")
.get_faceting()
.await
.unwrap();
update_faceting_settings_1: |-
let mut faceting = FacetingSettings {
max_values_per_facet: 2,
};

let task: TaskInfo = client
.index("books")
.set_faceting(&faceting)
.await
.unwrap();
reset_faceting_settings_1: |-
let task: TaskInfo = client
.index("books")
.reset_faceting()
.await
.unwrap();
get_index_stats_1: |-
let stats: IndexStats = client
.index("movies")
Expand Down Expand Up @@ -661,6 +683,18 @@ settings_guide_sortable_1: |-
.set_settings(&settings)
.await
.unwrap();
settings_guide_faceting_1: |-
let faceting = FacetingSettings {
max_values_per_facet: 5,
};
let settings = Settings::new()
.with_faceting(&faceting);

let task: TaskInfo = client
.index("movies")
.set_settings(&settings)
.await
.unwrap();
add_movies_json_1: |-
use meilisearch_sdk::{
indexes::*,
Expand Down Expand Up @@ -987,6 +1021,16 @@ getting_started_sorting: |-
.execute()
.await
.unwrap();
getting_started_faceting: |-
let mut faceting = FacetingSettings {
max_values_per_facet: 2,
};

let task: TaskInfo = client
.index("movies")
.set_faceting(&faceting)
.await
.unwrap();
getting_started_filtering: |-
let results: SearchResults<Meteorite> = client
.index("meteorites")
Expand Down
183 changes: 183 additions & 0 deletions src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ use crate::{
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

#[derive(Serialize, Deserialize, Default, Debug, Clone, Eq, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct FacetingSettings {
#[serde()]
pub max_values_per_facet: usize,
}

/// Struct reprensenting a set of settings.
/// You can build this struct using the builder syntax.
///
Expand Down Expand Up @@ -58,6 +65,9 @@ pub struct Settings {
/// Fields displayed in the returned documents
#[serde(skip_serializing_if = "Option::is_none")]
pub displayed_attributes: Option<Vec<String>>,
/// Faceting settings
#[serde(skip_serializing_if = "Option::is_none")]
pub faceting: Option<FacetingSettings>,
}

#[allow(missing_docs)]
Expand All @@ -73,6 +83,7 @@ impl Settings {
distinct_attribute: None,
searchable_attributes: None,
displayed_attributes: None,
faceting: None,
}
}
pub fn with_synonyms<S, U, V>(self, synonyms: HashMap<S, U>) -> Settings
Expand Down Expand Up @@ -193,6 +204,16 @@ impl Settings {
..self
}
}

pub fn with_faceting(
self,
faceting: &FacetingSettings,
) -> Settings {
Settings {
faceting: Some(faceting.clone()),
..self
}
}
}

impl Index {
Expand Down Expand Up @@ -454,6 +475,35 @@ impl Index {
.await
}

/// Get [faceting](https://docs.meilisearch.com/reference/api/settings.html#faceting) settings of the [Index].
///
/// ```
/// # use meilisearch_sdk::{client::*, indexes::*};
/// #
/// # let MEILISEARCH_HOST = option_env!("MEILISEARCH_HOST").unwrap_or("http://localhost:7700");
/// # let MEILISEARCH_API_KEY = option_env!("MEILISEARCH_API_KEY").unwrap_or("masterKey");
/// #
/// # futures::executor::block_on(async move {
/// let client = Client::new(MEILISEARCH_HOST, MEILISEARCH_API_KEY);
/// # client.create_index("get_faceting", None).await.unwrap().wait_for_completion(&client, None, None).await.unwrap();
/// let index = client.index("get_faceting");
/// let faceting = index.get_faceting().await.unwrap();
/// # index.delete().await.unwrap().wait_for_completion(&client, None, None).await.unwrap();
/// # });
/// ```
pub async fn get_faceting(&self) -> Result<FacetingSettings, Error> {
request::<(), FacetingSettings>(
&format!(
"{}/indexes/{}/settings/faceting",
self.client.host, self.uid
),
&self.client.api_key,
Method::Get(()),
200,
)
.await
}

/// Update [settings](../settings/struct.Settings.html) of the [Index].
/// Updates in the settings are partial. This means that any parameters corresponding to a `None` value will be left unchanged.
///
Expand Down Expand Up @@ -816,6 +866,45 @@ impl Index {
.await
}

/// Update [faceting](https://docs.meilisearch.com/reference/api/settings.html#faceting) settings of the [Index].
///
/// # Example
///
/// ```
/// # use meilisearch_sdk::{client::*, indexes::*, settings::Settings, settings::FacetingSettings};
/// #
/// # let MEILISEARCH_HOST = option_env!("MEILISEARCH_HOST").unwrap_or("http://localhost:7700");
/// # let MEILISEARCH_API_KEY = option_env!("MEILISEARCH_API_KEY").unwrap_or("masterKey");
/// #
/// # futures::executor::block_on(async move {
/// let client = Client::new(MEILISEARCH_HOST, MEILISEARCH_API_KEY);
/// # client.create_index("set_faceting", None).await.unwrap().wait_for_completion(&client, None, None).await.unwrap();
/// let mut index = client.index("set_faceting");
///
/// let mut faceting = FacetingSettings {
/// max_values_per_facet: 12,
/// };
///
/// let task = index.set_faceting(&faceting).await.unwrap();
/// # index.delete().await.unwrap().wait_for_completion(&client, None, None).await.unwrap();
/// # });
/// ```
pub async fn set_faceting(
&self,
faceting: &FacetingSettings,
) -> Result<TaskInfo, Error> {
request::<&FacetingSettings, TaskInfo>(
&format!(
"{}/indexes/{}/settings/faceting",
self.client.host, self.uid
),
&self.client.api_key,
Method::Patch(faceting),
202,
)
.await
}

/// Reset [Settings] of the [Index].
/// All settings will be reset to their [default value](https://docs.meilisearch.com/reference/api/settings.html#reset-settings).
///
Expand Down Expand Up @@ -1102,4 +1191,98 @@ impl Index {
)
.await
}

/// Reset [faceting](https://docs.meilisearch.com/reference/api/settings.html#faceting) settings of the [Index].
///
/// # Example
///
/// ```
/// # use meilisearch_sdk::{client::*, indexes::*, settings::Settings};
/// #
/// # let MEILISEARCH_HOST = option_env!("MEILISEARCH_HOST").unwrap_or("http://localhost:7700");
/// # let MEILISEARCH_API_KEY = option_env!("MEILISEARCH_API_KEY").unwrap_or("masterKey");
/// #
/// # futures::executor::block_on(async move {
/// let client = Client::new(MEILISEARCH_HOST, MEILISEARCH_API_KEY);
/// # client.create_index("reset_faceting", None).await.unwrap().wait_for_completion(&client, None, None).await.unwrap();
/// let mut index = client.index("reset_faceting");
///
/// let task = index.reset_faceting().await.unwrap();
/// # index.delete().await.unwrap().wait_for_completion(&client, None, None).await.unwrap();
/// # });
/// ```
pub async fn reset_faceting(&self) -> Result<TaskInfo, Error> {
request::<(), TaskInfo>(
&format!(
"{}/indexes/{}/settings/faceting",
self.client.host, self.uid
),
&self.client.api_key,
Method::Delete,
202,
)
.await
}
}


#[cfg(test)]
mod tests {
use super::*;

use crate::client::*;
use meilisearch_test_macro::meilisearch_test;

#[meilisearch_test]
async fn test_set_faceting_settings(client: Client, index: Index) {
let faceting = FacetingSettings {
max_values_per_facet: 5,
};
let settings = Settings::new()
.with_faceting(&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);
}

#[meilisearch_test]
async fn test_get_faceting(index: Index) {
let faceting = FacetingSettings {
max_values_per_facet: 100,
};

let res = index.get_faceting().await.unwrap();

assert_eq!(faceting, res);
}

#[meilisearch_test]
async fn test_set_faceting(client: Client, index: Index) {
let faceting = FacetingSettings {
max_values_per_facet: 5,
};
let task_info = index.set_faceting(&faceting).await.unwrap();
client.wait_for_task(task_info, None, None).await.unwrap();

let res = index.get_faceting().await.unwrap();

assert_eq!(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 {
max_values_per_facet: 100,
};

let res = index.get_faceting().await.unwrap();

assert_eq!(faceting, res);
}
}