Skip to content

Commit e53f80f

Browse files
committed
Allow retrieving the ranking score in Search API
1 parent 241e797 commit e53f80f

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

.code-samples.meilisearch.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -803,6 +803,15 @@ search_parameter_guide_show_matches_position_1: |-
803803
.iter()
804804
.map(|r| r.matches_position.as_ref().unwrap())
805805
.collect();
806+
search_parameter_guide_show_ranking_score_1: |-
807+
let results: SearchResults<Movie> = client
808+
.index("movies")
809+
.search()
810+
.with_query("dragon")
811+
.with_show_ranking_score(true)
812+
.execute()
813+
.await
814+
.unwrap();
806815
search_parameter_guide_matching_strategy_1: |-
807816
let results: SearchResults<Movie> = client
808817
.index("movies")

src/search.rs

Lines changed: 29 additions & 0 deletions
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)]
@@ -302,6 +305,12 @@ pub struct SearchQuery<'a> {
302305
#[serde(skip_serializing_if = "Option::is_none")]
303306
pub show_matches_position: Option<bool>,
304307

308+
/// Defines whether to show the relevancy score of the match.
309+
///
310+
/// **Default: `false`**
311+
#[serde(skip_serializing_if = "Option::is_none")]
312+
pub show_ranking_score: Option<bool>,
313+
305314
/// Defines the strategy on how to handle queries containing multiple words.
306315
#[serde(skip_serializing_if = "Option::is_none")]
307316
pub matching_strategy: Option<MatchingStrategies>,
@@ -331,6 +340,7 @@ impl<'a> SearchQuery<'a> {
331340
highlight_pre_tag: None,
332341
highlight_post_tag: None,
333342
show_matches_position: None,
343+
show_ranking_score: None,
334344
matching_strategy: None,
335345
index_uid: None,
336346
}
@@ -480,6 +490,13 @@ impl<'a> SearchQuery<'a> {
480490
self.show_matches_position = Some(show_matches_position);
481491
self
482492
}
493+
pub fn with_show_ranking_score<'b>(
494+
&'b mut self,
495+
show_ranking_score: bool,
496+
) -> &'b mut SearchQuery<'a> {
497+
self.show_ranking_score = Some(show_ranking_score);
498+
self
499+
}
483500
pub fn with_matching_strategy<'b>(
484501
&'b mut self,
485502
matching_strategy: MatchingStrategies,
@@ -1027,6 +1044,18 @@ mod tests {
10271044
Ok(())
10281045
}
10291046

1047+
#[meilisearch_test]
1048+
async fn test_query_show_ranking_score(client: Client, index: Index) -> Result<(), Error> {
1049+
setup_test_index(&client, &index).await?;
1050+
1051+
let mut query = SearchQuery::new(&index);
1052+
query.with_query("dolor text");
1053+
query.with_show_ranking_score(true);
1054+
let results: SearchResults<Document> = index.execute_query(&query).await?;
1055+
assert!(results.hits[0].ranking_score.is_some());
1056+
Ok(())
1057+
}
1058+
10301059
#[meilisearch_test]
10311060
async fn test_phrase_search(client: Client, index: Index) -> Result<(), Error> {
10321061
setup_test_index(&client, &index).await?;

0 commit comments

Comments
 (0)