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

Ensure that only highlighted fields exist in snippet.

Kishore Nallanb7e33584fbae
diff --git a/include/collection.h b/include/collection.h
index 3dd7b746..4d5d3ae4 100644
--- a/include/collection.h
+++ b/include/collection.h
@@ -481,6 +481,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,
+ nlohmann::json& dst);
+
Option<bool> alter(nlohmann::json& alter_payload);
void
diff --git a/src/collection.cpp b/src/collection.cpp
index e6f34b81..aed6cb1d 100644
--- a/src/collection.cpp
+++ b/src/collection.cpp
@@ -1390,17 +1390,19 @@ Option<nlohmann::json> Collection::search(const std::string & raw_query,
continue;
}
- nlohmann::json highlight_res;
+ nlohmann::json highlight_res = nlohmann::json::object();
if(!highlight_items.empty()) {
highlight_res["meta"] = nlohmann::json::object();
+ highlight_res["snippet"] = nlohmann::json::object();
+ copy_highlight_doc(highlight_items, document, highlight_res["snippet"]);
highlight_res["snippet"] = document;
remove_flat_fields(highlight_res["snippet"]);
highlight_res["snippet"].erase("id");
if(has_atleast_one_fully_highlighted_field) {
- highlight_res["full"] = document;
+ copy_highlight_doc(highlight_items, document, highlight_res["full"]);
remove_flat_fields(highlight_res["full"]);
highlight_res["full"].erase("id");
} else {
@@ -1469,9 +1471,12 @@ Option<nlohmann::json> Collection::search(const std::string & raw_query,
}
// remove fields from highlight doc that were not highlighted
- if(!highlight_items.empty()) {
+ if(!hfield_names.empty()) {
prune_doc(highlight_res["snippet"], hfield_names, tsl::htrie_set<char>(), "");
prune_doc(highlight_res["full"], h_full_field_names, tsl::htrie_set<char>(), "");
+ } else {
+ highlight_res["snippet"] = nlohmann::json::object();
+ highlight_res["full"] = nlohmann::json::object();
}
std::sort(highlights.begin(), highlights.end());
@@ -1727,6 +1732,32 @@ Option<nlohmann::json> Collection::search(const 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) {
+ for(const auto& hightlight_item: hightlight_items) {
+ std::string root_field_name;
+ for(size_t i = 0; i < hightlight_item.name.size(); i++) {
+ if(hightlight_item.name[i] == '.') {
+ break;
+ }
+
+ root_field_name += hightlight_item.name[i];
+ }
+
+ if(dst.count(root_field_name) != 0) {
+ // skip if parent "foo" has already has been copied over in e.g. foo.bar, foo.baz
+ continue;
+ }
+
+ // 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 {
+ dst[root_field_name] = src[root_field_name];
+ }
+ }
+}
+
void Collection::process_search_field_weights(const std::vector<std::string>& raw_search_fields,
std::vector<uint32_t>& query_by_weights,
std::vector<search_field_t>& weighted_search_fields,
diff --git a/test/collection_nested_fields_test.cpp b/test/collection_nested_fields_test.cpp
index 13450f88..edc31d02 100644
--- a/test/collection_nested_fields_test.cpp
+++ b/test/collection_nested_fields_test.cpp
@@ -750,7 +750,9 @@ TEST_F(CollectionNestedFieldsTest, HighlightShouldHaveMeta) {
20, {}, {}, {}, 0, "<mark>", "</mark>", {}, 1000, true, false, true,
"x").get();
- ASSERT_EQ(0, results["hits"][0]["highlight"].size());
+ ASSERT_EQ(2, results["hits"][0]["highlight"].size());
+ ASSERT_EQ(0, results["hits"][0]["highlight"]["snippet"].size());
+ ASSERT_EQ(0, results["hits"][0]["highlight"]["full"].size());
}
TEST_F(CollectionNestedFieldsTest, FieldsWithExplicitSchema) {
diff --git a/test/collection_specific_more_test.cpp b/test/collection_specific_more_test.cpp
index 4c2e55c7..35319ec8 100644
--- a/test/collection_specific_more_test.cpp
+++ b/test/collection_specific_more_test.cpp
@@ -1137,3 +1137,37 @@ TEST_F(CollectionSpecificMoreTest, UnorderedWeightingOfFields) {
ASSERT_TRUE(res_op.ok());
ASSERT_EQ(0, res_op.get()["hits"].size());
}
+
+TEST_F(CollectionSpecificMoreTest, HighlightObjectShouldBeEmptyWhenNoHighlightFieldFound) {
+ nlohmann::json schema = R"({
+ "name": "coll1",
+ "fields": [
+ {"name": "title", "type": "string"},
+ {"name": "brand", "type": "string"},
+ {"name": "sku", "type": "string"}
+ ]
+ })"_json;
+
+ Collection* coll1 = collectionManager.create_collection(schema).get();
+
+ nlohmann::json doc;
+ doc["title"] = "42f05db9-373a-4372-9bd0-ff4b5aaba28d";
+ doc["brand"] = "brand";
+ doc["sku"] = "rgx761";
+
+ ASSERT_TRUE(coll1->add(doc.dump()).ok());
+
+ auto res_op = coll1->search("brand", {"title", "brand", "sku"}, "", {}, {}, {2, 2, 0}, 10, 1,
+ FREQUENCY, {true},
+ 10, spp::sparse_hash_set<std::string>(),
+ spp::sparse_hash_set<std::string>(), 10, "", 30, 4, "locations.address",
+ 20, {}, {}, {}, 0, "<mark>", "</mark>", {}, 1000, true, false, true,
+ "title");
+
+ ASSERT_TRUE(res_op.ok());
+ auto res = res_op.get();
+ ASSERT_EQ(1, res["hits"].size());
+
+ ASSERT_TRUE(res["hits"][0]["highlight"]["snippet"].empty());
+ LOG(INFO) << res;
+}
\ No newline at end of file