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

Fix copy doc helper: handle missing keys.

Kishore Nallan729c73bb6dbd
diff --git a/src/collection.cpp b/src/collection.cpp
index 0c205105..26ef40ff 100644
--- a/src/collection.cpp
+++ b/src/collection.cpp
@@ -1776,11 +1776,11 @@ void Collection::copy_highlight_doc(std::vector<highlight_field_t>& hightlight_i
}
// root field name might not exist if object has primitive field values with "."s in the name
- if(src.count(root_field_name) == 0) {
- // copy the full field
- dst[hightlight_item.name] = src[hightlight_item.name];
- } else {
+ if(src.count(root_field_name) != 0) {
+ // copy whole sub-object
dst[root_field_name] = src[root_field_name];
+ } else if(src.count(hightlight_item.name) != 0) {
+ dst[hightlight_item.name] = src[hightlight_item.name];
}
}
}
diff --git a/test/collection_specific_more_test.cpp b/test/collection_specific_more_test.cpp
index 07381fbc..80eaa25f 100644
--- a/test/collection_specific_more_test.cpp
+++ b/test/collection_specific_more_test.cpp
@@ -1185,6 +1185,26 @@ TEST_F(CollectionSpecificMoreTest, QueryWithOnlySpecialChars) {
ASSERT_EQ("0", res["hits"][0]["document"]["id"].get<std::string>());
}
+TEST_F(CollectionSpecificMoreTest, CopyDocHelper) {
+ std::vector<highlight_field_t> hightlight_items = {
+ highlight_field_t("foo.bar", false, false),
+ highlight_field_t("baz", false, false),
+ highlight_field_t("not-found", false, false),
+ };
+
+ nlohmann::json src = R"({
+ "baz": {"name": "John"},
+ "foo.bar": 12345
+ })"_json;
+
+ nlohmann::json dst;
+ Collection::copy_highlight_doc(hightlight_items, src, dst);
+
+ ASSERT_EQ(2, dst.size());
+ ASSERT_EQ(1, dst.count("baz"));
+ ASSERT_EQ(1, dst.count("foo.bar"));
+}
+
TEST_F(CollectionSpecificMoreTest, HighlightObjectShouldBeEmptyWhenNoHighlightFieldFound) {
nlohmann::json schema = R"({
"name": "coll1",
@@ -1216,5 +1236,4 @@ TEST_F(CollectionSpecificMoreTest, HighlightObjectShouldBeEmptyWhenNoHighlightFi
ASSERT_EQ(1, res["hits"].size());
ASSERT_TRUE(res["hits"][0]["highlight"]["snippet"].empty());
- LOG(INFO) << res;
-}
\ No newline at end of file
+}