Skip to content

Commit 3294636

Browse files
authored
Merge pull request #765 from aznszn/feat/add-showPerformanceDetails
Added `show_performance_details` option to search, multi-search, federated multi-search and similar
2 parents ae1b906 + a20e4a9 commit 3294636

File tree

2 files changed

+125
-0
lines changed

2 files changed

+125
-0
lines changed

src/search.rs

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,9 @@ pub struct SearchResults<T> {
135135
skip_serializing_if = "Option::is_none"
136136
)]
137137
pub query_vector: Option<Vec<f32>>,
138+
/// The search query's performance trace.
139+
/// Returned when `showPerformanceDetails` is enabled.
140+
pub performance_details: Option<Value>,
138141
}
139142

140143
fn serialize_attributes_to_crop_with_wildcard<S: Serializer>(
@@ -424,6 +427,12 @@ pub struct SearchQuery<'a, Http: HttpClient> {
424427

425428
#[serde(skip_serializing_if = "Option::is_none")]
426429
pub(crate) federation_options: Option<QueryFederationOptions>,
430+
431+
/// Defines whether to return performance trace.
432+
///
433+
/// **Default: `false`**
434+
#[serde(skip_serializing_if = "Option::is_none")]
435+
pub show_performance_details: Option<bool>,
427436
}
428437

429438
#[derive(Debug, Serialize, Clone)]
@@ -473,6 +482,7 @@ impl<'a, Http: HttpClient> SearchQuery<'a, Http> {
473482
ranking_score_threshold: None,
474483
locales: None,
475484
federation_options: None,
485+
show_performance_details: None,
476486
}
477487
}
478488

@@ -752,6 +762,15 @@ impl<'a, Http: HttpClient> SearchQuery<'a, Http> {
752762
self
753763
}
754764

765+
/// Request performance details in the response.
766+
pub fn with_show_performance_details<'b>(
767+
&'b mut self,
768+
show_performance_details: bool,
769+
) -> &'b mut SearchQuery<'a, Http> {
770+
self.show_performance_details = Some(show_performance_details);
771+
self
772+
}
773+
755774
/// Execute the query and fetch the results.
756775
pub async fn execute<T: 'static + DeserializeOwned + Send + Sync>(
757776
&'a self,
@@ -880,6 +899,10 @@ pub struct FederationOptions {
880899
/// Request to merge the facets to enforce a maximum number of values per facet.
881900
#[serde(skip_serializing_if = "Option::is_none")]
882901
pub merge_facets: Option<MergeFacets>,
902+
903+
/// Request performance details in the response.
904+
#[serde(skip_serializing_if = "Option::is_none")]
905+
pub show_performance_details: Option<bool>,
883906
}
884907

885908
impl<'a, Http: HttpClient> FederatedMultiSearchQuery<'a, '_, Http> {
@@ -929,6 +952,9 @@ pub struct FederatedMultiSearchResponse<T> {
929952

930953
/// Indicates which remote requests failed and why
931954
pub remote_errors: Option<HashMap<String, MeilisearchError>>,
955+
956+
/// The performance trace for the query.
957+
pub performance_details: Option<Value>,
932958
}
933959

