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

Hits and found fields should always be returned in response.

kishorenc74aa2e8a840c
diff --git a/src/collection.cpp b/src/collection.cpp
index 5f1fca1b..7597035e 100644
--- a/src/collection.cpp
+++ b/src/collection.cpp
@@ -589,6 +589,9 @@ Option<nlohmann::json> Collection::search(std::string query, const std::vector<s
nlohmann::json result = nlohmann::json::object();
+ result["hits"] = nlohmann::json::array();
+ result["found"] = total_found;
+
const int start_result_index = (page - 1) * per_page;
const int kvsize = raw_result_kvs.size() + override_result_kvs.size();
@@ -625,9 +628,6 @@ Option<nlohmann::json> Collection::search(std::string query, const std::vector<s
size_t end_result_index = std::min(num_results, result_kvs.size()) - 1;
- result["hits"] = nlohmann::json::array();
- result["found"] = total_found;
-
// construct results array
for(size_t result_kvs_index = start_result_index; result_kvs_index <= end_result_index; result_kvs_index++) {
const auto & field_order_kv = result_kvs[result_kvs_index];
diff --git a/test/collection_test.cpp b/test/collection_test.cpp
index 2d747f6f..3a8fe0ae 100644
--- a/test/collection_test.cpp
+++ b/test/collection_test.cpp
@@ -112,6 +112,11 @@ TEST_F(CollectionTest, ExactSearchShouldBeStable) {
std::string result_id = result["document"]["id"];
ASSERT_STREQ(id.c_str(), result_id.c_str());
}
+
+ // when a query does not return results, hits and found fields should still exist in response
+ results = collection->search("zxsadqewsad", query_fields, "", facets, sort_fields_asc, 0, 10).get();
+ ASSERT_EQ(0, results["hits"].size());
+ ASSERT_EQ(0, results["found"].get<int>());
}
TEST_F(CollectionTest, ExactPhraseSearch) {