Skip to content

Commit 0107e03

Browse files
committed
Improve test
1 parent 2c540a1 commit 0107e03

File tree

1 file changed

+60
-24
lines changed

1 file changed

+60
-24
lines changed

src/search.rs

Lines changed: 60 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -751,6 +751,19 @@ impl<'a, 'b, Http: HttpClient> MultiSearchQuery<'a, 'b, Http> {
751751
self
752752
}
753753

754+
pub fn with_search_query_and_weight(
755+
&mut self,
756+
mut search_query: SearchQuery<'b, Http>,
757+
weight: f32,
758+
) -> &mut MultiSearchQuery<'a, 'b, Http> {
759+
search_query.with_index_uid();
760+
search_query.federation_options = Some(QueryFederationOptions {
761+
weight: Some(weight),
762+
});
763+
self.queries.push(search_query);
764+
self
765+
}
766+
754767
pub fn with_search_query_and_options(
755768
&mut self,
756769
mut search_query: SearchQuery<'b, Http>,
@@ -1248,41 +1261,34 @@ mod tests {
12481261
#[meilisearch_test]
12491262
async fn test_federated_multi_search(
12501263
client: Client,
1251-
index_a: Index,
1252-
index_b: Index,
1264+
test_index: Index,
1265+
video_index: Index,
12531266
) -> Result<(), Error> {
1254-
setup_test_index(&client, &index_a).await?;
1255-
setup_test_video_index(&client, &index_b).await?;
1267+
setup_test_index(&client, &test_index).await?;
1268+
setup_test_video_index(&client, &video_index).await?;
12561269

1257-
let query_death_a = SearchQuery::new(&index_a).with_query("death").build();
1258-
let query_death_b = SearchQuery::new(&index_b).with_query("death").build();
1270+
let query_test_index = SearchQuery::new(&test_index).with_query("death").build();
1271+
let query_video_index = SearchQuery::new(&video_index).with_query("death").build();
12591272

12601273
#[derive(Debug, Serialize, Deserialize, PartialEq)]
12611274
#[serde(untagged)]
12621275
enum AnyDocument {
1263-
IndexA(Document),
1264-
IndexB(VideoDocument),
1276+
Document(Document),
1277+
VideoDocument(VideoDocument),
12651278
}
12661279

1280+
// Search with big weight on the test index
12671281
let mut multi_query = client.multi_search();
1268-
multi_query.with_search_query(query_death_a.clone());
1269-
multi_query.with_search_query(query_death_b.clone());
1282+
multi_query.with_search_query_and_weight(query_test_index.clone(), 999.0);
1283+
multi_query.with_search_query(query_video_index.clone());
12701284
let response = multi_query
12711285
.with_federation(FederationOptions::default())
12721286
.execute::<AnyDocument>()
12731287
.await?;
1274-
12751288
assert_eq!(response.hits.len(), 2);
1276-
let pos_a = response
1277-
.hits
1278-
.iter()
1279-
.position(|hit| hit.federation.as_ref().unwrap().index_uid == index_a.uid)
1280-
.expect("No hit of index_a found");
1281-
let hit_a = &response.hits[pos_a];
1282-
let hit_b = &response.hits[if pos_a == 0 { 1 } else { 0 }];
12831289
assert_eq!(
1284-
hit_a.result,
1285-
AnyDocument::IndexA(Document {
1290+
response.hits[0].result,
1291+
AnyDocument::Document(Document {
12861292
id: 9,
12871293
kind: "title".into(),
12881294
number: 90,
@@ -1292,19 +1298,49 @@ mod tests {
12921298
})
12931299
);
12941300
assert_eq!(
1295-
hit_b.result,
1296-
AnyDocument::IndexB(VideoDocument {
1301+
response.hits[1].result,
1302+
AnyDocument::VideoDocument(VideoDocument {
12971303
id: 3,
12981304
title: S("Harry Potter and the Deathly Hallows"),
12991305
description: None,
13001306
duration: 7654,
13011307
})
13021308
);
13031309

1310+
// Search with big weight on the video index
1311+
let mut multi_query = client.multi_search();
1312+
multi_query.with_search_query(query_test_index.clone());
1313+
multi_query.with_search_query_and_weight(query_video_index.clone(), 999.0);
1314+
let response = multi_query
1315+
.with_federation(FederationOptions::default())
1316+
.execute::<AnyDocument>()
1317+
.await?;
1318+
assert_eq!(response.hits.len(), 2);
1319+
assert_eq!(
1320+
response.hits[0].result,
1321+
AnyDocument::VideoDocument(VideoDocument {
1322+
id: 3,
1323+
title: S("Harry Potter and the Deathly Hallows"),
1324+
description: None,
1325+
duration: 7654,
1326+
})
1327+
);
1328+
assert_eq!(
1329+
response.hits[1].result,
1330+
AnyDocument::Document(Document {
1331+
id: 9,
1332+
kind: "title".into(),
1333+
number: 90,
1334+
value: S("Harry Potter and the Deathly Hallows"),
1335+
nested: Nested { child: S("tenth") },
1336+
_vectors: None,
1337+
})
1338+
);
1339+
13041340
// Make sure federation options are applied
13051341
let mut multi_query = client.multi_search();
1306-
multi_query.with_search_query(query_death_a.clone());
1307-
multi_query.with_search_query(query_death_b.clone());
1342+
multi_query.with_search_query(query_test_index.clone());
1343+
multi_query.with_search_query(query_video_index.clone());
13081344
let response = multi_query
13091345
.with_federation(FederationOptions {
13101346
limit: Some(1),

0 commit comments

Comments
 (0)