934960
/// Returned for each hit in `_federation` when doing federated multi search.
@@ -2305,4 +2331,70 @@ pub(crate) mod tests {
23052331
assert_eq!(res.facet_hits[0].count, 7);
23062332
Ok(())
23072333
}
2334+
2335+
#[meilisearch_test]
2336+
async fn test_search_with_show_performance_details(
2337+
client: Client,
2338+
index: Index,
2339+
) -> Result<(), Error> {
2340+
setup_test_index(&client, &index).await?;
2341+
2342+
let res = index
2343+
.search()
2344+
.with_show_performance_details(true)
2345+
.with_query("Lorem")
2346+
.execute::<Value>()
2347+
.await?;
2348+
2349+
assert!(res.performance_details.is_some());
2350+
2351+
Ok(())
2352+
}
2353+
2354+
#[meilisearch_test]
2355+
async fn test_multi_search_with_show_performance_details(
2356+
client: Client,
2357+
index: Index,
2358+
) -> Result<(), Error> {
2359+
setup_test_index(&client, &index).await?;
2360+
let search_query_1 = SearchQuery::new(&index)
2361+
.with_query("Sorcerer's Stone")
2362+
.with_show_performance_details(true)
2363+
.build();
2364+
let search_query_2 = SearchQuery::new(&index)
2365+
.with_query("Chamber of Secrets")
2366+
.build();
2367+
2368+
let response = client
2369+
.multi_search()
2370+
.with_search_query(search_query_1)
2371+
.with_search_query(search_query_2)
2372+
.execute::<Document>()
2373+
.await
2374+
.unwrap();
2375+
2376+
assert!(response.results[0].performance_details.is_some());
2377+
Ok(())
2378+
}
2379+
2380+
#[meilisearch_test]
2381+
async fn test_federated_multi_search_with_show_performance_details(
2382+
client: Client,
2383+
test_index: Index,
2384+
) -> Result<(), Error> {
2385+
setup_test_index(&client, &test_index).await?;
2386+
2387+
let response = client
2388+
.multi_search()
2389+
.with_federation(FederationOptions {
2390+
show_performance_details: Some(true),
2391+
..Default::default()
2392+
})
2393+
.execute::<Value>()
2394+
.await?;
2395+
2396+
assert!(response.performance_details.is_some());
2397+
2398+
Ok(())
2399+
}
23082400
}

src/similar.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ pub struct SimilarResults<T> {
2929
pub limit: Option<usize>,
3030
/// Estimated total number of matches
3131
pub estimated_total_hits: Option<usize>,
32+
/// Performance trace of the query
33+
pub performance_details: Option<Value>,
3234
/// Processing time of the query
3335
pub processing_time_ms: usize,
3436
/// Identifier of the target document
@@ -125,6 +127,12 @@ pub struct SimilarQuery<'a, Http: HttpClient> {
125127
/// **Default: `false`**
126128
#[serde(skip_serializing_if = "Option::is_none")]
127129
pub retrieve_vectors: Option<bool>,
130+
131+
/// Defines whether to return performance trace
132+
///
133+
/// **Default: `false`**
134+
#[serde(skip_serializing_if = "Option::is_none")]
135+
pub show_performance_details: Option<bool>,
128136
}
129137

130138
#[allow(missing_docs)]
@@ -143,6 +151,7 @@ impl<'a, Http: HttpClient> SimilarQuery<'a, Http> {
143151
show_ranking_score_details: None,
144152
ranking_score_threshold: None,
145153
retrieve_vectors: None,
154+
show_performance_details: None,
146155
}
147156
}
148157

@@ -209,6 +218,15 @@ impl<'a, Http: HttpClient> SimilarQuery<'a, Http> {
209218
self
210219
}
211220

221+
/// Request performance trace in the response.
222+
pub fn with_show_performance_details<'b>(
223+
&'b mut self,
224+
show_performance_details: bool,
225+
) -> &'b mut SimilarQuery<'a, Http> {
226+
self.show_performance_details = Some(show_performance_details);
227+
self
228+
}
229+
212230
/// Execute the query and fetch the results.
213231
pub async fn execute<T: 'static + DeserializeOwned + Send + Sync>(
214232
&'a self,
@@ -389,4 +407,19 @@ mod tests {
389407
assert!(results.hits[0].result._vectors.is_some());
390408
Ok(())
391409
}
410+
411+
#[meilisearch_test]
412+
async fn test_query_show_performance_details(
413+
client: Client,
414+
index: Index,
415+
) -> Result<(), Error> {
416+
setup_embedder(&client, &index).await?;
417+
setup_test_index(&client, &index).await?;
418+
419+
let mut query = SimilarQuery::new(&index, "1", "default");
420+
query.with_show_performance_details(true);
421+
let results: SimilarResults<Document> = query.execute().await?;
422+
assert!(results.performance_details.is_some());
423+
Ok(())
424+
}
392425
}

0 commit comments

Comments
 (0)