Skip to content

Open Source alternative to Algolia + Pinecone and an Easier-to-Use alternative to ElasticSearch ⚡ 🔍 ✨ Fast, typo tolerant, in-memory fuzzy Search Engine for building delightful search experiences

Limit `filter_by` on number of operations.

Harpreet Sangard6d3bf88b3e1
diff --git a/src/field.cpp b/src/field.cpp
index ee62b7fa..344347b4 100644
--- a/src/field.cpp
+++ b/src/field.cpp
@@ -489,16 +489,16 @@ Option<bool> filter::parse_filter_query(const std::string& filter_query,
return tokenize_op;
}
- if (tokens.size() > 100) {
- return Option<bool>(400, "Filter expression is not valid.");
- }
-
std::queue<std::string> postfix;
Option<bool> toPostfix_op = toPostfix(tokens, postfix);
if (!toPostfix_op.ok()) {
return toPostfix_op;
}
+ if (postfix.size() > 100) {
+ return Option<bool>(400, "`filter_by` has too many operations.");
+ }
+
Option<bool> toParseTree_op = toParseTree(postfix,
root,
search_schema,
diff --git a/test/collection_filtering_test.cpp b/test/collection_filtering_test.cpp
index 1628c2dc..0e736e23 100644
--- a/test/collection_filtering_test.cpp
+++ b/test/collection_filtering_test.cpp
@@ -2627,5 +2627,27 @@ TEST_F(CollectionFilteringTest, ComplexFilterQuery) {
ASSERT_STREQ(id.c_str(), result_id.c_str());
}
+ std::string extreme_filter = "(years:>2000 && ((age:<30 && rating:>5) || (age:>50 && rating:<5))) ||"
+ "(years:>2000 && ((age:<30 && rating:>5) || (age:>50 && rating:<5))) ||"
+ "(years:>2000 && ((age:<30 && rating:>5) || (age:>50 && rating:<5))) ||"
+ "(years:>2000 && ((age:<30 && rating:>5) || (age:>50 && rating:<5))) ||"
+ "(years:>2000 && ((age:<30 && rating:>5) || (age:>50 && rating:<5))) ||"
+ "(years:>2000 && ((age:<30 && rating:>5) || (age:>50 && rating:<5))) ||"
+ "(years:>2000 && ((age:<30 && rating:>5) || (age:>50 && rating:<5))) ||"
+ "(years:>2000 && ((age:<30 && rating:>5) || (age:>50 && rating:<5))) ||"
+ "(years:>2000 && ((age:<30 && rating:>5) || (age:>50 && rating:<5))) ||"
+ "(years:>2000 && ((age:<30 && rating:>5) || (age:>50 && rating:<5)))";
+
+ auto search_op = coll->search("Jeremy", {"name"}, extreme_filter,
+ {}, sort_fields_desc, {0}, 10, 1, FREQUENCY, {false});
+ ASSERT_TRUE(search_op.ok());
+ ASSERT_EQ(1, search_op.get()["hits"].size());
+
+ extreme_filter += "|| (years:>2000 && ((age:<30 && rating:>5) || (age:>50 && rating:<5)))";
+ search_op = coll->search("Jeremy", {"name"}, extreme_filter,
+ {}, sort_fields_desc, {0}, 10, 1, FREQUENCY, {false});
+ ASSERT_FALSE(search_op.ok());
+ ASSERT_EQ("`filter_by` has too many operations.", search_op.error());
+
collectionManager.drop_collection("ComplexFilterQueryCollection");
}
\ No newline at end of file