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
Fix highlight glitch involving prefix tokens.
Kishore Nallan1dd791b0b7db
diff --git a/include/art.h b/include/art.hindex 7455c413..0235bf82 100644--- a/include/art.h+++ b/include/art.h@@ -106,11 +106,12 @@ typedef struct {struct token_leaf {art_leaf* leaf;+ bool is_prefix;uint32_t root_len;uint32_t num_typos;- token_leaf(art_leaf* leaf, uint32_t root_len, uint32_t num_typos) : leaf(leaf), root_len(root_len),- num_typos(num_typos) {+ token_leaf(art_leaf* leaf, uint32_t root_len, uint32_t num_typos, bool is_prefix) :+ leaf(leaf), root_len(root_len), num_typos(num_typos), is_prefix(is_prefix) {}};diff --git a/src/collection.cpp b/src/collection.cppindex eb3fcd47..c9e63491 100644--- a/src/collection.cpp+++ b/src/collection.cpp@@ -1534,7 +1534,9 @@ void Collection::process_highlight_fields(const std::vector<std::string>& searchconst auto& field_name = highlight_item.name;art_leaf* leaf = index->get_token_leaf(field_name, (const unsigned char*) qtoken.c_str(), qtoken.size()+1);if(leaf) {- highlight_item.qtoken_leaves.insert(qtoken, token_leaf(leaf, it.value().root_len, it.value().num_typos));+ highlight_item.qtoken_leaves.insert(qtoken,+ token_leaf(leaf, it.value().root_len, it.value().num_typos, it.value().is_prefix)+ );}}}@@ -1547,7 +1549,7 @@ void Collection::process_highlight_fields(const std::vector<std::string>& searchconst auto& field_name = highlight_item.name;art_leaf* leaf = index->get_token_leaf(field_name, (const unsigned char*) q_token.c_str(), q_token.size()+1);if(leaf) {- highlight_item.qtoken_leaves.insert(q_token, token_leaf(leaf, q_token.size(), 0));+ highlight_item.qtoken_leaves.insert(q_token, token_leaf(leaf, q_token.size(), 0, false));}}}@@ -1954,7 +1956,8 @@ void Collection::highlight_result(const std::string& raw_query, const field &seamatch.offsets[match_offset_index].offset == raw_token_index)) {// check if the matched token is a prefix of this found token- if(qtoken_it != qtoken_leaves.end() && qtoken_it.value().root_len < raw_token.size()) {+ if(qtoken_it != qtoken_leaves.end() && qtoken_it.value().is_prefix &&+ qtoken_it.value().root_len < raw_token.size()) {// need to ensure that only the prefix portion is highlighted// if length diff is within 2, we still might not want to highlight partially in some cases// e.g. "samsng" vs "samsung" -> full highlight is preferred, unless it's a full prefix match@@ -1988,7 +1991,8 @@ void Collection::highlight_result(const std::string& raw_query, const field &sea// If field is marked to be highlighted fully, or field length exceeds snippet_threshold, we will// locate all tokens that appear in the query / query candidates- if(qtoken_it != qtoken_leaves.end() && qtoken_it.value().root_len < raw_token.size()) {+ if(qtoken_it != qtoken_leaves.end() && qtoken_it.value().is_prefix &&+ qtoken_it.value().root_len < raw_token.size()) {// need to ensure that only the prefix portion is highlightedsize_t char_diff = (tok_end - tok_start + 1) - last_raw_q_token.size();auto new_tok_end = (char_diff <= 2 && qtoken_it.value().num_typos != 0) ?diff --git a/src/index.cpp b/src/index.cppindex 448f7556..88e95e78 100644--- a/src/index.cpp+++ b/src/index.cpp@@ -1172,12 +1172,12 @@ void Index::search_all_candidates(const size_t num_search_fields,//LOG(INFO) << "field_num_results: " << field_num_results << ", typo_tokens_threshold: " << typo_tokens_threshold;//LOG(INFO) << "n: " << n;- std::stringstream fullq;+ /*std::stringstream fullq;for(const auto& qtok : query_suggestion) {fullq << qtok.value << " ";}- /*LOG(INFO) << "query: " << fullq.str() << ", total_cost: " << total_cost;*/+ LOG(INFO) << "query: " << fullq.str() << ", total_cost: " << total_cost;*/search_across_fields(query_suggestion, num_typos, prefixes, the_fields, num_search_fields,sort_fields, topster,groups_processed,@@ -2876,7 +2876,7 @@ void Index::search_across_fields(const std::vector<token_t>& query_tokens,if(!result_ids.empty()) {searched_queries.push_back(query_suggestion);for(const auto& qtoken: query_tokens) {- qtoken_set.insert(qtoken.value, token_leaf(nullptr, qtoken.root_len, qtoken.num_typos));+ qtoken_set.insert(qtoken.value, token_leaf(nullptr, qtoken.root_len, qtoken.num_typos, qtoken.is_prefix_searched));}}diff --git a/test/collection_specific_test.cpp b/test/collection_specific_test.cppindex c62f6b10..8caa9d7c 100644--- a/test/collection_specific_test.cpp+++ b/test/collection_specific_test.cpp@@ -2781,3 +2781,33 @@ TEST_F(CollectionSpecificTest, NonIndexField) {collectionManager.drop_collection("coll1");}+TEST_F(CollectionSpecificTest, HighlightPrefixProperly) {+ std::vector<field> fields = {field("title", field_types::STRING, false),};++ Collection* coll1 = collectionManager.create_collection("coll1", 1, fields).get();++ std::vector<std::vector<std::string>> records = {+ {"Cinderella: the story."},+ {"The story of a girl."},+ };++ for(size_t i=0; i<records.size(); i++) {+ nlohmann::json doc;++ doc["id"] = std::to_string(i);+ doc["title"] = records[i][0];+ doc["points"] = i;++ ASSERT_TRUE(coll1->add(doc.dump()).ok());+ }++ auto results = coll1->search("cindrella o", {"title"},+ "", {}, {}, {2}, 10, 1, FREQUENCY, {true}, 1).get();++ ASSERT_EQ(1, results["hits"].size());+ ASSERT_EQ("0", results["hits"][0]["document"]["id"].get<std::string>());+ ASSERT_EQ("<mark>Cinderella:</mark> the story.", results["hits"][0]["highlights"][0]["snippet"].get<std::string>());++ collectionManager.drop_collection("coll1");+}+