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

Support ranking score threshold #601

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
9 changes: 9 additions & 0 deletions .code-samples.meilisearch.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1730,3 +1730,12 @@ distinct_attribute_guide_distinct_parameter_1: |-
.execute()
.await
.unwrap();
search_parameter_reference_ranking_score_threshold_1: |-
let res = client
.index("INDEX_NAME")
.search()
.with_query("badman")
.with_ranking_score_threshold(0.2)
.execute()
.await
.unwrap();
33 changes: 30 additions & 3 deletions src/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -336,12 +336,16 @@ pub struct SearchQuery<'a, Http: HttpClient> {
#[serde(skip_serializing_if = "Option::is_none")]
pub matching_strategy: Option<MatchingStrategies>,

///Defines one attribute in the filterableAttributes list as a distinct attribute.
#[serde(skip_serializing_if = "Option::is_none")]
pub(crate) index_uid: Option<&'a str>,
pub distinct: Option<&'a str>,

///Defines one attribute in the filterableAttributes list as a distinct attribute.
///Excludes results below the specified ranking score.
#[serde(skip_serializing_if = "Option::is_none")]
pub(crate) distinct: Option<&'a str>,
pub ranking_score_threshold: Option<f64>,

#[serde(skip_serializing_if = "Option::is_none")]
pub(crate) index_uid: Option<&'a str>,
}

#[allow(missing_docs)]
Expand Down Expand Up @@ -372,6 +376,7 @@ impl<'a, Http: HttpClient> SearchQuery<'a, Http> {
matching_strategy: None,
index_uid: None,
distinct: None,
ranking_score_threshold: None,
}
}
pub fn with_query<'b>(&'b mut self, query: &'a str) -> &'b mut SearchQuery<'a, Http> {
Expand Down Expand Up @@ -568,6 +573,13 @@ impl<'a, Http: HttpClient> SearchQuery<'a, Http> {
self.distinct = Some(distinct);
self
}
pub fn with_ranking_score_threshold<'b>(
&'b mut self,
ranking_score_threshold: f64,
) -> &'b mut SearchQuery<'a, Http> {
self.ranking_score_threshold = Some(ranking_score_threshold);
self
}
pub fn build(&mut self) -> SearchQuery<'a, Http> {
self.clone()
}
Expand Down Expand Up @@ -1134,6 +1146,21 @@ mod tests {
Ok(())
}

#[meilisearch_test]
async fn test_query_show_ranking_score_threshold(
client: Client,
index: Index,
) -> Result<(), Error> {
setup_test_index(&client, &index).await?;

let mut query = SearchQuery::new(&index);
query.with_query("dolor text");
query.with_ranking_score_threshold(1.0);
let results: SearchResults<Document> = index.execute_query(&query).await.unwrap();
assert!(results.hits.is_empty());
Ok(())
}

#[meilisearch_test]
async fn test_phrase_search(client: Client, index: Index) -> Result<(), Error> {
setup_test_index(&client, &index).await?;
Expand Down
Loading