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

Simplify the structure of the "highlight" field in response.

Kishore Nallan46983ba6b484
diff --git a/src/collection.cpp b/src/collection.cpp
index 855860be..fde6be63 100644
--- a/src/collection.cpp
+++ b/src/collection.cpp
@@ -619,11 +619,11 @@ Option<nlohmann::json> Collection::search(std::string query, const std::vector<s
}
wrapper_doc["highlight"] = nlohmann::json::object();
- wrapper_doc["highlight"][field_name] = nlohmann::json::object();
- wrapper_doc["highlight"][field_name]["value"] = snippet_stream.str();
+ wrapper_doc["highlight"]["field"] = field_name;
+ wrapper_doc["highlight"]["snippet"] = snippet_stream.str();
if(search_field.type == field_types::STRING_ARRAY) {
- wrapper_doc["highlight"][field_name]["index"] = matched_array_index;
+ wrapper_doc["highlight"]["index"] = matched_array_index;
}
for (auto it = leaf_to_indices.begin(); it != leaf_to_indices.end(); it++) {
diff --git a/test/collection_test.cpp b/test/collection_test.cpp
index 86d6475b..e25b2b58 100644
--- a/test/collection_test.cpp
+++ b/test/collection_test.cpp
@@ -138,6 +138,11 @@ TEST_F(CollectionTest, ExactPhraseSearch) {
ASSERT_STREQ(id.c_str(), result_id.c_str());
}
+ ASSERT_EQ(results["hits"][0]["highlight"].size(), (unsigned long) 2);
+ ASSERT_STREQ(results["hits"][0]["highlight"]["field"].get<std::string>().c_str(), "title");
+ ASSERT_STREQ(results["hits"][0]["highlight"]["snippet"].get<std::string>().c_str(),
+ "What is the power requirement of a <mark>rocket</mark> <mark>launch</mark> these days?");
+
// Check ASC sort order
std::vector<sort_by> sort_fields_asc = { sort_by("points", "ASC") };
results = collection->search("rocket launch", query_fields, "", facets, sort_fields_asc, 0, 10).get();
@@ -534,9 +539,11 @@ TEST_F(CollectionTest, ArrayStringFieldHighlight) {
ASSERT_STREQ(id.c_str(), result_id.c_str());
}
- ASSERT_STREQ(results["hits"][0]["highlight"]["tags"]["value"].get<std::string>().c_str(),
+ ASSERT_EQ(results["hits"][0]["highlight"].size(), 3);
+ ASSERT_STREQ(results["hits"][0]["highlight"]["field"].get<std::string>().c_str(), "tags");
+ ASSERT_STREQ(results["hits"][0]["highlight"]["snippet"].get<std::string>().c_str(),
"<mark>truth</mark> <mark>about</mark>");
- ASSERT_EQ(results["hits"][0]["highlight"]["tags"]["index"].get<size_t>(), 2);
+ ASSERT_EQ(results["hits"][0]["highlight"]["index"].get<size_t>(), 2);
results = coll_array_text->search("forever truth", query_fields, "", facets, sort_fields, 0, 10, 1, FREQUENCY,
false, 0).get();
@@ -551,9 +558,9 @@ TEST_F(CollectionTest, ArrayStringFieldHighlight) {
ASSERT_STREQ(id.c_str(), result_id.c_str());
}
- ASSERT_STREQ(results["hits"][0]["highlight"]["tags"]["value"].get<std::string>().c_str(),
- "the <mark>truth</mark>");
- ASSERT_EQ(results["hits"][0]["highlight"]["tags"]["index"].get<size_t>(), 0);
+ ASSERT_STREQ(results["hits"][0]["highlight"]["field"].get<std::string>().c_str(), "tags");
+ ASSERT_STREQ(results["hits"][0]["highlight"]["snippet"].get<std::string>().c_str(), "the <mark>truth</mark>");
+ ASSERT_EQ(results["hits"][0]["highlight"]["index"].get<size_t>(), 0);
results = coll_array_text->search("truth", query_fields, "", facets, sort_fields, 0, 10, 1, FREQUENCY,
false, 0).get();