Skip to content

Allow retrieving the ranking score in Search API #515

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

Merged
merged 1 commit into from
Sep 12, 2023
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 @@ -803,6 +803,15 @@ search_parameter_guide_show_matches_position_1: |-
.iter()
.map(|r| r.matches_position.as_ref().unwrap())
.collect();
search_parameter_guide_show_ranking_score_1: |-
let results: SearchResults<Movie> = client
.index("movies")
.search()
.with_query("dragon")
.with_show_ranking_score(true)
.execute()
.await
.unwrap();
search_parameter_guide_matching_strategy_1: |-
let results: SearchResults<Movie> = client
.index("movies")
Expand Down
29 changes: 29 additions & 0 deletions src/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ pub struct SearchResult<T> {
/// The object that contains information about the matches.
#[serde(rename = "_matchesPosition")]
pub matches_position: Option<HashMap<String, Vec<MatchRange>>>,
/// The relevancy score of the match.
#[serde(rename = "_rankingScore")]
pub ranking_score: Option<f64>,
}

#[derive(Deserialize, Debug, Clone)]
Expand Down Expand Up @@ -302,6 +305,12 @@ pub struct SearchQuery<'a> {
#[serde(skip_serializing_if = "Option::is_none")]
pub show_matches_position: Option<bool>,

/// Defines whether to show the relevancy score of the match.
///
/// **Default: `false`**
#[serde(skip_serializing_if = "Option::is_none")]
pub show_ranking_score: Option<bool>,

/// Defines the strategy on how to handle queries containing multiple words.
#[serde(skip_serializing_if = "Option::is_none")]
pub matching_strategy: Option<MatchingStrategies>,
Expand Down Expand Up @@ -331,6 +340,7 @@ impl<'a> SearchQuery<'a> {
highlight_pre_tag: None,
highlight_post_tag: None,
show_matches_position: None,
show_ranking_score: None,
matching_strategy: None,
index_uid: None,
}
Expand Down Expand Up @@ -480,6 +490,13 @@ impl<'a> SearchQuery<'a> {
self.show_matches_position = Some(show_matches_position);
self
}
pub fn with_show_ranking_score<'b>(
&'b mut self,
show_ranking_score: bool,
) -> &'b mut SearchQuery<'a> {
self.show_ranking_score = Some(show_ranking_score);
self
}
pub fn with_matching_strategy<'b>(
&'b mut self,
matching_strategy: MatchingStrategies,
Expand Down Expand Up @@ -1027,6 +1044,18 @@ mod tests {
Ok(())
}

#[meilisearch_test]
async fn test_query_show_ranking_score(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_show_ranking_score(true);
let results: SearchResults<Document> = index.execute_query(&query).await?;
assert!(results.hits[0].ranking_score.is_some());
Ok(())
}

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