diff --git a/.code-samples.meilisearch.yaml b/.code-samples.meilisearch.yaml index b3647aab..e2997571 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(); diff --git a/src/search.rs b/src/search.rs index d82e7b59..f56c86b0 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() } @@ -1182,6 +1191,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,