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

Handle optional field for emplace.

Kishore Nallancd8c543a0c6b
diff --git a/include/validator.h b/include/validator.h
index a6d51af4..638b5eb3 100644
--- a/include/validator.h
+++ b/include/validator.h
@@ -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);
diff --git a/src/collection.cpp b/src/collection.cpp
index 7609e151..274a4b41 100644
--- a/src/collection.cpp
+++ b/src/collection.cpp
@@ -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()) {
diff --git a/src/index.cpp b/src/index.cpp
index b72b6ca8..8d39085d 100644
--- a/src/index.cpp
+++ b/src/index.cpp
@@ -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);
diff --git a/src/validator.cpp b/src/validator.cpp
index d3c828b7..9616b84b 100644
--- a/src/validator.cpp
+++ b/src/validator.cpp
@@ -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);
}
diff --git a/test/collection_specific_more_test.cpp b/test/collection_specific_more_test.cpp
index 92ee07b2..cae778c0 100644
--- a/test/collection_specific_more_test.cpp
+++ b/test/collection_specific_more_test.cpp
@@ -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",