From 3eed0d509e487697b23e646c3d1cbdbf4129d4df Mon Sep 17 00:00:00 2001 From: Kishore Nallan Date: Sat, 30 Oct 2021 11:54:19 +0530 Subject: [PATCH] Allow highlighting to be skipped via non-indexed field. --- src/collection.cpp | 5 +++++ test/collection_specific_test.cpp | 11 +++++++++++ 2 files changed, 16 insertions(+) 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 Collection::search(const std::string & raw_query, const s const std::string& field_name = fields_highlighted_vec[i]; const std::vector& 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(), + spp::sparse_hash_set(), 10, "", 30, 4, "", 1, {}, {}, {}, 0, + "", "", {1}, 10000, true, false, true, "not-found").get(); + + ASSERT_EQ(1, results["found"].get()); + ASSERT_EQ(1, results["hits"].size()); + ASSERT_EQ(0, results["hits"][0]["highlights"].size()); + collectionManager.drop_collection("coll1"); }