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

Treat zero facet sample percent as not sampled.

Kishore Nallancc980c2ecd56
diff --git a/src/index.cpp b/src/index.cpp
index 3acfa7ed..4ccb37f4 100644
--- a/src/index.cpp
+++ b/src/index.cpp
@@ -1368,6 +1368,10 @@ void Index::do_facets(std::vector<facet> & facets, facet_query_t & facet_query,
auto sort_index_it = sort_index.find(a_facet.field_name);
auto facet_sort_index_it = sort_index.find(a_facet.sort_field);
+ if(facet_sample_percent == 0) {
+ facet_sample_percent = 1;
+ }
+
size_t mod_value = 100 / facet_sample_percent;
auto num_facet_values = facet_index_v4->get_facet_count(facet_field.name);
@@ -3612,7 +3616,8 @@ Option<bool> Index::search(std::vector<query_tokens_t>& field_query_tokens, cons
delete [] exclude_token_ids;
delete [] excluded_result_ids;
- bool estimate_facets = (facet_sample_percent < 100 && all_result_ids_len > facet_sample_threshold);
+ bool estimate_facets = (facet_sample_percent > 0 && facet_sample_percent < 100 &&
+ all_result_ids_len > facet_sample_threshold);
bool is_wildcard_no_filter_query = is_wildcard_non_phrase_query && no_filters_provided;
if(!facets.empty()) {
diff --git a/test/collection_faceting_test.cpp b/test/collection_faceting_test.cpp
index 81760ec3..7df37703 100644
--- a/test/collection_faceting_test.cpp
+++ b/test/collection_faceting_test.cpp
@@ -1999,6 +1999,18 @@ TEST_F(CollectionFacetingTest, SampleFacetCounts) {
ASSERT_FALSE(res["facet_counts"][0]["sampled"].get<bool>());
+ // facet sample percent zero is treated as not sampled
+ res = coll1->search("*", {}, "", {"color"}, {}, {0}, 3, 1, FREQUENCY, {true}, 5,
+ spp::sparse_hash_set<std::string>(),
+ spp::sparse_hash_set<std::string>(), 10, "", 30, 4, "", 20, {}, {}, {}, 0,
+ "<mark>", "</mark>", {}, 1000, true, false, true, "", false, 6000 * 1000, 4, 7, fallback,
+ 4, {off}, 3, 3, 2, 2, false, "", true, 0, max_score, 0, 10).get();
+
+ ASSERT_EQ(1000, res["found"].get<size_t>());
+ ASSERT_EQ(1, res["facet_counts"].size());
+ ASSERT_EQ(2, res["facet_counts"][0]["counts"].size());
+ ASSERT_FALSE(res["facet_counts"][0]["sampled"].get<bool>());
+
// test for sample percent > 100
auto res_op = coll1->search("*", {}, "", {"color"}, {}, {0}, 3, 1, FREQUENCY, {true}, 5,