Skip to content

Commit bfae87b

Browse files
Merge #599
599: Accept distinct search parameter r=irevoire a=NoodleSamaChan # Pull Request ## Related issue #597 ## What does this PR do? #597 ## PR checklist Please check if your PR fulfills the following requirements: - [x] Does this PR fix an existing issue, or have you listed the changes applied in the PR description (and why they are needed)? - [x] Have you read the contributing guidelines? - [x] Have you made sure that the title is accurate and descriptive of the changes? Thank you so much for contributing to Meilisearch! Co-authored-by: NoodleSamaChan <[email protected]> Co-authored-by: NoodleSamaChan <[email protected]>
2 parents 3d15605 + 693156f commit bfae87b

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

.code-samples.meilisearch.yaml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1704,3 +1704,29 @@ create_snapshot_1: |-
17041704
.create_snapshot()
17051705
.await
17061706
.unwrap();
1707+
search_parameter_reference_distinct_1: |-
1708+
let res = client
1709+
.index("INDEX_NAME")
1710+
.search()
1711+
.with_query("QUERY TERMS")
1712+
.with_distinct("ATTRIBUTE_A")
1713+
.execute()
1714+
.await
1715+
.unwrap();
1716+
distinct_attribute_guide_filterable_1: |-
1717+
let task: TaskInfo = client
1718+
.index("products")
1719+
.settings()
1720+
.set_filterable_attributes(["product_id", "sku", "url"])
1721+
.execute()
1722+
.await
1723+
.unwrap();
1724+
distinct_attribute_guide_distinct_parameter_1: |-
1725+
let res = client
1726+
.index("products")
1727+
.search()
1728+
.with_query("white shirt")
1729+
.with_distinct("sku")
1730+
.execute()
1731+
.await
1732+
.unwrap();

src/search.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,10 @@ pub struct SearchQuery<'a, Http: HttpClient> {
338338

339339
#[serde(skip_serializing_if = "Option::is_none")]
340340
pub(crate) index_uid: Option<&'a str>,
341+
342+
///Defines one attribute in the filterableAttributes list as a distinct attribute.
343+
#[serde(skip_serializing_if = "Option::is_none")]
344+
pub(crate) distinct: Option<&'a str>,
341345
}
342346

343347
#[allow(missing_docs)]
@@ -367,6 +371,7 @@ impl<'a, Http: HttpClient> SearchQuery<'a, Http> {
367371
show_ranking_score_details: None,
368372
matching_strategy: None,
369373
index_uid: None,
374+
distinct: None,
370375
}
371376
}
372377
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> {
559564
self.index_uid = Some(&self.index.uid);
560565
self
561566
}
567+
pub fn with_distinct<'b>(&'b mut self, distinct: &'a str) -> &'b mut SearchQuery<'a, Http> {
568+
self.distinct = Some(distinct);
569+
self
570+
}
562571
pub fn build(&mut self) -> SearchQuery<'a, Http> {
563572
self.clone()
564573
}
@@ -1182,6 +1191,20 @@ mod tests {
11821191
Ok(())
11831192
}
11841193

1194+
#[meilisearch_test]
1195+
async fn test_distinct(client: Client, index: Index) -> Result<(), Error> {
1196+
setup_test_index(&client, &index).await?;
1197+
1198+
let results = SearchQuery::new(&index)
1199+
.with_distinct("kind")
1200+
.execute::<Document>()
1201+
.await
1202+
.unwrap();
1203+
1204+
assert_eq!(results.hits.len(), 2);
1205+
Ok(())
1206+
}
1207+
11851208
#[meilisearch_test]
11861209
async fn test_generate_tenant_token_from_client(
11871210
client: Client,

0 commit comments

Comments
 (0)