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
C++ algoliadatastoreelasticsearchenterprise-searchfacetingfull-text-searchfuzzy-searchgeosearchin-memoryinstantsearchmerchandisingpineconesearchsearch-enginesemantic-searchsimilarity-searchsite-searchsynonymstypo-tolerancevector-search
Default fields to highlight must respect include_fields.
Kishore Nallanf4d3ecc84ed2
diff --git a/include/collection.h b/include/collection.hindex 2d748be7..665f3f73 100644--- a/include/collection.h+++ b/include/collection.h@@ -463,6 +463,7 @@ public:void process_highlight_fields(const std::vector<std::string>& search_fields,const spp::sparse_hash_set<std::string>& exclude_fields,+ const spp::sparse_hash_set<std::string>& include_fields,const string& highlight_fields,const std::string& highlight_full_fields,const std::vector<infix_t>& infixes,diff --git a/src/collection.cpp b/src/collection.cppindex 3887a562..d1ec8b0b 100644--- a/src/collection.cpp+++ b/src/collection.cpp@@ -1152,8 +1152,8 @@ Option<nlohmann::json> Collection::search(const std::string & raw_query, const sstd::vector<highlight_field_t> highlight_items;if(query != "*") {- process_highlight_fields(search_fields, exclude_fields, highlight_fields, highlight_full_fields,- infixes, highlight_items);+ process_highlight_fields(search_fields, include_fields, exclude_fields,+ highlight_fields, highlight_full_fields, infixes, highlight_items);}nlohmann::json result = nlohmann::json::object();@@ -1457,6 +1457,7 @@ Option<nlohmann::json> Collection::search(const std::string & raw_query, const s}void Collection::process_highlight_fields(const std::vector<std::string>& search_fields,+ const spp::sparse_hash_set<std::string>& include_fields,const spp::sparse_hash_set<std::string>& exclude_fields,const string& highlight_fields,const std::string& highlight_full_fields,@@ -1490,6 +1491,12 @@ void Collection::process_highlight_fields(const std::vector<std::string>& search// should not pick excluded field for highlighting (only for implicit highlighting)continue;}++ if(!include_fields.empty() && include_fields.count(field_name) == 0) {+ // if include fields have been specified, use that as allow list+ continue;+ }+bool fully_highlighted = (fields_highlighted_fully_set.count(field_name) != 0);bool infixed = (fields_infixed_set.count(field_name) != 0);highlight_items.emplace_back(field_name, fully_highlighted, infixed);diff --git a/test/collection_specific_test.cpp b/test/collection_specific_test.cppindex 5ab05b28..0fd2dde7 100644--- a/test/collection_specific_test.cpp+++ b/test/collection_specific_test.cpp@@ -2568,3 +2568,28 @@ TEST_F(CollectionSpecificTest, RepeatedTokensInArray) {collectionManager.drop_collection("coll1");}+TEST_F(CollectionSpecificTest, HighlightOnlyIncludedFields) {+ std::vector<field> fields = {field("title", field_types::STRING, false),+ field("description", field_types::STRING, false),};++ Collection* coll1 = collectionManager.create_collection("coll1", 1, fields).get();++ nlohmann::json doc1;+ doc1["id"] = "0";+ doc1["title"] = "Harry";+ doc1["description"] = "Harry Roscow";++ ASSERT_TRUE(coll1->add(doc1.dump()).ok());++ spp::sparse_hash_set<std::string> include_fields = {"title"};+ auto results = coll1->search("harry", {"title", "description"},+ "", {}, {}, {2}, 10, 1, FREQUENCY, {true}, 10, include_fields).get();++ ASSERT_EQ(1, results["hits"].size());+ ASSERT_EQ(1, results["hits"][0]["highlights"].size());+ ASSERT_EQ("title", results["hits"][0]["highlights"][0]["field"].get<std::string>());++ collectionManager.drop_collection("coll1");+}++