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

Allow highlighting to be skipped via non-indexed field.

Kishore Nallan3eed0d509e48
diff --git a/src/collection.cpp b/src/collection.cpp
index f2268a72..a5389753 100644
--- a/src/collection.cpp
+++ b/src/collection.cpp
@@ -1070,6 +1070,10 @@ Option<nlohmann::json> Collection::search(const std::string & raw_query, const s
const std::string& field_name = fields_highlighted_vec[i];
const std::vector<std::string>& q_tokens = field_query_tokens[fields_highlighted_indices[i]].q_include_tokens;
+ if(search_schema.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)) {
@@ -1369,6 +1373,7 @@ bool Collection::facet_value_to_string(const facet &a_facet, const facet_count_t
const nlohmann::json &document, std::string &value) const {
if(document.count(a_facet.field_name) == 0) {
+ // check for field exists
if(search_schema.at(a_facet.field_name).optional) {
return false;
}
diff --git a/test/collection_specific_test.cpp b/test/collection_specific_test.cpp
index 55de988f..8ae33b22 100644
--- a/test/collection_specific_test.cpp
+++ b/test/collection_specific_test.cpp
@@ -142,6 +142,17 @@ TEST_F(CollectionSpecificTest, ExplicitHighlightFieldsConfig) {
ASSERT_EQ(1, results["hits"].size());
ASSERT_EQ(0, results["hits"][0]["highlights"].size());
+ // highlight field that does not exist
+
+ results = coll1->search("brown fox pernell", {"title"}, "", {}, {}, {2}, 10,
+ 1, FREQUENCY, {false}, 1, spp::sparse_hash_set<std::string>(),
+ spp::sparse_hash_set<std::string>(), 10, "", 30, 4, "", 1, {}, {}, {}, 0,
+ "<mark>", "</mark>", {1}, 10000, true, false, true, "not-found").get();
+
+ ASSERT_EQ(1, results["found"].get<size_t>());
+ ASSERT_EQ(1, results["hits"].size());
+ ASSERT_EQ(0, results["hits"][0]["highlights"].size());
+
collectionManager.drop_collection("coll1");
}