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

Improve int32 filter error message. (#1618)

Harpreet Sangar5e1600c3d913
diff --git a/src/collection.cpp b/src/collection.cpp
index d9006746..6c6f5ef9 100644
--- a/src/collection.cpp
+++ b/src/collection.cpp
@@ -90,7 +90,8 @@ Option<bool> single_value_filter_query(nlohmann::json& document, const std::stri
filter_query[filter_query.size() - 1] = '=';
filter_query += (" " + value.get<std::string>());
} else if (value.is_number_integer() && (ref_field_type == field_types::INT64 ||
- (ref_field_type == field_types::INT32 && StringUtils::is_int32_t(std::to_string(value.get<int64_t>()))))) {
+ (ref_field_type == field_types::INT32 &&
+ StringUtils::is_int32_t(std::to_string(value.get<int64_t>()))))) {
filter_query += std::to_string(value.get<int64_t>());
} else {
return Option<bool>(400, "Field `" + field_name + "` must have `" + ref_field_type + "` value.");
@@ -274,7 +275,8 @@ Option<bool> Collection::add_reference_helper_fields(nlohmann::json& document, c
if (item_value.is_string() && ref_field_type == field_types::STRING) {
filter_query += item_value.get<std::string>();
} else if (item_value.is_number_integer() && (ref_field_type == field_types::INT64 ||
- (ref_field_type == field_types::INT32 && StringUtils::is_int32_t(std::to_string(item_value.get<int64_t>()))))) {
+ (ref_field_type == field_types::INT32 &&
+ StringUtils::is_int32_t(std::to_string(item_value.get<int64_t>()))))) {
filter_query += std::to_string(item_value.get<int64_t>());
} else {
return Option<bool>(400, "Field `" + field_name + "` must only have `" + ref_field_type + "` values.");
diff --git a/src/filter.cpp b/src/filter.cpp
index 65abfcf7..cba4e83d 100644
--- a/src/filter.cpp
+++ b/src/filter.cpp
@@ -5,8 +5,14 @@
#include "filter.h"
Option<bool> filter::validate_numerical_filter_value(field _field, const string &raw_value) {
- if(_field.is_int32() && !StringUtils::is_int32_t(raw_value)) {
- return Option<bool>(400, "Error with filter field `" + _field.name + "`: Not an int32.");
+ if(_field.is_int32()) {
+ if (!StringUtils::is_integer(raw_value)) {
+ return Option<bool>(400, "Error with filter field `" + _field.name + "`: Not an int32.");
+ } else if (!StringUtils::is_int32_t(raw_value)) {
+ return Option<bool>(400, "Error with filter field `" + _field.name +
+ "`: `" + raw_value + "` exceeds the range of an int32.");
+ }
+ return Option<bool>(true);
}
else if(_field.is_int64() && !StringUtils::is_int64_t(raw_value)) {
diff --git a/test/collection_filtering_test.cpp b/test/collection_filtering_test.cpp
index d452caf2..5bdf8a4c 100644
--- a/test/collection_filtering_test.cpp
+++ b/test/collection_filtering_test.cpp
@@ -477,6 +477,11 @@ TEST_F(CollectionFilteringTest, FilterAndQueryFieldRestrictions) {
ASSERT_EQ(false, result_op.ok());
ASSERT_STREQ("Error with filter field `points`: Numerical field has an invalid comparator.", result_op.error().c_str());
+ result_op = coll_mul_fields->search("captain", query_fields, "points:<= foo", facets, sort_fields, {0}, 10, 1,
+ FREQUENCY, {false});
+ ASSERT_FALSE(result_op.ok());
+ ASSERT_EQ("Error with filter field `points`: Not an int32.", result_op.error());
+
// bad filter value type - equaling float on an integer field
result_op = coll_mul_fields->search("captain", query_fields, "points: 100.34", facets, sort_fields, {0}, 10, 1,
FREQUENCY, {false});
@@ -493,7 +498,12 @@ TEST_F(CollectionFilteringTest, FilterAndQueryFieldRestrictions) {
result_op = coll_mul_fields->search("captain", query_fields, "points: <2230070399", facets, sort_fields, {0}, 10, 1,
FREQUENCY, {false});
ASSERT_EQ(false, result_op.ok());
- ASSERT_STREQ("Error with filter field `points`: Not an int32.", result_op.error().c_str());
+ ASSERT_EQ("Error with filter field `points`: `2230070399` exceeds the range of an int32.", result_op.error());
+
+ result_op = coll_mul_fields->search("captain", query_fields, "points:<= 9223372036854775808", facets, sort_fields, {0}, 10, 1,
+ FREQUENCY, {false});
+ ASSERT_FALSE(result_op.ok());
+ ASSERT_EQ("Error with filter field `points`: `9223372036854775808` exceeds the range of an int32.", result_op.error());
// using a string filter value against an integer field
result_op = coll_mul_fields->search("captain", query_fields, "points: <sdsdfsdf", facets, sort_fields, {0}, 10, 1,