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

adding test for group sorting without group_by fields

krunal1313ff7f9060814e
diff --git a/src/collection.cpp b/src/collection.cpp
index 6afab144..1778e70e 100644
--- a/src/collection.cpp
+++ b/src/collection.cpp
@@ -819,19 +819,18 @@ Option<bool> Collection::validate_and_standardize_sort_fields(const std::vector<
}
if (sort_field_std.name != sort_field_const::text_match && sort_field_std.name != sort_field_const::eval &&
- sort_field_std.name != sort_field_const::seq_id) {
- if(!is_group_by_query) {
- const auto field_it = search_schema.find(sort_field_std.name);
- if(field_it == search_schema.end() || !field_it.value().sort || !field_it.value().index) {
- std::string error = "Could not find a field named `" + sort_field_std.name +
- "` in the schema for sorting.";
- return Option<bool>(404, error);
- }
+ sort_field_std.name != sort_field_const::seq_id && sort_field_std.name != sort_field_const::group_count) {
+
+ const auto field_it = search_schema.find(sort_field_std.name);
+ if(field_it == search_schema.end() || !field_it.value().sort || !field_it.value().index) {
+ std::string error = "Could not find a field named `" + sort_field_std.name +
+ "` in the schema for sorting.";
+ return Option<bool>(404, error);
}
}
if(sort_field_std.name == sort_field_const::group_count && is_group_by_query == false) {
- std::string error = " group_by parameters should not be empty when using sort_by group_count";
+ std::string error = "group_by parameters should not be empty when using sort_by group_count";
return Option<bool>(404, error);
}
diff --git a/test/collection_grouping_test.cpp b/test/collection_grouping_test.cpp
index 85039f9f..bc1a7b54 100644
--- a/test/collection_grouping_test.cpp
+++ b/test/collection_grouping_test.cpp
@@ -785,4 +785,19 @@ TEST_F(CollectionGroupingTest, SortingMoreThanMaxTopsterSize) {
ASSERT_EQ(4, res2["grouped_hits"][50]["found"].get<int32_t>());
ASSERT_EQ(4, res2["grouped_hits"][99]["found"].get<int32_t>());
+}
+
+TEST_F(CollectionGroupingTest, GroupSortingWithoutGroupingFields) {
+
+ std::vector<sort_by> sort_fields = {sort_by("_group_count", "DESC")};
+
+ auto res = coll_group->search("*", {}, "", {"brand"}, sort_fields, {0}, 50, 1, FREQUENCY,
+ {false}, Index::DROP_TOKENS_THRESHOLD,
+ spp::sparse_hash_set<std::string>(),
+ spp::sparse_hash_set<std::string>(), 10, "", 30, 5,
+ "", 10,
+ {}, {}, {});
+
+ ASSERT_EQ(res.ok(), false);
+ ASSERT_EQ(res.error(), "group_by parameters should not be empty when using sort_by group_count");
}
\ No newline at end of file