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

Group key should be actual values.

kishorenc0762f4ae296c
diff --git a/cmake/Glog.cmake b/cmake/Glog.cmake
deleted file mode 100644
index e69de29b..00000000
diff --git a/include/logger.h b/include/logger.h
index 01adc553..5eee4cdf 100644
--- a/include/logger.h
+++ b/include/logger.h
@@ -1,5 +1,3 @@
#pragma once
-#include <string>
-#include <iostream>
#include <glog/logging.h>
\ No newline at end of file
diff --git a/include/typesense_server_utils.h b/include/typesense_server_utils.h
index 4b0af691..34f0cff4 100644
--- a/include/typesense_server_utils.h
+++ b/include/typesense_server_utils.h
@@ -1,10 +1,10 @@
#pragma once
+#include "logger.h"
#include <string>
#include <iostream>
#include <cmdline.h>
#include "config.h"
-#include "logger.h"
#include "store.h"
#include "collection_manager.h"
#include <csignal>
diff --git a/src/collection.cpp b/src/collection.cpp
index fbe0a376..402892e5 100644
--- a/src/collection.cpp
+++ b/src/collection.cpp
@@ -862,7 +862,7 @@ Option<nlohmann::json> Collection::search(const std::string & query, const std::
result["found"] = total_found;
- std::string hits_key = (group_limit > 1) ? "grouped_hits" : "hits";
+ std::string hits_key = group_limit ? "grouped_hits" : "hits";
result[hits_key] = nlohmann::json::array();
// construct results array
@@ -870,17 +870,11 @@ Option<nlohmann::json> Collection::search(const std::string & query, const std::
const std::vector<KV*> & kv_group = result_group_kvs[result_kvs_index];
nlohmann::json group_hits;
- if(group_limit > 1) {
+ if(group_limit) {
group_hits["hits"] = nlohmann::json::array();
- std::vector<std::string> group_keys;
- for(const auto& group_key: group_by_fields) {
- group_keys.push_back(group_key);
- }
-
- group_hits["group_key"] = StringUtils::join(group_keys, ",");
}
- nlohmann::json& hits_array = (group_limit > 1) ? group_hits["hits"] : result["hits"];
+ nlohmann::json& hits_array = group_limit ? group_hits["hits"] : result["hits"];
for(const KV* field_order_kv: kv_group) {
const std::string& seq_id_key = get_seq_id_key((uint32_t) field_order_kv->key);
@@ -960,7 +954,16 @@ Option<nlohmann::json> Collection::search(const std::string & query, const std::
hits_array.push_back(wrapper_doc);
}
- if(group_limit > 1) {
+ if(group_limit) {
+ const auto& document = group_hits["hits"][0]["document"];
+
+ group_hits["group_key"] = nlohmann::json::array();
+ for(const auto& field_name: group_by_fields) {
+ if(document.count(field_name) != 0) {
+ group_hits["group_key"].push_back(document[field_name]);
+ }
+ }
+
result["grouped_hits"].push_back(group_hits);
}
}
diff --git a/src/main/typesense_server.cpp b/src/main/typesense_server.cpp
index 5a3ef7d5..fd9ed722 100644
--- a/src/main/typesense_server.cpp
+++ b/src/main/typesense_server.cpp
@@ -1,6 +1,6 @@
+#include "typesense_server_utils.h"
#include "core_api.h"
#include "config.h"
-#include "typesense_server_utils.h"
void master_server_routes() {
// collection management
diff --git a/test/collection_grouping_test.cpp b/test/collection_grouping_test.cpp
index b1fcd8b6..1c565bd5 100644
--- a/test/collection_grouping_test.cpp
+++ b/test/collection_grouping_test.cpp
@@ -70,9 +70,10 @@ TEST_F(CollectionGroupingTest, GroupingBasics) {
ASSERT_EQ(3, res["found"].get<size_t>());
ASSERT_EQ(3, res["grouped_hits"].size());
- ASSERT_STREQ("size", res["grouped_hits"][0]["group_key"].get<std::string>().c_str());
+ ASSERT_EQ(11, res["grouped_hits"][0]["group_key"][0].get<size_t>());
ASSERT_FLOAT_EQ(4.8, res["grouped_hits"][0]["hits"][0]["document"]["rating"].get<float>());
+ ASSERT_EQ(11, res["grouped_hits"][0]["hits"][0]["document"]["size"].get<size_t>());
ASSERT_STREQ("5", res["grouped_hits"][0]["hits"][0]["document"]["id"].get<std::string>().c_str());
ASSERT_FLOAT_EQ(4.3, res["grouped_hits"][0]["hits"][1]["document"]["rating"].get<float>());
ASSERT_STREQ("1", res["grouped_hits"][0]["hits"][1]["document"]["id"].get<std::string>().c_str());
@@ -112,7 +113,7 @@ TEST_F(CollectionGroupingTest, GroupingBasics) {
// 7 unique ratings
ASSERT_EQ(7, res["found"].get<size_t>());
ASSERT_EQ(7, res["grouped_hits"].size());
- ASSERT_STREQ("rating", res["grouped_hits"][0]["group_key"].get<std::string>().c_str());
+ ASSERT_FLOAT_EQ(4.4, res["grouped_hits"][0]["group_key"][0].get<float>());
ASSERT_EQ(12, res["grouped_hits"][0]["hits"][0]["document"]["size"].get<uint32_t>());
ASSERT_STREQ("8", res["grouped_hits"][0]["hits"][0]["document"]["id"].get<std::string>().c_str());
@@ -151,7 +152,14 @@ TEST_F(CollectionGroupingTest, GroupingCompoundKey) {
ASSERT_EQ(10, res["found"].get<size_t>());
ASSERT_EQ(10, res["grouped_hits"].size());
- ASSERT_STREQ("size,brand", res["grouped_hits"][0]["group_key"].get<std::string>().c_str());
+ ASSERT_EQ(11, res["grouped_hits"][0]["group_key"][0].get<size_t>());
+
+ ASSERT_STREQ("Beta", res["grouped_hits"][0]["group_key"][1].get<std::string>().c_str());
+
+ // optional field should have no value in the group key component
+ ASSERT_EQ(1, res["grouped_hits"][5]["group_key"].size());
+ ASSERT_STREQ("10", res["grouped_hits"][5]["hits"][0]["document"]["id"].get<std::string>().c_str());
+ ASSERT_STREQ("11", res["grouped_hits"][5]["hits"][1]["document"]["id"].get<std::string>().c_str());
ASSERT_EQ(1, res["grouped_hits"][0]["hits"].size());
ASSERT_FLOAT_EQ(4.8, res["grouped_hits"][0]["hits"][0]["document"]["rating"].get<float>());
@@ -199,7 +207,8 @@ TEST_F(CollectionGroupingTest, GroupingCompoundKey) {
// total count and facet counts should be the same
ASSERT_EQ(10, res["found"].get<size_t>());
ASSERT_EQ(2, res["grouped_hits"].size());
- ASSERT_STREQ("size,brand", res["grouped_hits"][0]["group_key"].get<std::string>().c_str());
+ ASSERT_EQ(10, res["grouped_hits"][0]["group_key"][0].get<size_t>());
+ ASSERT_STREQ("Omega", res["grouped_hits"][0]["group_key"][1].get<std::string>().c_str());
ASSERT_STREQ("brand", res["facet_counts"][0]["field_name"].get<std::string>().c_str());
ASSERT_EQ(3, (int) res["facet_counts"][0]["counts"][0]["count"]);
@@ -215,7 +224,7 @@ TEST_F(CollectionGroupingTest, GroupingCompoundKey) {
ASSERT_STREQ("Zeta", res["facet_counts"][0]["counts"][3]["value"].get<std::string>().c_str());
}
-TEST_F(CollectionGroupingTest, GroupingWithSingleDistinct) {
+TEST_F(CollectionGroupingTest, GroupingWithGropLimitOfOne) {
auto res = coll_group->search("*", {}, "", {"brand"}, {}, 0, 50, 1, FREQUENCY,
false, Index::DROP_TOKENS_THRESHOLD,
spp::sparse_hash_set<std::string>(),
@@ -224,13 +233,18 @@ TEST_F(CollectionGroupingTest, GroupingWithSingleDistinct) {
{}, {}, {"brand"}, 1).get();
ASSERT_EQ(5, res["found"].get<size_t>());
- ASSERT_EQ(5, res["hits"].size());
+ ASSERT_EQ(5, res["grouped_hits"].size());
- ASSERT_STREQ("4", res["hits"][0]["document"]["id"].get<std::string>().c_str());
- ASSERT_STREQ("2", res["hits"][1]["document"]["id"].get<std::string>().c_str());
- ASSERT_STREQ("8", res["hits"][2]["document"]["id"].get<std::string>().c_str());
- ASSERT_STREQ("10", res["hits"][3]["document"]["id"].get<std::string>().c_str()); // unbranded
- ASSERT_STREQ("9", res["hits"][4]["document"]["id"].get<std::string>().c_str());
+ // all hits array must be of size 1
+ for(auto i=0; i<5; i++) {
+ ASSERT_EQ(1, res["grouped_hits"][i]["hits"].size());
+ }
+
+ ASSERT_STREQ("4", res["grouped_hits"][0]["hits"][0]["document"]["id"].get<std::string>().c_str());
+ ASSERT_STREQ("2", res["grouped_hits"][1]["hits"][0]["document"]["id"].get<std::string>().c_str());
+ ASSERT_STREQ("8", res["grouped_hits"][2]["hits"][0]["document"]["id"].get<std::string>().c_str());
+ ASSERT_STREQ("10", res["grouped_hits"][3]["hits"][0]["document"]["id"].get<std::string>().c_str()); // unbranded
+ ASSERT_STREQ("9", res["grouped_hits"][4]["hits"][0]["document"]["id"].get<std::string>().c_str());
// facet counts should each be 1, including unbranded
ASSERT_STREQ("brand", res["facet_counts"][0]["field_name"].get<std::string>().c_str());
@@ -290,7 +304,9 @@ TEST_F(CollectionGroupingTest, GroupingWithArrayFieldAndOverride) {
ASSERT_EQ(4, res["found"].get<size_t>());
ASSERT_EQ(4, res["grouped_hits"].size());
- ASSERT_STREQ("colors", res["grouped_hits"][0]["group_key"].get<std::string>().c_str());
+
+ ASSERT_EQ(1, res["grouped_hits"][0]["group_key"][0].size());
+ ASSERT_STREQ("white", res["grouped_hits"][0]["group_key"][0][0].get<std::string>().c_str());
ASSERT_STREQ("11", res["grouped_hits"][0]["hits"][0]["document"]["id"].get<std::string>().c_str());
ASSERT_STREQ("10", res["grouped_hits"][0]["hits"][1]["document"]["id"].get<std::string>().c_str());
diff --git a/test/collection_override_test.cpp b/test/collection_override_test.cpp
index a36fb85a..e79e2b42 100644
--- a/test/collection_override_test.cpp
+++ b/test/collection_override_test.cpp
@@ -378,7 +378,11 @@ TEST_F(CollectionOverrideTest, PinnedHitsGrouping) {
ASSERT_EQ(8, results["found"].get<size_t>());
- ASSERT_STREQ("cast", results["grouped_hits"][0]["group_key"].get<std::string>().c_str());
+ ASSERT_EQ(1, results["grouped_hits"][0]["group_key"].size());
+ ASSERT_EQ(2, results["grouped_hits"][0]["group_key"][0].size());
+ ASSERT_STREQ("Chris Evans", results["grouped_hits"][0]["group_key"][0][0].get<std::string>().c_str());
+ ASSERT_STREQ("Scarlett Johansson", results["grouped_hits"][0]["group_key"][0][1].get<std::string>().c_str());
+
ASSERT_STREQ("6", results["grouped_hits"][0]["hits"][0]["document"]["id"].get<std::string>().c_str());
ASSERT_STREQ("8", results["grouped_hits"][0]["hits"][1]["document"]["id"].get<std::string>().c_str());