Add guards for null values for optional fields.

This commit is contained in:
Kishore Nallan 2024-04-07 21:10:35 +05:30
parent 39072af55b
commit ab9936f96b
2 changed files with 63 additions and 1 deletions

View File

@ -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);

View File

@ -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",