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

Don't prune actually empty elements.

Kishore Nalland25a021ea97d
diff --git a/src/collection.cpp b/src/collection.cpp
index 4b874fc5..3259d76c 100644
--- a/src/collection.cpp
+++ b/src/collection.cpp
@@ -3508,8 +3508,9 @@ void Collection::prune_doc(nlohmann::json& doc,
}
if(it.value().is_object()) {
+ bool is_orig_empty = it.value().empty();
prune_doc(it.value(), include_names, exclude_names, nested_name, depth+1);
- if(it.value().empty()) {
+ if(!is_orig_empty && it.value().empty()) {
it = doc.erase(it);
} else {
it++;
@@ -3526,8 +3527,9 @@ void Collection::prune_doc(nlohmann::json& doc,
// NOTE: we will not support array of array of nested objects
primitive_array = primitive_array && !arr_it.value().is_object();
if(arr_it.value().is_object()) {
+ bool orig_ele_empty = arr_it.value().empty();
prune_doc(arr_it.value(), include_names, exclude_names, nested_name, depth+1);
- if(arr_it.value().empty()) {
+ if(!orig_ele_empty && arr_it.value().empty()) {
arr_it = it.value().erase(arr_it);
continue;
}
diff --git a/test/collection_nested_fields_test.cpp b/test/collection_nested_fields_test.cpp
index 847ba0c3..685c20db 100644
--- a/test/collection_nested_fields_test.cpp
+++ b/test/collection_nested_fields_test.cpp
@@ -645,6 +645,23 @@ TEST_F(CollectionNestedFieldsTest, IncludeExcludeFieldsPruning) {
ASSERT_EQ(R"({"locations":[{"address":{"products":["shoes","tshirts"]}},{"address":{"products":["sneakers","shoes"]}}]})", doc.dump());
}
+TEST_F(CollectionNestedFieldsTest, ShouldNotPruneEmptyFields) {
+ auto doc_str = R"({
+ "name": "Foo",
+ "obj": {},
+ "obj_arr": [{}],
+ "price": {
+ "per_unit": {},
+ "items": [{}]
+ }
+ })";
+
+ auto doc = nlohmann::json::parse(doc_str);
+ auto expected_doc = doc;
+ Collection::prune_doc(doc, tsl::htrie_set<char>(), tsl::htrie_set<char>());
+ ASSERT_EQ(expected_doc.dump(), doc.dump());
+}
+
TEST_F(CollectionNestedFieldsTest, IncludeFieldsSearch) {
nlohmann::json schema = R"({
"name": "coll1",