mirror of
https://github.com/typesense/typesense.git
synced 2025-05-23 15:23:40 +08:00
Add guards for null values for optional fields.
This commit is contained in:
parent
39072af55b
commit
ab9936f96b
@ -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);
|
||||
|
@ -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",
|
||||
|
Loading…
x
Reference in New Issue
Block a user