From cc980c2ecd56a2d998c49a1794d2dd449c62b407 Mon Sep 17 00:00:00 2001 From: Kishore Nallan Date: Wed, 14 Feb 2024 12:29:48 +0530 Subject: [PATCH] Treat zero facet sample percent as not sampled. --- src/index.cpp | 7 ++++++- test/collection_faceting_test.cpp | 12 ++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) 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 & 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 Index::search(std::vector& 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()); + // facet sample percent zero is treated as not sampled + res = coll1->search("*", {}, "", {"color"}, {}, {0}, 3, 1, FREQUENCY, {true}, 5, + spp::sparse_hash_set(), + spp::sparse_hash_set(), 10, "", 30, 4, "", 20, {}, {}, {}, 0, + "", "", {}, 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()); + ASSERT_EQ(1, res["facet_counts"].size()); + ASSERT_EQ(2, res["facet_counts"][0]["counts"].size()); + ASSERT_FALSE(res["facet_counts"][0]["sampled"].get()); + // test for sample percent > 100 auto res_op = coll1->search("*", {}, "", {"color"}, {}, {0}, 3, 1, FREQUENCY, {true}, 5,