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

Excluded fields should not be present in highlights.

kishorencda5b860661a0
diff --git a/src/collection.cpp b/src/collection.cpp
index af5027a1..01c7da07 100644
--- a/src/collection.cpp
+++ b/src/collection.cpp
@@ -743,6 +743,11 @@ Option<nlohmann::json> Collection::search(const std::string & query, const std::
}
for(const std::string & field_name: search_fields) {
+ // should not pick excluded field for highlighting
+ if(exclude_fields.count(field_name) > 0) {
+ continue;
+ }
+
field search_field = search_schema.at(field_name);
if(query != "*" && (search_field.type == field_types::STRING ||
search_field.type == field_types::STRING_ARRAY)) {
diff --git a/test/collection_test.cpp b/test/collection_test.cpp
index bc7fd114..27dea864 100644
--- a/test/collection_test.cpp
+++ b/test/collection_test.cpp
@@ -2175,6 +2175,23 @@ TEST_F(CollectionTest, SearchHighlightFieldFully) {
ASSERT_EQ(1, res["hits"][0]["highlights"][1]["values"][0].size());
ASSERT_STREQ("<mark>LAZY</mark>", res["hits"][0]["highlights"][1]["values"][0].get<std::string>().c_str());
+ // excluded fields should not be returned in highlights section
+ spp::sparse_hash_set<std::string> excluded_fields = {"tags"};
+ res = coll1->search("lazy", {"title", "tags"}, "", {}, sort_fields, 0, 10, 1,
+ token_ordering::FREQUENCY, true, 10, spp::sparse_hash_set<std::string>(),
+ excluded_fields, 10, 500, "", 5, "title, tags").get();
+
+ ASSERT_EQ(1, res["hits"][0]["highlights"].size());
+ ASSERT_STREQ("The quick brown fox jumped over the <mark>lazy</mark> dog and ran straight to the forest to sleep.",
+ res["hits"][0]["highlights"][0]["value"].get<std::string>().c_str());
+
+ // when all fields are excluded
+ excluded_fields = {"tags", "title"};
+ res = coll1->search("lazy", {"title", "tags"}, "", {}, sort_fields, 0, 10, 1,
+ token_ordering::FREQUENCY, true, 10, spp::sparse_hash_set<std::string>(),
+ excluded_fields, 10, 500, "", 5, "title, tags").get();
+ ASSERT_EQ(0, res["hits"][0]["highlights"].size());
+
collectionManager.drop_collection("coll1");
}