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

Highlight infix matches.

Kishore Nallan4fe747810e66
diff --git a/include/collection.h b/include/collection.h
index 16332cb5..ad5e26f0 100644
--- a/include/collection.h
+++ b/include/collection.h
@@ -211,6 +211,7 @@ private:
const size_t snippet_threshold,
const size_t highlight_affix_num_tokens,
bool highlighted_fully,
+ bool is_infix_search,
const std::string& highlight_start_tag,
const std::string& highlight_end_tag,
highlight_t &highlight) const;
diff --git a/src/collection.cpp b/src/collection.cpp
index 61f4fa7c..8586ce8a 100644
--- a/src/collection.cpp
+++ b/src/collection.cpp
@@ -1204,7 +1204,8 @@ Option<nlohmann::json> Collection::search(const std::string & raw_query, const s
highlight_t highlight;
highlight_result(raw_query, search_field, searched_queries, q_tokens, field_order_kv, document,
string_utils, snippet_threshold, highlight_affix_num_tokens,
- highlight_item.fully_highlighted, highlight_start_tag, highlight_end_tag, highlight);
+ highlight_item.fully_highlighted, highlight_item.infix,
+ highlight_start_tag, highlight_end_tag, highlight);
if(!highlight.snippets.empty()) {
highlights.push_back(highlight);
}
@@ -1721,6 +1722,7 @@ void Collection::highlight_result(const std::string& raw_query, const field &sea
const size_t snippet_threshold,
const size_t highlight_affix_num_tokens,
bool highlighted_fully,
+ bool is_infix_search,
const std::string& highlight_start_tag,
const std::string& highlight_end_tag,
highlight_t & highlight) const {
@@ -1955,6 +1957,10 @@ void Collection::highlight_result(const std::string& raw_query, const field &sea
raw_token.rfind(last_raw_q_token, 0) == 0) {
token_offsets.emplace(tok_start, tok_end);
token_hits.insert(raw_token);
+ } else if(is_infix_search && text.size() < 100 &&
+ raw_token.find(raw_query_tokens.front()) != std::string::npos) {
+ token_offsets.emplace(tok_start, tok_end);
+ token_hits.insert(raw_token);
}
if(last_full_q_tokens.find(raw_token) != last_full_q_tokens.end()) {
diff --git a/test/collection_infix_search_test.cpp b/test/collection_infix_search_test.cpp
index 14e34110..0eb36e5c 100644
--- a/test/collection_infix_search_test.cpp
+++ b/test/collection_infix_search_test.cpp
@@ -58,6 +58,11 @@ TEST_F(CollectionInfixSearchTest, InfixBasics) {
ASSERT_EQ(1, results["hits"].size());
ASSERT_STREQ("0", results["hits"][0]["document"]["id"].get<std::string>().c_str());
+ ASSERT_EQ(1, results["hits"][0]["highlights"].size());
+ ASSERT_EQ("title", results["hits"][0]["highlights"][0]["field"].get<std::string>());
+ ASSERT_EQ("<mark>GH100037IN8900X</mark>", results["hits"][0]["highlights"][0]["snippet"].get<std::string>());
+ ASSERT_EQ("<mark>GH100037IN8900X</mark>", results["hits"][0]["highlights"][0]["value"].get<std::string>());
+
// verify off behavior
results = coll1->search("100037",
@@ -218,5 +223,28 @@ TEST_F(CollectionInfixSearchTest, InfixSpecificField) {
ASSERT_EQ(1, results["hits"].size());
ASSERT_STREQ("1", results["hits"][0]["document"]["id"].get<std::string>().c_str());
+ // highlight infix match only on infix-searched field
+ doc["id"] = "2";
+ doc["title"] = "fuzzbuzz HYU16736GY6372";
+ doc["description"] = "HYU16736GY6372";
+ doc["points"] = 100;
+ ASSERT_TRUE(coll1->add(doc.dump()).ok());
+
+ results = coll1->search("16736",
+ {"title", "description"}, "", {}, {}, {0}, 3, 1, FREQUENCY, {true}, 5,
+ spp::sparse_hash_set<std::string>(),
+ spp::sparse_hash_set<std::string>(), 10, "", 30, 4, "title", 20, {}, {}, {}, 0,
+ "<mark>", "</mark>", {}, 1000, true, false, true, "", false, 6000 * 1000, 4, 7, true,
+ 4, {off, always}).get();
+
+ ASSERT_EQ(1, results["found"].get<size_t>());
+ ASSERT_EQ(1, results["hits"].size());
+ ASSERT_STREQ("2", results["hits"][0]["document"]["id"].get<std::string>().c_str());
+
+ ASSERT_EQ(1, results["hits"][0]["highlights"].size());
+ ASSERT_EQ("description", results["hits"][0]["highlights"][0]["field"].get<std::string>());
+ ASSERT_EQ("<mark>HYU16736GY6372</mark>", results["hits"][0]["highlights"][0]["snippet"].get<std::string>());
+ ASSERT_FALSE(results["hits"][0]["highlights"][0].contains("value"));
+
collectionManager.drop_collection("coll1");
}