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

Fix single character full field value highlight.

Kishore Nallane42f78a69529
diff --git a/src/collection.cpp b/src/collection.cpp
index 6a1193eb..cb4ee5d9 100644
--- a/src/collection.cpp
+++ b/src/collection.cpp
@@ -1704,7 +1704,9 @@ void Collection::highlight_result(const field &search_field,
if(offset_it != token_offsets.end()) {
if (i == offset_it->first) {
value_stream << highlight_start_tag;
- } else if (i == offset_it->second) {
+ }
+
+ if (i == offset_it->second) {
value_stream << text[i];
value_stream << highlight_end_tag;
offset_it++;
diff --git a/src/index.cpp b/src/index.cpp
index ea8d9ca4..d1350620 100644
--- a/src/index.cpp
+++ b/src/index.cpp
@@ -910,8 +910,6 @@ void Index::search_candidates(const uint8_t & field_id,
auto product = []( long long a, token_candidates & b ) { return a*b.candidates.size(); };
long long int N = std::accumulate(token_candidates_vec.begin(), token_candidates_vec.end(), 1LL, product);
- size_t last_cost = 0;
-
for(long long n=0; n<N && n<combination_limit; ++n) {
// every element in `query_suggestion` contains a token and its associated hits
std::vector<art_leaf*> query_suggestion(token_candidates_vec.size());
@@ -924,12 +922,6 @@ void Index::search_candidates(const uint8_t & field_id,
query_suggestion, token_bits);
//LOG(INFO) << "field_num_results: " << field_num_results << ", typo_tokens_threshold: " << typo_tokens_threshold;
- if(total_cost != last_cost && field_num_results >= typo_tokens_threshold) {
- //break;
- }
-
- last_cost = total_cost;
-
/*LOG(INFO) << "n: " << n;
for(size_t i=0; i < actual_query_suggestion.size(); i++) {
LOG(INFO) << "i: " << i << " - " << actual_query_suggestion[i]->key << ", ids: "
diff --git a/test/collection_specific_test.cpp b/test/collection_specific_test.cpp
index 19170cb0..85f4fafc 100644
--- a/test/collection_specific_test.cpp
+++ b/test/collection_specific_test.cpp
@@ -525,3 +525,34 @@ TEST_F(CollectionSpecificTest, DeleteOverridesAndSynonymsOnDiskDuringCollDrop) {
store->scan_fill(Collection::COLLECTION_SYNONYM_PREFIX, stored_values);
ASSERT_TRUE(stored_values.empty());
}
+
+TEST_F(CollectionSpecificTest, SingleCharMatchFullFieldHighlight) {
+ std::vector<field> fields = {field("title", field_types::STRING, false),
+ field("points", field_types::INT32, false),};
+
+ Collection* coll1 = collectionManager.create_collection("coll1", 1, fields, "points").get();
+
+ nlohmann::json doc1;
+ doc1["id"] = "0";
+ doc1["title"] = "Which of the following is a probable sign of infection?";
+ doc1["points"] = 100;
+
+ ASSERT_TRUE(coll1->add(doc1.dump()).ok());
+
+ auto results = coll1->search("a 3-month", {"title"}, "", {}, {}, {2}, 10,
+ 1, FREQUENCY, {false}, 1,
+ spp::sparse_hash_set<std::string>(),
+ spp::sparse_hash_set<std::string>(), 10, "", 30, 5,
+ "title", 1).get();
+
+ ASSERT_EQ(1, results["hits"].size());
+ ASSERT_EQ("0", results["hits"][0]["document"]["id"].get<std::string>());
+
+ ASSERT_EQ("Which of the following is <mark>a</mark> probable sign of infection?",
+ results["hits"][0]["highlights"][0]["snippet"].get<std::string>());
+
+ ASSERT_EQ("Which of the following is <mark>a</mark> probable sign of infection?",
+ results["hits"][0]["highlights"][0]["value"].get<std::string>());
+
+ collectionManager.drop_collection("coll1");
+}