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 highlighting when both flat/nested form of field is present.

Kishore Nallana0ee96cdadd9
diff --git a/include/collection.h b/include/collection.h
index d0fdfec9..3b50c435 100644
--- a/include/collection.h
+++ b/include/collection.h
@@ -534,7 +534,9 @@ public:
const tsl::htrie_map<char, token_leaf>& qtoken_set,
std::vector<highlight_field_t>& highlight_items) const;
- static void copy_highlight_doc(std::vector<highlight_field_t>& hightlight_items, const nlohmann::json& src,
+ static void copy_highlight_doc(std::vector<highlight_field_t>& hightlight_items,
+ const bool nested_fields_enabled,
+ const nlohmann::json& src,
nlohmann::json& dst);
Option<bool> alter(nlohmann::json& alter_payload);
diff --git a/src/collection.cpp b/src/collection.cpp
index 08f19217..749e8ab0 100644
--- a/src/collection.cpp
+++ b/src/collection.cpp
@@ -1695,7 +1695,7 @@ Option<nlohmann::json> Collection::search(std::string raw_query,
nlohmann::json highlight_res = nlohmann::json::object();
if(!highlight_items.empty()) {
- copy_highlight_doc(highlight_items, document, highlight_res);
+ copy_highlight_doc(highlight_items, enable_nested_fields, document, highlight_res);
remove_flat_fields(highlight_res);
highlight_res.erase("id");
}
@@ -2058,9 +2058,17 @@ Option<nlohmann::json> Collection::search(std::string raw_query,
return Option<nlohmann::json>(result);
}
-void Collection::copy_highlight_doc(std::vector<highlight_field_t>& hightlight_items, const nlohmann::json& src, nlohmann::json& dst) {
+void Collection::copy_highlight_doc(std::vector<highlight_field_t>& hightlight_items,
+ const bool nested_fields_enabled,
+ const nlohmann::json& src, nlohmann::json& dst) {
for(const auto& hightlight_item: hightlight_items) {
+ if(!nested_fields_enabled && src.count(hightlight_item.name) != 0) {
+ dst[hightlight_item.name] = src[hightlight_item.name];
+ continue;
+ }
+
std::string root_field_name;
+
for(size_t i = 0; i < hightlight_item.name.size(); i++) {
if(hightlight_item.name[i] == '.') {
break;
diff --git a/test/collection_specific_more_test.cpp b/test/collection_specific_more_test.cpp
index 2e9369cf..3d76d37c 100644
--- a/test/collection_specific_more_test.cpp
+++ b/test/collection_specific_more_test.cpp
@@ -1331,11 +1331,51 @@ TEST_F(CollectionSpecificMoreTest, CopyDocHelper) {
})"_json;
nlohmann::json dst;
- Collection::copy_highlight_doc(hightlight_items, src, dst);
+ Collection::copy_highlight_doc(hightlight_items, true, src, dst);
ASSERT_EQ(2, dst.size());
ASSERT_EQ(1, dst.count("baz"));
ASSERT_EQ(1, dst.count("foo.bar"));
+
+ // when both nested & flattened forms are present, copy only flat form for collection without nesting enabled
+ src = R"({
+ "baz": {"name": "John"},
+ "baz.name": "John"
+ })"_json;
+ dst.clear();
+
+ hightlight_items = {
+ highlight_field_t("baz.name", false, false, true),
+ };
+
+ Collection::copy_highlight_doc(hightlight_items, false, src, dst);
+ ASSERT_EQ(1, dst.size());
+ ASSERT_EQ(1, dst.count("baz.name"));
+}
+
+TEST_F(CollectionSpecificMoreTest, HighlightFieldWithBothFlatAndNestedForm) {
+ nlohmann::json schema = R"({
+ "name": "coll1",
+ "fields": [
+ {"name": "name.first", "type": "string"}
+ ]
+ })"_json;
+
+ Collection *coll1 = collectionManager.create_collection(schema).get();
+
+ nlohmann::json doc;
+ doc["name.first"] = "John";
+ doc["name"]["first"] = "John";
+
+ ASSERT_TRUE(coll1->add(doc.dump()).ok());
+
+ auto res = coll1->search("john", {"name.first"}, "", {}, {}, {2}, 10, 1,
+ FREQUENCY, {true},
+ 10, spp::sparse_hash_set<std::string>(),
+ spp::sparse_hash_set<std::string>()).get();
+
+ ASSERT_EQ(1, res["hits"].size());
+ ASSERT_EQ("<mark>John</mark>", res["hits"][0]["highlight"]["name.first"]["snippet"].get<std::string>());
}
TEST_F(CollectionSpecificMoreTest, HighlightWordWithSymbols) {