Skip to content

Commit da5f05d

Browse files
Merge #515
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]>
2 parents 7dd3bd2 + e53f80f commit da5f05d

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

.code-samples.meilisearch.yaml

+9
Original file line numberDiff line numberDiff line change
@@ -812,6 +812,15 @@ search_parameter_guide_show_matches_position_1: |-
812812
.iter()
813813
.map(|r| r.matches_position.as_ref().unwrap())
814814
.collect();
815+
search_parameter_guide_show_ranking_score_1: |-
816+
let results: SearchResults<Movie> = client
817+
.index("movies")
818+
.search()
819+
.with_query("dragon")
820+
.with_show_ranking_score(true)
821+
.execute()
822+
.await
823+
.unwrap();
815824
search_parameter_guide_matching_strategy_1: |-
816825
let results: SearchResults<Movie> = client
817826
.index("movies")

src/search.rs

+29
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ pub struct SearchResult<T> {
4545
/// The object that contains information about the matches.
4646
#[serde(rename = "_matchesPosition")]
4747
pub matches_position: Option<HashMap<String, Vec<MatchRange>>>,
48+
/// The relevancy score of the match.
49+
#[serde(rename = "_rankingScore")]
50+
pub ranking_score: Option<f64>,
4851
}
4952

5053
#[derive(Deserialize, Debug, Clone)]
@@ -309,6 +312,12 @@ pub struct SearchQuery<'a> {
309312
#[serde(skip_serializing_if = "Option::is_none")]
310313
pub show_matches_position: Option<bool>,
311314

315+
/// Defines whether to show the relevancy score of the match.
316+
///
317+
/// **Default: `false`**
318+
#[serde(skip_serializing_if = "Option::is_none")]
319+
pub show_ranking_score: Option<bool>,
320+
312321
/// Defines the strategy on how to handle queries containing multiple words.
313322
#[serde(skip_serializing_if = "Option::is_none")]
314323
pub matching_strategy: Option<MatchingStrategies>,
@@ -339,6 +348,7 @@ impl<'a> SearchQuery<'a> {
339348
highlight_pre_tag: None,
340349
highlight_post_tag: None,
341350
show_matches_position: None,
351+
show_ranking_score: None,
342352
matching_strategy: None,
343353
index_uid: None,
344354
}
@@ -495,6 +505,13 @@ impl<'a> SearchQuery<'a> {
495505
self.show_matches_position = Some(show_matches_position);
496506
self
497507
}
508+
pub fn with_show_ranking_score<'b>(
509+
&'b mut self,
510+
show_ranking_score: bool,
511+
) -> &'b mut SearchQuery<'a> {
512+
self.show_ranking_score = Some(show_ranking_score);
513+
self
514+
}
498515
pub fn with_matching_strategy<'b>(
499516
&'b mut self,
500517
matching_strategy: MatchingStrategies,
@@ -1056,6 +1073,18 @@ mod tests {
10561073
Ok(())
10571074
}
10581075

1076+
#[meilisearch_test]
1077+
async fn test_query_show_ranking_score(client: Client, index: Index) -> Result<(), Error> {
1078+
setup_test_index(&client, &index).await?;
1079+
1080+
let mut query = SearchQuery::new(&index);
1081+
query.with_query("dolor text");
1082+
query.with_show_ranking_score(true);
1083+
let results: SearchResults<Document> = index.execute_query(&query).await?;
1084+
assert!(results.hits[0].ranking_score.is_some());
1085+
Ok(())
1086+
}
1087+
10591088
#[meilisearch_test]
10601089
async fn test_phrase_search(client: Client, index: Index) -> Result<(), Error> {
10611090
setup_test_index(&client, &index).await?;

0 commit comments

Comments
 (0)