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

Handle facet query being larger than matched token.

kishorenc5b55b071b232
diff --git a/src/collection.cpp b/src/collection.cpp
index bb62c24f..32024266 100644
--- a/src/collection.cpp
+++ b/src/collection.cpp
@@ -1065,9 +1065,11 @@ Option<nlohmann::json> Collection::search(const std::string & query, const std::
}
if(token_query_pos.count(i) != 0) {
- size_t highlight_len = facet_query_tokens[token_query_pos[i]].size();
- const std::string & unmarked = tokens[i].substr(highlight_len, std::string::npos);
- highlightedss << "<mark>" + tokens[i].substr(0, highlight_len) + "</mark>" + unmarked;
+ size_t query_token_len = facet_query_tokens[token_query_pos[i]].size();
+ // handle query token being larger than actual token (typo correction)
+ query_token_len = std::min(query_token_len, tokens[i].size());
+ const std::string & unmarked = tokens[i].substr(query_token_len, std::string::npos);
+ highlightedss << "<mark>" + tokens[i].substr(0, query_token_len) + "</mark>" + unmarked;
} else {
highlightedss << tokens[i];
}
diff --git a/test/collection_faceting_test.cpp b/test/collection_faceting_test.cpp
index 49752473..82db30a4 100644
--- a/test/collection_faceting_test.cpp
+++ b/test/collection_faceting_test.cpp
@@ -105,7 +105,7 @@ TEST_F(CollectionFacetingTest, FacetCounts) {
ASSERT_STREQ("gold", results["facet_counts"][0]["counts"][1]["value"].get<std::string>().c_str());
ASSERT_EQ(3, (int) results["facet_counts"][0]["counts"][1]["count"]);
- // 2 facets, 1 text filter with no filters
+ // 2 facets, 1 text query with no filters
facets.clear();
facets.push_back("tags");
facets.push_back("name_facet");
@@ -492,6 +492,18 @@ TEST_F(CollectionFacetingTest, FacetCountsHighlighting) {
ASSERT_STREQ("Cell Phone Accessories", results["facet_counts"][0]["counts"][0]["value"].get<std::string>().c_str());
ASSERT_STREQ("Cell Phones", results["facet_counts"][0]["counts"][1]["value"].get<std::string>().c_str());
+ // facet query longer than a token is correctly matched with typo tolerance
+ results = coll1->search("phone", {"categories"}, "", facets, sort_fields, 0, 10, 1,
+ token_ordering::FREQUENCY, true, 10, spp::sparse_hash_set<std::string>(),
+ spp::sparse_hash_set<std::string>(), 10, "categories:cellx").get();
+
+ ASSERT_EQ(1, results["facet_counts"].size());
+ ASSERT_EQ(3, results["facet_counts"][0]["counts"].size());
+
+ ASSERT_STREQ("<mark>Cell</mark> Phone Accessories", results["facet_counts"][0]["counts"][0]["highlighted"].get<std::string>().c_str());
+ ASSERT_STREQ("<mark>Cell</mark> Phones", results["facet_counts"][0]["counts"][1]["highlighted"].get<std::string>().c_str());
+ ASSERT_STREQ("<mark>Cello</mark>phanes", results["facet_counts"][0]["counts"][2]["highlighted"].get<std::string>().c_str());
+
collectionManager.drop_collection("coll1");
}
diff --git a/test/match_score_test.cpp b/test/match_score_test.cpp
index 06e74ec9..3ca48140 100644
--- a/test/match_score_test.cpp
+++ b/test/match_score_test.cpp
@@ -1,3 +1,4 @@
+#include <chrono>
#include <gtest/gtest.h>
#include <match_score.h>