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 clean deletion of nested fields

Kishore Nallandde82cc8de55
diff --git a/src/collection.cpp b/src/collection.cpp
index f24e1af5..b5310d46 100644
--- a/src/collection.cpp
+++ b/src/collection.cpp
@@ -3301,23 +3301,16 @@ Option<std::string> Collection::remove(const std::string & id, const bool remove
uint32_t seq_id = (uint32_t) std::stoul(seq_id_str);
- std::string parsed_document;
- StoreStatus doc_status = store->get(get_seq_id_key(seq_id), parsed_document);
-
- if(doc_status == StoreStatus::NOT_FOUND) {
- LOG(ERROR) << "Sequence ID exists, but document is missing for id: " << id;
- return Option<std::string>(404, "Could not find a document with id: " + id);
- }
+ nlohmann::json document;
+ auto get_doc_op = get_document_from_store(get_seq_id_key(seq_id), document);
- if(doc_status == StoreStatus::ERROR) {
- return Option<std::string>(500, "Error while fetching the document.");
- }
+ if(!get_doc_op.ok()) {
+ if(get_doc_op.code() == 404) {
+ LOG(ERROR) << "Sequence ID exists, but document is missing for id: " << id;
+ return Option<std::string>(404, "Could not find a document with id: " + id);
+ }
- nlohmann::json document;
- try {
- document = nlohmann::json::parse(parsed_document);
- } catch(...) {
- return Option<std::string>(500, "Error while parsing stored document.");
+ return Option<std::string>(get_doc_op.code(), get_doc_op.error());
}
remove_document(document, seq_id, remove_from_store);
@@ -3325,23 +3318,16 @@ Option<std::string> Collection::remove(const std::string & id, const bool remove
}
Option<bool> Collection::remove_if_found(uint32_t seq_id, const bool remove_from_store) {
- std::string parsed_document;
- StoreStatus doc_status = store->get(get_seq_id_key(seq_id), parsed_document);
+ nlohmann::json document;
+ auto get_doc_op = get_document_from_store(get_seq_id_key(seq_id), document);
- if(doc_status == StoreStatus::NOT_FOUND) {
- return Option<bool>(false);
- }
+ if(!get_doc_op.ok()) {
+ if(get_doc_op.code() == 404) {
+ return Option<bool>(false);
+ }
- if(doc_status == StoreStatus::ERROR) {
return Option<bool>(500, "Error while fetching the document with seq id: " +
- std::to_string(seq_id));
- }
-
- nlohmann::json document;
- try {
- document = nlohmann::json::parse(parsed_document);
- } catch(...) {
- return Option<bool>(500, "Error while parsing stored document.");
+ std::to_string(seq_id));
}
remove_document(document, seq_id, remove_from_store);
@@ -3502,7 +3488,11 @@ Option<bool> Collection::get_document_from_store(const std::string &seq_id_key,
if(json_doc_status != StoreStatus::FOUND) {
const std::string& seq_id = std::to_string(get_seq_id_from_key(seq_id_key));
- return Option<bool>(500, "Could not locate the JSON document for sequence ID: " + seq_id);
+ if(json_doc_status == StoreStatus::NOT_FOUND) {
+ return Option<bool>(404, "Could not locate the JSON document for sequence ID: " + seq_id);
+ }
+
+ return Option<bool>(500, "Error while fetching JSON document for sequence ID: " + seq_id);
}
try {
diff --git a/test/collection_nested_fields_test.cpp b/test/collection_nested_fields_test.cpp
index 49a7eef9..39c59030 100644
--- a/test/collection_nested_fields_test.cpp
+++ b/test/collection_nested_fields_test.cpp
@@ -1382,6 +1382,22 @@ TEST_F(CollectionNestedFieldsTest, FieldsWithExplicitSchema) {
ASSERT_TRUE(coll_field.optional);
}
}
+
+ // deleting doc from coll1 and try querying again
+ coll1->remove("0");
+ results = coll1->search("brown", {"locations.address"},
+ "", {}, sort_fields, {0}, 10, 1,
+ token_ordering::FREQUENCY, {true}, 10, spp::sparse_hash_set<std::string>(),
+ spp::sparse_hash_set<std::string>(), 10, "", 30, 4).get();
+ ASSERT_EQ(0, results["found"].get<size_t>());
+
+ // use remove_if_found API
+ coll2->remove_if_found(0);
+ results = coll2->search("brown", {"locations.address"},
+ "", {}, sort_fields, {0}, 10, 1,
+ token_ordering::FREQUENCY, {true}, 10, spp::sparse_hash_set<std::string>(),
+ spp::sparse_hash_set<std::string>(), 10, "", 30, 4).get();
+ ASSERT_EQ(0, results["found"].get<size_t>());
}
TEST_F(CollectionNestedFieldsTest, ExplicitSchemaOptionalFieldValidation) {