From 45877b31547e7591305bf67e9872332a037e70fd Mon Sep 17 00:00:00 2001 From: Kerollmops Date: Tue, 1 Dec 2020 14:08:51 +0100 Subject: [PATCH 1/2] Lowercase the facet string value --- src/search/facet/mod.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/search/facet/mod.rs b/src/search/facet/mod.rs index 06f543dcc..b3eb29e58 100644 --- a/src/search/facet/mod.rs +++ b/src/search/facet/mod.rs @@ -237,7 +237,8 @@ impl FacetCondition { FacetType::Integer => Ok(OperatorI64(fid, Equal(pest_parse(value)?))), FacetType::Float => Ok(OperatorF64(fid, Equal(pest_parse(value)?))), FacetType::String => { - Ok(OperatorString(fid, FacetStringOperator::Equal(value.as_str().to_string()))) + let value = value.as_str().to_lowercase().to_string(); + Ok(OperatorString(fid, FacetStringOperator::Equal(value))) }, } } @@ -563,7 +564,7 @@ mod tests { // Test that the facet condition is correctly generated. let rtxn = index.read_txn().unwrap(); let condition = FacetCondition::from_str(&rtxn, &index, "channel = ponce").unwrap(); - let expected = OperatorString(1, FacetStringOperator::Equal("ponce".into())); + let expected = OperatorString(1, FacetStringOperator::Equal("Ponce".into())); assert_eq!(condition, expected); let condition = FacetCondition::from_str(&rtxn, &index, "channel != ponce").unwrap(); From 844a9022fb97ae34df9e7ef0fc442ce3e607893e Mon Sep 17 00:00:00 2001 From: Kerollmops Date: Tue, 1 Dec 2020 14:29:44 +0100 Subject: [PATCH 2/2] Introduce the FacetStringOperator equal and not_equal constructors --- src/search/facet/mod.rs | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/src/search/facet/mod.rs b/src/search/facet/mod.rs index b3eb29e58..7717e2c03 100644 --- a/src/search/facet/mod.rs +++ b/src/search/facet/mod.rs @@ -57,6 +57,14 @@ pub enum FacetStringOperator { } impl FacetStringOperator { + fn equal(s: &str) -> Self { + FacetStringOperator::Equal(s.to_lowercase()) + } + + fn not_equal(s: &str) -> Self { + FacetStringOperator::equal(s).negate() + } + fn negate(self) -> Self { match self { FacetStringOperator::Equal(x) => FacetStringOperator::NotEqual(x), @@ -236,10 +244,7 @@ impl FacetCondition { match ftype { FacetType::Integer => Ok(OperatorI64(fid, Equal(pest_parse(value)?))), FacetType::Float => Ok(OperatorF64(fid, Equal(pest_parse(value)?))), - FacetType::String => { - let value = value.as_str().to_lowercase().to_string(); - Ok(OperatorString(fid, FacetStringOperator::Equal(value))) - }, + FacetType::String => Ok(OperatorString(fid, FacetStringOperator::equal(value.as_str()))), } } @@ -564,15 +569,15 @@ mod tests { // Test that the facet condition is correctly generated. let rtxn = index.read_txn().unwrap(); let condition = FacetCondition::from_str(&rtxn, &index, "channel = ponce").unwrap(); - let expected = OperatorString(1, FacetStringOperator::Equal("Ponce".into())); + let expected = OperatorString(1, FacetStringOperator::equal("Ponce")); assert_eq!(condition, expected); let condition = FacetCondition::from_str(&rtxn, &index, "channel != ponce").unwrap(); - let expected = OperatorString(1, FacetStringOperator::NotEqual("ponce".into())); + let expected = OperatorString(1, FacetStringOperator::not_equal("ponce")); assert_eq!(condition, expected); let condition = FacetCondition::from_str(&rtxn, &index, "NOT channel = ponce").unwrap(); - let expected = OperatorString(1, FacetStringOperator::NotEqual("ponce".into())); + let expected = OperatorString(1, FacetStringOperator::not_equal("ponce")); assert_eq!(condition, expected); } @@ -629,10 +634,10 @@ mod tests { "channel = gotaga OR (timestamp 22 TO 44 AND channel != ponce)", ).unwrap(); let expected = Or( - Box::new(OperatorString(0, FacetStringOperator::Equal("gotaga".into()))), + Box::new(OperatorString(0, FacetStringOperator::equal("gotaga"))), Box::new(And( Box::new(OperatorI64(1, Between(22, 44))), - Box::new(OperatorString(0, FacetStringOperator::NotEqual("ponce".into()))), + Box::new(OperatorString(0, FacetStringOperator::not_equal("ponce"))), )) ); assert_eq!(condition, expected); @@ -642,13 +647,13 @@ mod tests { "channel = gotaga OR NOT (timestamp 22 TO 44 AND channel != ponce)", ).unwrap(); let expected = Or( - Box::new(OperatorString(0, FacetStringOperator::Equal("gotaga".into()))), + Box::new(OperatorString(0, FacetStringOperator::equal("gotaga"))), Box::new(Or( Box::new(Or( Box::new(OperatorI64(1, LowerThan(22))), Box::new(OperatorI64(1, GreaterThan(44))), )), - Box::new(OperatorString(0, FacetStringOperator::Equal("ponce".into()))), + Box::new(OperatorString(0, FacetStringOperator::equal("ponce"))), )), ); assert_eq!(condition, expected);