Skip to content

Commit a5afb1a

Browse files
committed
feat: Add with_array_filter() function to allow passing array as a filter list
1 parent 79642ad commit a5afb1a

File tree

1 file changed

+48
-2
lines changed

1 file changed

+48
-2
lines changed

src/search.rs

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use crate::{errors::Error, indexes::Index};
22
use serde::{de::DeserializeOwned, Deserialize, Serialize, Serializer};
3+
use serde::ser::{SerializeSeq};
34
use serde_json::{Map, Value};
45
use std::collections::HashMap;
56

@@ -9,6 +10,32 @@ pub struct MatchRange {
910
pub length: usize,
1011
}
1112

13+
#[derive(Debug, Eq, PartialEq , Clone)]
14+
pub enum Filter<'a> {
15+
String(&'a str),
16+
Array(Vec<&'a str>)
17+
}
18+
19+
impl<'a> Serialize for Filter<'a> {
20+
fn serialize<S>(&self , serializer: S) -> Result<S::Ok , S::Error>
21+
where
22+
S: Serializer
23+
{
24+
match self {
25+
Filter::String(s) => serializer.serialize_str(s),
26+
Filter::Array(s) => {
27+
let mut seq = serializer.serialize_seq(Some(s.len()))?;
28+
29+
for item in s {
30+
seq.serialize_element(item)?;
31+
}
32+
33+
seq.end()
34+
},
35+
}
36+
}
37+
}
38+
1239
#[derive(Debug, Clone, Serialize)]
1340
pub enum MatchingStrategies {
1441
#[serde(rename = "all")]
@@ -182,7 +209,7 @@ pub struct SearchQuery<'a> {
182209
/// Filter applied to documents.
183210
/// Read the [dedicated guide](https://docs.meilisearch.com/reference/features/filtering.html) to learn the syntax.
184211
#[serde(skip_serializing_if = "Option::is_none")]
185-
pub filter: Option<&'a str>,
212+
pub filter: Option<Filter<'a>>,
186213
/// Facets for which to retrieve the matching count.
187214
///
188215
/// Can be set to a [wildcard value](enum.Selectors.html#variant.All) that will select all existing attributes.
@@ -284,7 +311,11 @@ impl<'a> SearchQuery<'a> {
284311
self
285312
}
286313
pub fn with_filter<'b>(&'b mut self, filter: &'a str) -> &'b mut SearchQuery<'a> {
287-
self.filter = Some(filter);
314+
self.filter = Some(Filter::String(filter));
315+
self
316+
}
317+
pub fn with_array_filter<'b>(&'b mut self , filter: Vec<&'a str>) -> &'b mut SearchQuery<'a> {
318+
self.filter = Some(Filter::Array(filter));
288319
self
289320
}
290321
pub fn with_facets<'b>(
@@ -499,6 +530,21 @@ mod tests {
499530
Ok(())
500531
}
501532

533+
#[meilisearch_test]
534+
async fn test_query_filter_with_array(client: Client, index: Index) -> Result<(), Error> {
535+
setup_test_index(&client, &index).await?;
536+
537+
let results: SearchResults<Document> = index
538+
.search()
539+
.with_array_filter(vec!["value = \"The Social Network\"" , "value = \"The Social Network\""])
540+
.execute()
541+
.await?;
542+
assert_eq!(results.hits.len(), 1);
543+
544+
Ok(())
545+
}
546+
547+
502548
#[meilisearch_test]
503549
async fn test_query_facet_distribution(client: Client, index: Index) -> Result<(), Error> {
504550
setup_test_index(&client, &index).await?;

0 commit comments

Comments
 (0)