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

Phrase search highlight.

Kishore Nallan9e00f3a73fd6
diff --git a/src/collection.cpp b/src/collection.cpp
index 4adbe23b..1d38de7f 100644
--- a/src/collection.cpp
+++ b/src/collection.cpp
@@ -1101,7 +1101,14 @@ Option<nlohmann::json> Collection::search(const std::string & raw_query, const s
for(size_t i = 0; i < fields_highlighted_vec.size(); i++) {
const std::string& field_name = fields_highlighted_vec[i];
- const std::vector<std::string>& q_tokens = field_query_tokens[fields_highlighted_indices[i]].q_include_tokens;
+ std::vector<std::string> q_tokens = field_query_tokens[fields_highlighted_indices[i]].q_include_tokens;
+
+ // we will also add phrase search tokens to the list
+ for(auto& phrase: field_query_tokens[fields_highlighted_indices[i]].q_phrases) {
+ for(auto& token: phrase) {
+ q_tokens.push_back(token);
+ }
+ }
if(search_schema.count(field_name) == 0) {
continue;
diff --git a/test/collection_specific_test.cpp b/test/collection_specific_test.cpp
index 528485ea..6f140825 100644
--- a/test/collection_specific_test.cpp
+++ b/test/collection_specific_test.cpp
@@ -2079,17 +2079,21 @@ TEST_F(CollectionSpecificTest, PhraseSearch) {
results = coll1->search(R"("down there by")", {"title"}, "", {}, {}, {0}, 10, 1, FREQUENCY, {false}, 10).get();
ASSERT_EQ(1, results["hits"].size());
ASSERT_EQ("1", results["hits"][0]["document"]["id"].get<std::string>());
+ ASSERT_EQ("<mark>Down</mark> <mark>There</mark> <mark>by</mark> the Train", results["hits"][0]["highlights"][0]["snippet"].get<std::string>());
// phrase search with exclusion
results = coll1->search(R"("by the" -train)", {"title"}, "", {}, {}, {0}, 10, 1, FREQUENCY, {false}, 10).get();
ASSERT_EQ(1, results["hits"].size());
ASSERT_EQ("0", results["hits"][0]["document"]["id"].get<std::string>());
+ ASSERT_EQ("<mark>Then</mark> and <mark>there</mark> <mark>by</mark> <mark>the</mark> down", results["hits"][0]["highlights"][0]["snippet"].get<std::string>());
// exclusion of an entire phrase
results = coll1->search(R"(-"by the down")", {"title"}, "", {}, {}, {0}, 10, 1, FREQUENCY, {false}, 10).get();
ASSERT_EQ(2, results["hits"].size());
ASSERT_EQ("2", results["hits"][0]["document"]["id"].get<std::string>());
ASSERT_EQ("1", results["hits"][1]["document"]["id"].get<std::string>());
+ ASSERT_EQ(0, results["hits"][0]["highlights"].size());
+ ASSERT_EQ(0, results["hits"][1]["highlights"].size());
results = coll1->search(R"(-"by the")", {"title"}, "", {}, {}, {0}, 10, 1, FREQUENCY, {false}, 10).get();
ASSERT_EQ(1, results["hits"].size());
@@ -2125,6 +2129,7 @@ TEST_F(CollectionSpecificTest, PhraseSearch) {
results = coll1->search(R"("by the" "then and")", {"title"}, "", {}, {}, {0}, 10, 1, FREQUENCY, {false}, 10).get();
ASSERT_EQ(1, results["hits"].size());
ASSERT_EQ("0", results["hits"][0]["document"]["id"].get<std::string>());
+ ASSERT_EQ("<mark>Then</mark> <mark>and</mark> there <mark>by</mark> <mark>the</mark> down", results["hits"][0]["highlights"][0]["snippet"].get<std::string>());
results = coll1->search(R"("by the" "there by")", {"title"}, "", {}, {}, {0}, 10, 1, FREQUENCY, {false}, 10).get();
ASSERT_EQ(2, results["hits"].size());