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

Take care of empty filter value in filter by id.

Kishore Nallanc48192097965
diff --git a/src/field.cpp b/src/field.cpp
index 8ae9a4ff..5835a981 100644
--- a/src/field.cpp
+++ b/src/field.cpp
@@ -98,6 +98,12 @@ Option<bool> filter::parse_filter_query(const string& simple_filter_query,
filter id_filter;
std::string&& raw_value = filter_block.substr(found_index+1, std::string::npos);
StringUtils::trim(raw_value);
+ std::string empty_filter_err = "Error with filter field `id`: Filter value cannot be empty.";
+
+ if(raw_value.empty()) {
+ return Option<bool>(400, empty_filter_err);
+ }
+
id_filter = {field_name, {}, {}};
NUM_COMPARATOR id_comparator = EQUALS;
@@ -114,8 +120,8 @@ Option<bool> filter::parse_filter_query(const string& simple_filter_query,
raw_value = raw_value.substr(filter_value_index);
}
- if(filter_value_index == raw_value.size()) {
- return Option<bool>(400, "Error with filter field `id`: Filter value cannot be empty.");
+ if(raw_value.empty()) {
+ return Option<bool>(400, empty_filter_err);
}
if(raw_value[0] == '[' && raw_value[raw_value.size() - 1] == ']') {
diff --git a/test/collection_filtering_test.cpp b/test/collection_filtering_test.cpp
index 6c9c4cf3..5f1d3a09 100644
--- a/test/collection_filtering_test.cpp
+++ b/test/collection_filtering_test.cpp
@@ -1633,8 +1633,21 @@ TEST_F(CollectionFilteringTest, FilteringViaDocumentIds) {
ASSERT_STREQ("125", results["hits"][1]["document"]["id"].get<std::string>().c_str());
ASSERT_STREQ("127", results["hits"][2]["document"]["id"].get<std::string>().c_str());
+ // empty id list not allowed
+ auto res_op = coll1->search("*", {}, "id:=", {}, sort_fields, {0}, 10, 1, FREQUENCY, {true});
+ ASSERT_FALSE(res_op.ok());
+ ASSERT_EQ("Error with filter field `id`: Filter value cannot be empty.", res_op.error());
+
+ res_op = coll1->search("*", {}, "id:= ", {}, sort_fields, {0}, 10, 1, FREQUENCY, {true});
+ ASSERT_FALSE(res_op.ok());
+ ASSERT_EQ("Error with filter field `id`: Filter value cannot be empty.", res_op.error());
+
+ res_op = coll1->search("*", {}, "id: ", {}, sort_fields, {0}, 10, 1, FREQUENCY, {true});
+ ASSERT_FALSE(res_op.ok());
+ ASSERT_EQ("Error with filter field `id`: Filter value cannot be empty.", res_op.error());
+
// not equals is not supported yet
- auto res_op = coll1->search("*",
+ res_op = coll1->search("*",
{}, "id:!= [123,125] && num_employees: <300",
{}, sort_fields, {0}, 10, 1, FREQUENCY, {true});
ASSERT_FALSE(res_op.ok());