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 query tokens being empty during highlighting.

Kishore Nallan94500c51a099
diff --git a/src/collection.cpp b/src/collection.cpp
index e640d4f1..95154220 100644
--- a/src/collection.cpp
+++ b/src/collection.cpp
@@ -2268,6 +2268,10 @@ void Collection::highlight_result(const std::string& raw_query, const field &sea
std::vector<std::string> raw_query_tokens;
Tokenizer(raw_query, normalise, false, search_field.locale, symbols_to_index, token_separators).tokenize(raw_query_tokens);
+ if(raw_query_tokens.empty()) {
+ return ;
+ }
+
const std::string& last_raw_q_token = raw_query_tokens.back();
size_t prefix_token_num_chars = StringUtils::get_num_chars(last_raw_q_token);
@@ -2753,7 +2757,12 @@ void Collection::highlight_text(const string& highlight_start_tag, const string&
const std::string& text_token = text.substr(snippet_start_offset, token_len);
matched_tokens.push_back(text_token);
- for(size_t j = 0; j < token_len && snippet_start_offset + j < text.size(); j++) {
+ for(size_t j = 0; j < token_len; j++) {
+ if((snippet_start_offset + j) >= text.size()) {
+ LOG(ERROR) << "??? snippet_start_offset: " << snippet_start_offset
+ << ", j: " << j << ", token_len: " << token_len << ", text: " << text;
+ break;
+ }
highlighted_text << text[snippet_start_offset + j];
}
diff --git a/test/collection_specific_more_test.cpp b/test/collection_specific_more_test.cpp
index 353b3760..07381fbc 100644
--- a/test/collection_specific_more_test.cpp
+++ b/test/collection_specific_more_test.cpp
@@ -1162,6 +1162,29 @@ TEST_F(CollectionSpecificMoreTest, IncludeFieldsOnlyId) {
ASSERT_EQ("0", res["hits"][0]["document"]["id"].get<std::string>());
}
+TEST_F(CollectionSpecificMoreTest, QueryWithOnlySpecialChars) {
+ nlohmann::json schema = R"({
+ "name": "coll1",
+ "fields": [
+ {"name": "title", "type": "string"}
+ ]
+ })"_json;
+
+ Collection* coll1 = collectionManager.create_collection(schema).get();
+
+ nlohmann::json doc;
+ doc["title"] = "Sample Title";
+ ASSERT_TRUE(coll1->add(doc.dump()).ok());
+
+ auto res_op = coll1->search("--", {"title"}, "", {}, {}, {2}, 10, 1, FREQUENCY, {true});
+
+ ASSERT_TRUE(res_op.ok());
+ auto res = res_op.get();
+
+ ASSERT_EQ(1, res["hits"].size());
+ ASSERT_EQ("0", res["hits"][0]["document"]["id"].get<std::string>());
+}
+
TEST_F(CollectionSpecificMoreTest, HighlightObjectShouldBeEmptyWhenNoHighlightFieldFound) {
nlohmann::json schema = R"({
"name": "coll1",