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 remove empty array of obj elements for highlight.

Kishore Nallana121f95bd8c7
diff --git a/src/collection.cpp b/src/collection.cpp
index 5a1d8a54..db5f9046 100644
--- a/src/collection.cpp
+++ b/src/collection.cpp
@@ -3535,10 +3535,7 @@ void Collection::prune_doc(nlohmann::json& doc,
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(!orig_ele_empty && arr_it.value().empty()) {
- arr_it = it.value().erase(arr_it);
- continue;
- }
+ // don't remove empty array objects to help frontend
}
arr_it++;
diff --git a/test/collection_nested_fields_test.cpp b/test/collection_nested_fields_test.cpp
index 678b93bb..49a7eef9 100644
--- a/test/collection_nested_fields_test.cpp
+++ b/test/collection_nested_fields_test.cpp
@@ -615,7 +615,8 @@ TEST_F(CollectionNestedFieldsTest, IncludeExcludeFieldsPruning) {
auto doc = nlohmann::json::parse(doc_str);
Collection::prune_doc(doc, tsl::htrie_set<char>(), {"one_obj_arr.foo"});
- ASSERT_EQ(0, doc.count("one_obj_arr"));
+ ASSERT_EQ(1, doc.count("one_obj_arr"));
+ ASSERT_EQ(1, doc["one_obj_arr"].size());
// handle non-existing exclude field
doc = nlohmann::json::parse(doc_str);
@@ -637,7 +638,7 @@ TEST_F(CollectionNestedFieldsTest, IncludeExcludeFieldsPruning) {
// exclusion takes preference
doc = nlohmann::json::parse(doc_str);
Collection::prune_doc(doc, {"locations.address.city"}, {"locations.address.city"});
- ASSERT_EQ(R"({})", doc.dump());
+ ASSERT_EQ(R"({"locations":[{},{}]})", doc.dump());
// include object, exclude sub-fields
doc = nlohmann::json::parse(doc_str);
@@ -2327,12 +2328,105 @@ TEST_F(CollectionNestedFieldsTest, UpdateNestedDocument) {
results = coll1->search("shaw", {"contributors"}, "", {}, {}, {0}, 10, 1, FREQUENCY, {false}).get();
ASSERT_EQ(1, results["found"].get<size_t>());
+ results = coll1->search("john", {"contributors.first_name"}, "", {}, {}, {0}, 10, 1, FREQUENCY, {false}).get();
+ ASSERT_EQ(1, results["found"].get<size_t>());
+
// should not be able to find the old name
results = coll1->search("galt", {"contributors"}, "", {}, {}, {0}, 10, 1, FREQUENCY, {false}).get();
ASSERT_EQ(0, results["found"].get<size_t>());
}
+TEST_F(CollectionNestedFieldsTest, UpdateNestedDocumentAutoSchema) {
+ nlohmann::json schema = R"({
+ "name": "coll1",
+ "enable_nested_fields": true,
+ "fields": [
+ {"name": ".*", "type": "auto"}
+ ]
+ })"_json;
+
+ auto op = collectionManager.create_collection(schema);
+ ASSERT_TRUE(op.ok());
+ Collection* coll1 = op.get();
+
+ auto doc1 = R"({
+ "id": "0",
+ "price": {"now": 3000, "country": "US"}
+ })"_json;
+
+ auto add_op = coll1->add(doc1.dump(), CREATE);
+ ASSERT_TRUE(add_op.ok());
+
+ // update document partially
+
+ doc1 = R"({
+ "id": "0",
+ "price": {"now": 4000}
+ })"_json;
+
+ add_op = coll1->add(doc1.dump(), UPDATE);
+ ASSERT_TRUE(add_op.ok());
+
+ auto results = coll1->search("us", {"price.country"}, "", {}, {}, {0}, 10, 1, FREQUENCY, {false}).get();
+ ASSERT_EQ(1, results["found"].get<size_t>());
+}
+
+TEST_F(CollectionNestedFieldsTest, HighlightArrayOfObjects) {
+ nlohmann::json schema = R"({
+ "name": "coll1",
+ "enable_nested_fields": true,
+ "fields": [
+ {"name": ".*", "type": "auto"}
+ ]
+ })"_json;
+
+ auto op = collectionManager.create_collection(schema);
+ ASSERT_TRUE(op.ok());
+ Collection* coll1 = op.get();
+
+ auto doc1 = R"({
+ "details": [
+ {"foo": "John Smith"},
+ {"name": "James Peterson"},
+ {"bar": "John Galt"}
+ ]
+ })"_json;
+
+ auto add_op = coll1->add(doc1.dump(), CREATE);
+ ASSERT_TRUE(add_op.ok());
+
+ auto results = coll1->search("james", {"details.name"}, "", {}, {}, {0}, 10, 1, FREQUENCY,
+ {true}, 1, spp::sparse_hash_set<std::string>(),
+ spp::sparse_hash_set<std::string>(), 10, "", 30, 4).get();
+ ASSERT_EQ(1, results["found"].get<size_t>());
+ ASSERT_EQ(3, results["hits"][0]["highlight"]["details"].size());
+ ASSERT_EQ(0, results["hits"][0]["highlight"]["details"][0].size());
+ ASSERT_EQ(1, results["hits"][0]["highlight"]["details"][1].size());
+ ASSERT_EQ(0, results["hits"][0]["highlight"]["details"][2].size());
+
+ results = coll1->search("james", {"details.name"}, "", {}, {}, {0}, 10, 1, FREQUENCY,
+ {true}, 1, spp::sparse_hash_set<std::string>(),
+ spp::sparse_hash_set<std::string>(), 10, "", 30, 4, "", 1, {}, {}, {}, 0,
+ "<mark>", "</mark>", {1}, 10000, true, false, true, "details.name").get();
+
+ ASSERT_EQ(1, results["found"].get<size_t>());
+ ASSERT_EQ(0, results["hits"][0]["highlight"]["details"][0].size());
+ ASSERT_EQ(1, results["hits"][0]["highlight"]["details"][1].size());
+ ASSERT_EQ(0, results["hits"][0]["highlight"]["details"][2].size());
+
+ results = coll1->search("james", {"details.name"}, "", {}, {}, {0}, 10, 1, FREQUENCY,
+ {true}, 1, spp::sparse_hash_set<std::string>(),
+ spp::sparse_hash_set<std::string>(), 10, "", 30, 4, "", 1, {}, {}, {}, 0,
+ "<mark>", "</mark>", {1}, 10000, true, false, true, "details").get();
+
+ ASSERT_EQ(1, results["found"].get<size_t>());
+ ASSERT_EQ(3, results["hits"][0]["highlight"]["details"].size());
+ ASSERT_EQ(1, results["hits"][0]["highlight"]["details"][0].size());
+ ASSERT_EQ(1, results["hits"][0]["highlight"]["details"][1].size());
+ ASSERT_EQ(1, results["hits"][0]["highlight"]["details"][2].size());
+}
+
TEST_F(CollectionNestedFieldsTest, HighlightOnFlatFieldWithSnippeting) {
std::vector<field> fields = {field("title", field_types::STRING, false),
field("body", field_types::STRING, false)};