Handle optional field for emplace.

This commit is contained in:
Kishore Nallan 2023-04-05 18:00:20 +05:30
parent 2474ac0625
commit cd8c543a0c
5 changed files with 23 additions and 9 deletions

View File

@ -28,6 +28,7 @@ public:
const std::string & default_sorting_field,
const tsl::htrie_map<char, field> & search_schema,
const index_operation_t op,
const bool is_update,
const std::string& fallback_field_type,
const DIRTY_VALUES& dirty_values);

View File

@ -568,7 +568,7 @@ Option<uint32_t> Collection::index_in_memory(nlohmann::json &document, uint32_t
std::unique_lock lock(mutex);
Option<uint32_t> validation_op = validator_t::validate_index_in_memory(document, seq_id, default_sorting_field,
search_schema, op,
search_schema, op, false,
fallback_field_type, dirty_values);
if(!validation_op.ok()) {
@ -1326,12 +1326,6 @@ Option<nlohmann::json> Collection::search(std::string raw_query,
}
}
// check for valid pagination
if(page != 0 && page < 1) {
std::string message = "Page must be an integer of value greater than 0.";
return Option<nlohmann::json>(422, message);
}
if(per_page > PER_PAGE_MAX) {
std::string message = "Only upto " + std::to_string(PER_PAGE_MAX) + " hits can be fetched per page.";
return Option<nlohmann::json>(422, message);
@ -4188,6 +4182,7 @@ Option<bool> Collection::validate_alter_payload(nlohmann::json& schema_changes,
auto validate_op = validator_t::validate_index_in_memory(document, seq_id, default_sorting_field,
updated_search_schema,
index_operation_t::CREATE,
false,
fallback_field_type,
DIRTY_VALUES::REJECT);
if(!validate_op.ok()) {

View File

@ -436,6 +436,7 @@ void Index::validate_and_preprocess(Index *index, std::vector<index_record>& ite
default_sorting_field,
search_schema,
index_rec.operation,
index_rec.is_update,
fallback_field_type,
index_rec.dirty_values);

View File

@ -530,12 +530,13 @@ Option<uint32_t> validator_t::validate_index_in_memory(nlohmann::json& document,
const std::string & default_sorting_field,
const tsl::htrie_map<char, field> & search_schema,
const index_operation_t op,
const bool is_update,
const std::string& fallback_field_type,
const DIRTY_VALUES& dirty_values) {
bool missing_default_sort_field = (!default_sorting_field.empty() && document.count(default_sorting_field) == 0);
if((op != UPDATE && op != EMPLACE) && missing_default_sort_field) {
if((op == CREATE || op == UPSERT) && missing_default_sort_field) {
return Option<>(400, "Field `" + default_sorting_field + "` has been declared as a default sorting field, "
"but is not found in the document.");
}
@ -560,7 +561,7 @@ Option<uint32_t> validator_t::validate_index_in_memory(nlohmann::json& document,
if(a_field.optional && doc_ele.is_null()) {
// we will ignore `null` on an option field
if(op != UPDATE && op != EMPLACE) {
if(!is_update) {
// for updates, the erasure is done later since we need to keep the key for overwrite
document.erase(field_name);
}

View File

@ -1746,6 +1746,22 @@ TEST_F(CollectionSpecificMoreTest, WildcardIncludeExclude) {
ASSERT_EQ(1, result["hits"][0]["document"].count("username"));
}
TEST_F(CollectionSpecificMoreTest, EmplaceWithNullValue) {
nlohmann::json schema = R"({
"name": "coll1",
"fields": [
{"name": "is_valid", "type": "bool", "optional": true}
]
})"_json;
Collection *coll1 = collectionManager.create_collection(schema).get();
nlohmann::json doc;
doc["id"] = "0";
doc["is_valid"] = nullptr;
ASSERT_TRUE(coll1->add(doc.dump(), EMPLACE).ok());
}
TEST_F(CollectionSpecificMoreTest, PhraseMatchRepeatingTokens) {
nlohmann::json schema = R"({
"name": "coll1",