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

respect num of groups when typo_tokens_threshold is applied (#1574)

Krunal Gandhi7762f2d0802a
diff --git a/src/index.cpp b/src/index.cpp
index 75a38ffd..e20e422e 100644
--- a/src/index.cpp
+++ b/src/index.cpp
@@ -4151,7 +4151,8 @@ Option<bool> Index::fuzzy_search_fields(const std::vector<search_field_t>& the_f
resume_typo_loop:
- if(!exhaustive_search && all_result_ids_len >= typo_tokens_threshold) {
+ auto results_count = group_limit != 0 ? groups_processed.size() : all_result_ids_len;
+ if(!exhaustive_search && results_count >= typo_tokens_threshold) {
// if typo threshold is breached, we are done
return Option<bool>(true);
}
diff --git a/test/collection_grouping_test.cpp b/test/collection_grouping_test.cpp
index d518f89b..693ebda8 100644
--- a/test/collection_grouping_test.cpp
+++ b/test/collection_grouping_test.cpp
@@ -159,6 +159,32 @@ TEST_F(CollectionGroupingTest, GroupingBasics) {
"", 10,
{}, {}, {"foo*"}, 2).error();
ASSERT_EQ("Pattern `foo*` is not allowed.", error);
+
+ // typo_tokens_threshold should respect num_groups
+ res = coll_group->search("beta", {"brand"}, "", {"brand"}, {}, {2}, 50, 1, FREQUENCY,
+ {false}, Index::DROP_TOKENS_THRESHOLD,
+ spp::sparse_hash_set<std::string>(),
+ spp::sparse_hash_set<std::string>(), 10, "", 30, 5,
+ "", 2,
+ {}, {}, {"brand"}, 1).get();
+
+ ASSERT_EQ(4, res["found_docs"].get<size_t>());
+ ASSERT_EQ(2, res["found"].get<size_t>());
+ ASSERT_EQ(2, res["grouped_hits"].size());
+ ASSERT_EQ("Beta", res["grouped_hits"][0]["group_key"][0]);
+ ASSERT_EQ("Zeta", res["grouped_hits"][1]["group_key"][0]);
+
+ res = coll_group->search("beta", {"brand"}, "", {"brand"}, {}, {2}, 50, 1, FREQUENCY,
+ {false}, Index::DROP_TOKENS_THRESHOLD,
+ spp::sparse_hash_set<std::string>(),
+ spp::sparse_hash_set<std::string>(), 10, "", 30, 5,
+ "", 1,
+ {}, {}, {"brand"}, 1).get();
+
+ ASSERT_EQ(3, res["found_docs"].get<size_t>());
+ ASSERT_EQ(1, res["found"].get<size_t>());
+ ASSERT_EQ(1, res["grouped_hits"].size());
+ ASSERT_EQ("Beta", res["grouped_hits"][0]["group_key"][0]);
}
TEST_F(CollectionGroupingTest, GroupingCompoundKey) {