Skip to content

Commit

Permalink
Merge #599
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
3 people authored Jul 2, 2024
2 parents 3d15605 + 693156f commit bfae87b
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
26 changes: 26 additions & 0 deletions .code-samples.meilisearch.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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();
23 changes: 23 additions & 0 deletions src/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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> {
Expand Down Expand Up @@ -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()
}
Expand Down Expand Up @@ -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::<Document>()
.await
.unwrap();

assert_eq!(results.hits.len(), 2);
Ok(())
}

#[meilisearch_test]
async fn test_generate_tenant_token_from_client(
client: Client,
Expand Down

0 comments on commit bfae87b

Please sign in to comment.