Skip to content

Commit cc16278

Browse files
bors[bot]puddingpy
andauthored
Merge #347
347: feat: Add `with_array_filter()` function to allow passing array as a filter list r=bidoubiwa a=VoidCupboard ## Related issue Closes #163 ## What does this PR do? Adds support to pass array as a filter ## PR checklist Please check if your PR fulfills the following requirements: - [X] Does this PR fix an existing issue, or have you listed the changes applied in the PR description (and why they are needed)? - [X] Have you read the contributing guidelines? - [X] Have you made sure that the title is accurate and descriptive of the changes? I am still learning Rust, and this is my first time Rust code contributing to any open source project 😅, let me know if I did something wrong. Edit: The tests arent that good, I just added them to check if the function was working or not. Suggest me a better test! Co-authored-by: VoidCupboard <[email protected]>
2 parents bdd5f5a + 8d69be5 commit cc16278

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ serde_json = "1.0"
2020
time = { version = "0.3.7", features = ["serde-well-known", "formatting", "parsing"] }
2121
jsonwebtoken = { version = "8", default-features = false }
2222
yaup = "0.2.0"
23+
either = { version = "1.8.0" , features = ["serde"] }
2324

2425
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
2526
futures = "0.3"

src/search.rs

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,29 @@ use crate::{errors::Error, indexes::Index};
22
use serde::{de::DeserializeOwned, Deserialize, Serialize, Serializer};
33
use serde_json::{Map, Value};
44
use std::collections::HashMap;
5+
use either::Either;
56

67
#[derive(Deserialize, Debug, Eq, PartialEq)]
78
pub struct MatchRange {
89
pub start: usize,
910
pub length: usize,
1011
}
1112

13+
#[derive(Serialize , Debug, Eq, PartialEq , Clone)]
14+
#[serde(transparent)]
15+
pub struct Filter<'a>{
16+
#[serde(with = "either::serde_untagged")]
17+
inner: Either<&'a str , Vec<&'a str>>
18+
}
19+
20+
impl<'a> Filter<'a> {
21+
pub fn new(inner: Either<&'a str , Vec<&'a str>>) -> Filter {
22+
Filter {
23+
inner
24+
}
25+
}
26+
}
27+
1228
#[derive(Debug, Clone, Serialize)]
1329
pub enum MatchingStrategies {
1430
#[serde(rename = "all")]
@@ -182,7 +198,7 @@ pub struct SearchQuery<'a> {
182198
/// Filter applied to documents.
183199
/// Read the [dedicated guide](https://docs.meilisearch.com/reference/features/filtering.html) to learn the syntax.
184200
#[serde(skip_serializing_if = "Option::is_none")]
185-
pub filter: Option<&'a str>,
201+
pub filter: Option<Filter<'a>>,
186202
/// Facets for which to retrieve the matching count.
187203
///
188204
/// Can be set to a [wildcard value](enum.Selectors.html#variant.All) that will select all existing attributes.
@@ -284,7 +300,11 @@ impl<'a> SearchQuery<'a> {
284300
self
285301
}
286302
pub fn with_filter<'b>(&'b mut self, filter: &'a str) -> &'b mut SearchQuery<'a> {
287-
self.filter = Some(filter);
303+
self.filter = Some(Filter::new(Either::Left(filter)));
304+
self
305+
}
306+
pub fn with_array_filter<'b>(&'b mut self , filter: Vec<&'a str>) -> &'b mut SearchQuery<'a> {
307+
self.filter = Some(Filter::new(Either::Right(filter)));
288308
self
289309
}
290310
pub fn with_facets<'b>(
@@ -499,6 +519,21 @@ mod tests {
499519
Ok(())
500520
}
501521

522+
#[meilisearch_test]
523+
async fn test_query_filter_with_array(client: Client, index: Index) -> Result<(), Error> {
524+
setup_test_index(&client, &index).await?;
525+
526+
let results: SearchResults<Document> = index
527+
.search()
528+
.with_array_filter(vec!["value = \"The Social Network\"" , "value = \"The Social Network\""])
529+
.execute()
530+
.await?;
531+
assert_eq!(results.hits.len(), 1);
532+
533+
Ok(())
534+
}
535+
536+
502537
#[meilisearch_test]
503538
async fn test_query_facet_distribution(client: Client, index: Index) -> Result<(), Error> {
504539
setup_test_index(&client, &index).await?;

0 commit comments

Comments
 (0)