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 some highlighting quirks.

Kishore Nallan16c681223c35
diff --git a/src/collection.cpp b/src/collection.cpp
index ad186d3e..c28c2d78 100644
--- a/src/collection.cpp
+++ b/src/collection.cpp
@@ -1783,6 +1783,8 @@ Option<nlohmann::json> Collection::search(std::string raw_query,
// remove fields from highlight doc that were not highlighted
if(!hfield_names.empty()) {
prune_doc(highlight_res, hfield_names, tsl::htrie_set<char>(), "");
+ } else {
+ highlight_res.clear();
}
if(enable_highlight_v1) {
@@ -2798,7 +2800,9 @@ void Collection::highlight_result(const std::string& raw_query, const field &sea
highlight_nested_field(highlight_doc, highlight_doc, path_parts, 0, false, -1,
[&](nlohmann::json& h_obj, bool is_arr_obj_ele, int array_i) {
- if(!h_obj.is_string()) {
+ if(h_obj.is_object()) {
+ return ;
+ } else if(!h_obj.is_string()) {
auto val_back = h_obj;
h_obj = nlohmann::json::object();
h_obj["snippet"] = to_string(val_back);
diff --git a/test/collection_specific_more_test.cpp b/test/collection_specific_more_test.cpp
index 778b2ff0..5fc7e353 100644
--- a/test/collection_specific_more_test.cpp
+++ b/test/collection_specific_more_test.cpp
@@ -2033,6 +2033,31 @@ TEST_F(CollectionSpecificMoreTest, ExhaustiveSearchWithoutExplicitDropTokens) {
ASSERT_EQ(2, res["hits"].size());
}
+TEST_F(CollectionSpecificMoreTest, DoNotHighlightFieldsForSpecialCharacterQuery) {
+ nlohmann::json schema = R"({
+ "name": "coll1",
+ "fields": [
+ {"name": "title", "type": "string"},
+ {"name": "description", "type": "string"}
+ ]
+ })"_json;
+
+ Collection* coll1 = collectionManager.create_collection(schema).get();
+
+ nlohmann::json doc;
+ doc["title"] = "alpha beta gamma";
+ doc["description"] = "alpha beta gamma";
+ ASSERT_TRUE(coll1->add(doc.dump()).ok());
+
+ auto res = coll1->search("'", {"title", "description"}, "", {}, {}, {0}, 3, 1, FREQUENCY, {false}, 1,
+ spp::sparse_hash_set<std::string>(),
+ spp::sparse_hash_set<std::string>()).get();
+
+ ASSERT_EQ(1, res["hits"].size());
+ ASSERT_EQ(0, res["hits"][0]["highlight"].size());
+ ASSERT_EQ(0, res["hits"][0]["highlights"].size());
+}
+
TEST_F(CollectionSpecificMoreTest, CrossFieldTypoAndPrefixWithWeights) {
nlohmann::json schema = R"({
"name": "coll1",
diff --git a/test/collection_test.cpp b/test/collection_test.cpp
index 43572401..7fe37ac7 100644
--- a/test/collection_test.cpp
+++ b/test/collection_test.cpp
@@ -4450,20 +4450,15 @@ TEST_F(CollectionTest, WildcardHighlightFields) {
spp::sparse_hash_set<std::string> dummy_include_exclude;
std::string highlight_fields = "user*";
// user* matches user_name, user.rank and user.phone
- auto result = coll->search("+91", {"user"}, "", {}, {}, {0},
+ auto result = coll->search("123", {"user"}, "", {}, {}, {0},
10, 1, FREQUENCY, {true}, Index::DROP_TOKENS_THRESHOLD, dummy_include_exclude, dummy_include_exclude, 10, "",
30, 4, "", Index::TYPO_TOKENS_THRESHOLD, "", "", {}, 3, "<mark>", "</mark>", {}, UINT32_MAX,
true, false, true, highlight_fields).get();
ASSERT_EQ(1, result["found"].get<size_t>());
ASSERT_EQ(1, result["hits"].size());
-
-// ASSERT_EQ("+<mark>91</mark> 123123123",
-// result["hits"][0]["highlight"]["user"]["phone"]["snippet"].get<std::string>());
-// ASSERT_EQ("100",
-// result["hits"][0]["highlight"]["user"]["rank"]["snippet"].get<std::string>());
- ASSERT_EQ("user_a",
- result["hits"][0]["highlight"]["user_name"]["snippet"].get<std::string>());
+ ASSERT_EQ(1, result["hits"][0]["highlight"].size());
+ ASSERT_EQ("+91 <mark>123</mark>123123", result["hits"][0]["highlight"]["user"]["phone"]["snippet"].get<std::string>());
highlight_fields = "user.*";
// user.* matches user.rank and user.phone
@@ -4474,11 +4469,9 @@ TEST_F(CollectionTest, WildcardHighlightFields) {
ASSERT_EQ(1, result["found"].get<size_t>());
ASSERT_EQ(1, result["hits"].size());
-
+ ASSERT_EQ(1, result["hits"][0]["highlight"].size());
ASSERT_EQ("+<mark>91</mark> 123123123",
result["hits"][0]["highlight"]["user"]["phone"]["snippet"].get<std::string>());
-// ASSERT_EQ("100",
-// result["hits"][0]["highlight"]["user"]["rank"]["snippet"].get<std::string>());
highlight_fields = "user*";
// user* matches user_name, user.rank and user.phone
@@ -4489,11 +4482,7 @@ TEST_F(CollectionTest, WildcardHighlightFields) {
ASSERT_EQ(1, result["found"].get<size_t>());
ASSERT_EQ(1, result["hits"].size());
-
-// ASSERT_EQ("+91 123123123",
-// result["hits"][0]["highlight"]["user"]["phone"]["snippet"].get<std::string>());
-// ASSERT_EQ("100",
-// result["hits"][0]["highlight"]["user"]["rank"]["snippet"].get<std::string>());
+ ASSERT_EQ(1, result["hits"][0]["highlight"].size());
ASSERT_EQ("<mark>user_a</mark>",
result["hits"][0]["highlight"]["user_name"]["snippet"].get<std::string>());
@@ -4506,11 +4495,7 @@ TEST_F(CollectionTest, WildcardHighlightFields) {
ASSERT_EQ(1, result["found"].get<size_t>());
ASSERT_EQ(1, result["hits"].size());
-
- ASSERT_EQ("+91 123123123",
- result["hits"][0]["highlight"]["user"]["phone"]["snippet"].get<std::string>());
- ASSERT_EQ("100",
- result["hits"][0]["highlight"]["user"]["rank"]["snippet"].get<std::string>());
+ ASSERT_EQ(0, result["hits"][0]["highlight"].size());
highlight_fields = "foo*";
// No matching field for highlight_fields