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 deletion of records with optional fields.

kishorenc9867ca5e294c
diff --git a/src/index.cpp b/src/index.cpp
index 71dd092b..590793c6 100644
--- a/src/index.cpp
+++ b/src/index.cpp
@@ -1706,6 +1706,10 @@ void Index::remove_and_shift_offset_index(sorted_array &offset_index, const uint
Option<uint32_t> Index::remove(const uint32_t seq_id, nlohmann::json & document) {
for(auto & name_field: search_schema) {
+ if(name_field.second.optional && document.count(name_field.first) == 0) {
+ continue;
+ }
+
// Go through all the field names and find the keys+values so that they can be removed from in-memory index
std::vector<std::string> tokens;
if(name_field.second.type == field_types::STRING) {
diff --git a/test/collection_test.cpp b/test/collection_test.cpp
index 6066da0a..cac9d715 100644
--- a/test/collection_test.cpp
+++ b/test/collection_test.cpp
@@ -2251,6 +2251,10 @@ TEST_F(CollectionTest, OptionalFields) {
auto res_op = coll1->search("*", {"title"}, "", {}, sort_fields, 0, 10, 1, FREQUENCY, false);
ASSERT_FALSE(res_op.ok());
ASSERT_STREQ("Cannot sort by `average` as it is defined as an optional field.", res_op.error().c_str());
+
+ // try deleting a record having optional field
+ Option<std::string> remove_op = coll1->remove("1");
+ ASSERT_TRUE(remove_op.ok());
// default sorting field should not be declared optional
fields = {