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

Add guards for null values for optional fields.

Kishore Nallanab9936f96beb
diff --git a/src/index.cpp b/src/index.cpp
index 9cb05e1a..f7ec0232 100644
--- a/src/index.cpp
+++ b/src/index.cpp
@@ -6704,6 +6704,10 @@ void Index::remove_field(uint32_t seq_id, const nlohmann::json& document, const
return;
}
+ if(search_field.optional && document[field_name].is_null()) {
+ return ;
+ }
+
// Go through all the field names and find the keys+values so that they can be removed from in-memory index
if(search_field.type == field_types::STRING_ARRAY || search_field.type == field_types::STRING) {
std::vector<std::string> tokens;
@@ -7158,7 +7162,7 @@ void Index::get_doc_changes(const index_operation_t op, const tsl::htrie_map<cha
if(it.value().is_null()) {
// null values should not be indexed
new_doc.erase(it.key());
- if(old_doc.contains(it.key())) {
+ if(old_doc.contains(it.key()) && !old_doc[it.key()].is_null()) {
del_doc[it.key()] = old_doc[it.key()];
}
it = update_doc.erase(it);
diff --git a/test/collection_nested_fields_test.cpp b/test/collection_nested_fields_test.cpp
index 4e5a1fa0..c3fa76db 100644
--- a/test/collection_nested_fields_test.cpp
+++ b/test/collection_nested_fields_test.cpp
@@ -2984,6 +2984,64 @@ TEST_F(CollectionNestedFieldsTest, UpdateNestedDocumentAutoSchema) {
ASSERT_EQ(1, results["found"].get<size_t>());
}
+TEST_F(CollectionNestedFieldsTest, UpdateNestedDocumentWithOptionalNullValue) {
+ nlohmann::json schema = R"({
+ "name": "coll1",
+ "enable_nested_fields": true,
+ "fields": [
+ {"name": "contributors", "type": "object", "optional": true},
+ {"name": "title", "type": "string", "optional": false}
+ ]
+ })"_json;
+
+ auto op = collectionManager.create_collection(schema);
+ ASSERT_TRUE(op.ok());
+ Collection* coll1 = op.get();
+
+ auto doc1 = R"({
+ "id": "0",
+ "title": "Title Alpha",
+ "contributors": {"first_name": "John", "last_name": null}
+ })"_json;
+
+ auto add_op = coll1->add(doc1.dump(), CREATE);
+ ASSERT_TRUE(add_op.ok());
+
+ // update document partially
+
+ doc1 = R"({
+ "id": "0",
+ "title": "Title Beta",
+ "contributors": {"first_name": "Jack", "last_name": null}
+ })"_json;
+
+ add_op = coll1->add(doc1.dump(), UPDATE);
+ ASSERT_TRUE(add_op.ok());
+
+ auto results = coll1->search("beta", {"title"}, "", {}, {}, {0}, 10, 1, FREQUENCY, {false}).get();
+ ASSERT_EQ(1, results["found"].get<size_t>());
+
+ // emplace document partially
+
+ doc1 = R"({
+ "id": "0",
+ "title": "Title Gamma",
+ "contributors": {"first_name": "Jim", "last_name": null}
+ })"_json;
+
+ add_op = coll1->add(doc1.dump(), EMPLACE);
+ ASSERT_TRUE(add_op.ok());
+
+ results = coll1->search("gamma", {"title"}, "", {}, {}, {0}, 10, 1, FREQUENCY, {false}).get();
+ ASSERT_EQ(1, results["found"].get<size_t>());
+
+ // remove field with null value
+ auto del_op = coll1->remove("0");
+ ASSERT_TRUE(del_op.ok());
+ results = coll1->search("gamma", {"title"}, "", {}, {}, {0}, 10, 1, FREQUENCY, {false}).get();
+ ASSERT_EQ(0, results["found"].get<size_t>());
+}
+
TEST_F(CollectionNestedFieldsTest, ImproveErrorMessageForNestedArrayNumericalFields) {
nlohmann::json schema = R"({
"name": "coll1",