Skip to content
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

Allow users to customize facet value sort behaviour #514

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
21 changes: 21 additions & 0 deletions .code-samples.meilisearch.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -1656,3 +1663,17 @@ multi_search_1: |-
.execute::<MovieRatings>()
.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();
123 changes: 110 additions & 13 deletions src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand All @@ -28,10 +28,20 @@ pub struct TypoToleranceSettings {
pub min_word_size_for_typos: Option<MinWordSizeForTypos>,
}

#[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,
Comment on lines +36 to +38
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
ALPHA,
#[serde(rename = "count")]
COUNT,
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<BTreeMap<String, FacetSortValue>>,
}

/// Struct reprensenting a set of settings.
Expand Down Expand Up @@ -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
}
}
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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]
Expand Down