mirror of
https://github.com/meilisearch/MeiliSearch
synced 2024-11-23 05:14:27 +01:00
Add one more filter "integration" test
This commit is contained in:
parent
e688581c36
commit
d38cc73630
@ -398,10 +398,12 @@ impl<'a> From<FilterCondition<'a>> for Filter<'a> {
|
|||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use std::fmt::Write;
|
use std::fmt::Write;
|
||||||
|
use std::iter::FromIterator;
|
||||||
|
|
||||||
use big_s::S;
|
use big_s::S;
|
||||||
use either::Either;
|
use either::Either;
|
||||||
use maplit::hashset;
|
use maplit::hashset;
|
||||||
|
use roaring::RoaringBitmap;
|
||||||
|
|
||||||
use crate::index::tests::TempIndex;
|
use crate::index::tests::TempIndex;
|
||||||
use crate::Filter;
|
use crate::Filter;
|
||||||
@ -752,4 +754,85 @@ mod tests {
|
|||||||
Err(crate::Error::UserError(crate::error::UserError::InvalidFilter(_)))
|
Err(crate::Error::UserError(crate::error::UserError::InvalidFilter(_)))
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn filter_number() {
|
||||||
|
let index = TempIndex::new();
|
||||||
|
|
||||||
|
index
|
||||||
|
.update_settings(|settings| {
|
||||||
|
settings.set_primary_key("id".to_owned());
|
||||||
|
settings.set_filterable_fields(hashset! { S("id"), S("one"), S("two") });
|
||||||
|
})
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
let mut docs = vec![];
|
||||||
|
for i in 0..100 {
|
||||||
|
docs.push(serde_json::json!({ "id": i, "two": i % 10 }));
|
||||||
|
}
|
||||||
|
|
||||||
|
index.add_documents(documents!(docs)).unwrap();
|
||||||
|
|
||||||
|
let rtxn = index.read_txn().unwrap();
|
||||||
|
for i in 0..100 {
|
||||||
|
let filter_str = format!("id = {i}");
|
||||||
|
let filter = Filter::from_str(&filter_str).unwrap().unwrap();
|
||||||
|
let result = filter.evaluate(&rtxn, &index).unwrap();
|
||||||
|
assert_eq!(result, RoaringBitmap::from_iter([i]));
|
||||||
|
}
|
||||||
|
for i in 0..100 {
|
||||||
|
let filter_str = format!("id > {i}");
|
||||||
|
let filter = Filter::from_str(&filter_str).unwrap().unwrap();
|
||||||
|
let result = filter.evaluate(&rtxn, &index).unwrap();
|
||||||
|
assert_eq!(result, RoaringBitmap::from_iter((i + 1)..100));
|
||||||
|
}
|
||||||
|
for i in 0..100 {
|
||||||
|
let filter_str = format!("id < {i}");
|
||||||
|
let filter = Filter::from_str(&filter_str).unwrap().unwrap();
|
||||||
|
let result = filter.evaluate(&rtxn, &index).unwrap();
|
||||||
|
assert_eq!(result, RoaringBitmap::from_iter(0..i));
|
||||||
|
}
|
||||||
|
for i in 0..100 {
|
||||||
|
let filter_str = format!("id <= {i}");
|
||||||
|
let filter = Filter::from_str(&filter_str).unwrap().unwrap();
|
||||||
|
let result = filter.evaluate(&rtxn, &index).unwrap();
|
||||||
|
assert_eq!(result, RoaringBitmap::from_iter(0..=i));
|
||||||
|
}
|
||||||
|
for i in 0..100 {
|
||||||
|
let filter_str = format!("id >= {i}");
|
||||||
|
let filter = Filter::from_str(&filter_str).unwrap().unwrap();
|
||||||
|
let result = filter.evaluate(&rtxn, &index).unwrap();
|
||||||
|
assert_eq!(result, RoaringBitmap::from_iter(i..100));
|
||||||
|
}
|
||||||
|
for i in 0..100 {
|
||||||
|
for j in i..100 {
|
||||||
|
let filter_str = format!("id {i} TO {j}");
|
||||||
|
let filter = Filter::from_str(&filter_str).unwrap().unwrap();
|
||||||
|
let result = filter.evaluate(&rtxn, &index).unwrap();
|
||||||
|
assert_eq!(result, RoaringBitmap::from_iter(i..=j));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
let filter = Filter::from_str("one >= 0 OR one <= 0").unwrap().unwrap();
|
||||||
|
let result = filter.evaluate(&rtxn, &index).unwrap();
|
||||||
|
assert_eq!(result, RoaringBitmap::default());
|
||||||
|
|
||||||
|
let filter = Filter::from_str("one = 0").unwrap().unwrap();
|
||||||
|
let result = filter.evaluate(&rtxn, &index).unwrap();
|
||||||
|
assert_eq!(result, RoaringBitmap::default());
|
||||||
|
|
||||||
|
for i in 0..10 {
|
||||||
|
for j in i..10 {
|
||||||
|
let filter_str = format!("two {i} TO {j}");
|
||||||
|
let filter = Filter::from_str(&filter_str).unwrap().unwrap();
|
||||||
|
let result = filter.evaluate(&rtxn, &index).unwrap();
|
||||||
|
assert_eq!(
|
||||||
|
result,
|
||||||
|
RoaringBitmap::from_iter((0..100).filter(|x| (i..=j).contains(&(x % 10))))
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
let filter = Filter::from_str("two != 0").unwrap().unwrap();
|
||||||
|
let result = filter.evaluate(&rtxn, &index).unwrap();
|
||||||
|
assert_eq!(result, RoaringBitmap::from_iter((0..100).filter(|x| x % 10 != 0)));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user