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

Allow indexing of null values for auto/string* fields.

Kishore Nallan4a3d2cc20961
diff --git a/src/collection.cpp b/src/collection.cpp
index 35efdddf..838863e3 100644
--- a/src/collection.cpp
+++ b/src/collection.cpp
@@ -2272,6 +2272,13 @@ Option<bool> Collection::check_and_update_schema(nlohmann::json& document, const
if(test_field_type == field_types::AUTO || field_types::is_string_or_array(test_field_type)) {
parseable = field::get_type(kv.value(), field_type);
if(!parseable) {
+
+ if(kv.value().is_null() && new_field.optional) {
+ // null values are allowed only if field is optional
+ kv++;
+ continue;
+ }
+
if(dirty_values == DIRTY_VALUES::REJECT || dirty_values == DIRTY_VALUES::COERCE_OR_REJECT) {
return Option<bool>(400, "Type of field `" + kv.key() + "` is invalid.");
} else {
diff --git a/test/collection_all_fields_test.cpp b/test/collection_all_fields_test.cpp
index 7429ab8e..a2d3e91c 100644
--- a/test/collection_all_fields_test.cpp
+++ b/test/collection_all_fields_test.cpp
@@ -915,6 +915,36 @@ TEST_F(CollectionAllFieldsTest, DynamicFieldsMustOnlyBeOptional) {
collectionManager.drop_collection("coll1");
}
+TEST_F(CollectionAllFieldsTest, AutoAndStringStarFieldsShouldAcceptNullValues) {
+ Collection *coll1;
+
+ std::vector<field> fields = {
+ field("foo", "string*", true, true),
+ //field("buzz", "auto", true, true),
+ field("bar.*", "string*", true, true),
+ field("baz.*", "auto", true, true),
+ };
+
+ coll1 = collectionManager.get_collection("coll1").get();
+ if (coll1 == nullptr) {
+ auto coll_op = collectionManager.create_collection("coll1", 1, fields, "", 0);
+ ASSERT_TRUE(coll_op.ok());
+ coll1 = coll_op.get();
+ }
+
+ nlohmann::json doc;
+ doc["foo"] = nullptr;
+ //doc["buzz"] = nullptr;
+ doc["bar_one"] = nullptr;
+ doc["baz_one"] = nullptr;
+
+ // should allow indexing of null values since all are optional
+ auto add_op = coll1->add(doc.dump(), CREATE);
+ ASSERT_TRUE(add_op.ok());
+
+ collectionManager.drop_collection("coll1");
+}
+
TEST_F(CollectionAllFieldsTest, BothFallbackAndDynamicFields) {
Collection *coll1;