Skip to content

Commit eb18ed9

Browse files
authored
Update search params for v0.29.0 (#333)
* Add MatchingStrategies as an enum * Improve example
1 parent 1bcf1c8 commit eb18ed9

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

src/search.rs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,14 @@ pub struct MatchRange {
99
pub length: usize,
1010
}
1111

12+
#[derive(Debug, Clone, Serialize)]
13+
pub enum MatchingStrategies {
14+
#[serde(rename = "all")]
15+
ALL,
16+
#[serde(rename = "last")]
17+
LAST,
18+
}
19+
1220
/// A single result.
1321
/// Contains the complete object, optionally the formatted object, and optionally an object that contains information about the matches.
1422
#[derive(Deserialize, Debug)]
@@ -234,6 +242,10 @@ pub struct Query<'a> {
234242
/// Default: `false`
235243
#[serde(skip_serializing_if = "Option::is_none")]
236244
pub show_matches_position: Option<bool>,
245+
246+
/// Defines the strategy on how to handle queries containing multiple words.
247+
#[serde(skip_serializing_if = "Option::is_none")]
248+
pub matching_strategy: Option<MatchingStrategies>,
237249
}
238250

239251
#[allow(missing_docs)]
@@ -255,6 +267,7 @@ impl<'a> Query<'a> {
255267
highlight_pre_tag: None,
256268
highlight_post_tag: None,
257269
show_matches_position: None,
270+
matching_strategy: None,
258271
}
259272
}
260273
pub fn with_query<'b>(&'b mut self, query: &'a str) -> &'b mut Query<'a> {
@@ -332,6 +345,13 @@ impl<'a> Query<'a> {
332345
self.show_matches_position = Some(show_matches_position);
333346
self
334347
}
348+
pub fn with_matching_strategy<'b>(
349+
&'b mut self,
350+
matching_strategy: MatchingStrategies,
351+
) -> &'b mut Query<'a> {
352+
self.matching_strategy = Some(matching_strategy);
353+
self
354+
}
335355
pub fn build(&mut self) -> Query<'a> {
336356
self.clone()
337357
}
@@ -755,6 +775,36 @@ mod tests {
755775
Ok(())
756776
}
757777

778+
#[meilisearch_test]
779+
async fn test_matching_strategy_all(client: Client, index: Index) -> Result<(), Error> {
780+
setup_test_index(&client, &index).await?;
781+
782+
let results = Query::new(&index)
783+
.with_query("Harry Styles")
784+
.with_matching_strategy(MatchingStrategies::ALL)
785+
.execute::<Document>()
786+
.await
787+
.unwrap();
788+
789+
assert_eq!(results.hits.len(), 0);
790+
Ok(())
791+
}
792+
793+
#[meilisearch_test]
794+
async fn test_matching_strategy_left(client: Client, index: Index) -> Result<(), Error> {
795+
setup_test_index(&client, &index).await?;
796+
797+
let results = Query::new(&index)
798+
.with_query("Harry Styles")
799+
.with_matching_strategy(MatchingStrategies::LAST)
800+
.execute::<Document>()
801+
.await
802+
.unwrap();
803+
804+
assert_eq!(results.hits.len(), 7);
805+
Ok(())
806+
}
807+
758808
#[meilisearch_test]
759809
async fn test_generate_tenant_token_from_client(
760810
client: Client,

0 commit comments

Comments
 (0)