Add support for filtering on arrays of strings

update changelog

Update CHANGELOG.md

Co-Authored-By: Clément Renault <renault.cle@gmail.com>

fix requested changes
This commit is contained in:
mposmta 2020-04-20 16:00:35 +02:00
parent 3d2f04a7af
commit cd3c0d750c
2 changed files with 24 additions and 13 deletions

View File

@ -1,6 +1,7 @@
## v0.10.1 (unreleased) ## v0.10.1 (unreleased)
- Add '@' character as tokenizer separator (#607) - Add '@' character as tokenizer separator (#607)
- Add support for filtering on arrays of strings (#611)
## v0.10 ## v0.10

View File

@ -10,7 +10,7 @@ use pest::iterators::Pair;
use serde_json::{Value, Number}; use serde_json::{Value, Number};
use super::parser::Rule; use super::parser::Rule;
#[derive(Debug)] #[derive(Debug, PartialEq)]
enum ConditionType { enum ConditionType {
Greater, Greater,
Less, Less,
@ -160,12 +160,19 @@ impl<'a> Condition<'a> {
document_id: DocumentId, document_id: DocumentId,
) -> Result<bool, Error> { ) -> Result<bool, Error> {
match index.document_attribute::<Value>(reader, document_id, self.field)? { match index.document_attribute::<Value>(reader, document_id, self.field)? {
Some(Value::Array(values)) => Ok(values.iter().any(|v| self.match_value(Some(v)))),
other => Ok(self.match_value(other.as_ref())),
}
}
fn match_value(&self, value: Option<&Value>) -> bool {
match value {
Some(Value::String(s)) => { Some(Value::String(s)) => {
let value = self.value.as_str(); let value = self.value.as_str();
match self.condition { match self.condition {
ConditionType::Equal => Ok(unicase::eq(value, &s)), ConditionType::Equal => unicase::eq(value, &s),
ConditionType::NotEqual => Ok(!unicase::eq(value, &s)), ConditionType::NotEqual => !unicase::eq(value, &s),
_ => Ok(false) _ => false
} }
}, },
Some(Value::Number(n)) => { Some(Value::Number(n)) => {
@ -179,22 +186,25 @@ impl<'a> Condition<'a> {
ConditionType::Greater => ord == Ordering::Greater, ConditionType::Greater => ord == Ordering::Greater,
ConditionType::Less => ord == Ordering::Less, ConditionType::Less => ord == Ordering::Less,
}; };
return Ok(res) return res
} }
} }
Ok(false) false
}, },
Some(Value::Bool(b)) => { Some(Value::Bool(b)) => {
if let Some(value) = self.value.as_bool() { if let Some(value) = self.value.as_bool() {
return match self.condition { let res = match self.condition {
ConditionType::Equal => Ok(b == value), ConditionType::Equal => *b == value,
ConditionType::NotEqual => Ok(b != value), ConditionType::NotEqual => *b != value,
_ => Ok(false) _ => false
} };
return res
} }
Ok(false) false
}, },
_ => Ok(false), // if field is not supported (or not found), all values are different from it,
// so != should always return true in this case.
_ => self.condition == ConditionType::NotEqual,
} }
} }
} }