Skip to content

Commit

Permalink
Merge #515
Browse files Browse the repository at this point in the history
515: Allow retrieving the ranking score in Search API r=curquiza a=hmacr

# Pull Request

## Related issue
Fixes #500 

## What does this PR do?
- Fetch ranking score in Search API

## 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: hmacr <[email protected]>
  • Loading branch information
meili-bors[bot] and hmacr committed Sep 12, 2023
2 parents 7dd3bd2 + e53f80f commit da5f05d
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
9 changes: 9 additions & 0 deletions .code-samples.meilisearch.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -812,6 +812,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 @@ -309,6 +312,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 @@ -339,6 +348,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 @@ -495,6 +505,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 @@ -1056,6 +1073,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

0 comments on commit da5f05d

Please sign in to comment.