From 0adffbf4cc3ce4c6798f012e51b7e9c49159ad48 Mon Sep 17 00:00:00 2001 From: NoodleSamaChan Date: Tue, 2 Jul 2024 15:19:39 +0200 Subject: [PATCH 1/4] added distinct to search settings --- src/search.rs | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/search.rs b/src/search.rs index d82e7b59..f130ebda 100644 --- a/src/search.rs +++ b/src/search.rs @@ -338,6 +338,10 @@ pub struct SearchQuery<'a, Http: HttpClient> { #[serde(skip_serializing_if = "Option::is_none")] pub(crate) index_uid: Option<&'a str>, + + ///Defines one attribute in the filterableAttributes list as a distinct attribute. + #[serde(skip_serializing_if = "Option::is_none")] + pub(crate) distinct: Option<&'a str>, } #[allow(missing_docs)] @@ -367,6 +371,7 @@ impl<'a, Http: HttpClient> SearchQuery<'a, Http> { show_ranking_score_details: None, matching_strategy: None, index_uid: None, + distinct: None, } } pub fn with_query<'b>(&'b mut self, query: &'a str) -> &'b mut SearchQuery<'a, Http> { @@ -559,6 +564,10 @@ impl<'a, Http: HttpClient> SearchQuery<'a, Http> { self.index_uid = Some(&self.index.uid); self } + pub fn with_distinct<'b>(&'b mut self, distinct: &'a str) -> &'b mut SearchQuery<'a, Http> { + self.distinct = Some(distinct); + self + } pub fn build(&mut self) -> SearchQuery<'a, Http> { self.clone() } @@ -632,6 +641,7 @@ mod tests { use meilisearch_test_macro::meilisearch_test; use serde::{Deserialize, Serialize}; use serde_json::{json, Map, Value}; + use time::format_description::well_known::iso8601::DateKind; #[derive(Debug, Serialize, Deserialize, PartialEq)] struct Nested { @@ -1182,6 +1192,20 @@ mod tests { Ok(()) } + #[meilisearch_test] + async fn test_distinct(client: Client, index: Index) -> Result<(), Error> { + setup_test_index(&client, &index).await?; + + let results = SearchQuery::new(&index) + .with_distinct("kind") + .execute::() + .await + .unwrap(); + + assert_eq!(results.hits.len(), 2); + Ok(()) + } + #[meilisearch_test] async fn test_generate_tenant_token_from_client( client: Client, From 9eb821bf87781666fd7a53439ba358fe2a4aca42 Mon Sep 17 00:00:00 2001 From: NoodleSamaChan Date: Tue, 2 Jul 2024 16:30:41 +0200 Subject: [PATCH 2/4] added distincts tests in yaml file --- .code-samples.meilisearch.yaml | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/.code-samples.meilisearch.yaml b/.code-samples.meilisearch.yaml index b3647aab..5c81a321 100644 --- a/.code-samples.meilisearch.yaml +++ b/.code-samples.meilisearch.yaml @@ -1704,3 +1704,29 @@ create_snapshot_1: |- .create_snapshot() .await .unwrap(); +search_parameter_reference_distinct_1: |- + let res = client + .index("INDEX_NAME") + .search() + .with_query("QUERY TERMS") + .with_distinct("ATTRIBUTE_A") + .execute() + .await + .unwrap(); +distinct_attribute_guide_filterable_1: |- + let task: TaskInfo = client + .index("products") + .settings() + .set_filterable_attributes(["product_id", "sku", "url"]) + .execute() + .await + .unwrap(); +distinct_attribute_guide_distinct_parameter_1: |- + let res = client + .index("products") + .search() + .with_query("white shirt") + .with_distinct("sku") + .execute() + .await + .unwrap(); \ No newline at end of file From 2af679dca15f2a242118925b205243d57ab9a23f Mon Sep 17 00:00:00 2001 From: NoodleSamaChan Date: Tue, 2 Jul 2024 16:40:46 +0200 Subject: [PATCH 3/4] added entry to yaml file --- .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 5c81a321..e2997571 100644 --- a/.code-samples.meilisearch.yaml +++ b/.code-samples.meilisearch.yaml @@ -1729,4 +1729,4 @@ distinct_attribute_guide_distinct_parameter_1: |- .with_distinct("sku") .execute() .await - .unwrap(); \ No newline at end of file + .unwrap(); From 693156f55ba557588517ed9dbb1e1ca9ea6f6c7a Mon Sep 17 00:00:00 2001 From: NoodleSamaChan <150461421+NoodleSamaChan@users.noreply.github.com> Date: Tue, 2 Jul 2024 16:47:15 +0200 Subject: [PATCH 4/4] Update src/search.rs Co-authored-by: Tamo --- src/search.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/search.rs b/src/search.rs index f130ebda..f56c86b0 100644 --- a/src/search.rs +++ b/src/search.rs @@ -641,7 +641,6 @@ mod tests { use meilisearch_test_macro::meilisearch_test; use serde::{Deserialize, Serialize}; use serde_json::{json, Map, Value}; - use time::format_description::well_known::iso8601::DateKind; #[derive(Debug, Serialize, Deserialize, PartialEq)] struct Nested {