From 5cf8b2c8d8dee8dfb68114d59d8822c4c2d781af Mon Sep 17 00:00:00 2001 From: krunal Date: Wed, 11 Oct 2023 16:39:56 +0530 Subject: [PATCH 1/4] add test for group_by with multiple facet field --- test/collection_grouping_test.cpp | 59 +++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/test/collection_grouping_test.cpp b/test/collection_grouping_test.cpp index c14a2608..def76477 100644 --- a/test/collection_grouping_test.cpp +++ b/test/collection_grouping_test.cpp @@ -986,4 +986,63 @@ TEST_F(CollectionGroupingTest, SkipToReverseGroupBy) { ASSERT_EQ("puma", res["grouped_hits"][2]["group_key"][0].get()); ASSERT_EQ(1, res["grouped_hits"][2]["hits"].size()); +} + +TEST_F(CollectionGroupingTest, GroupByMultipleFacetFields) { + auto res = coll_group->search("*", {}, "", {"brand", "colors"}, {}, {0}, 50, 1, FREQUENCY, + {false}, Index::DROP_TOKENS_THRESHOLD, + spp::sparse_hash_set(), + spp::sparse_hash_set(), 10, "", 30, 5, + "", 10, + {}, {}, {"size"}, 2).get(); + + ASSERT_EQ(12, res["found_docs"].get()); + ASSERT_EQ(3, res["found"].get()); + ASSERT_EQ(3, res["grouped_hits"].size()); + ASSERT_EQ(11, res["grouped_hits"][0]["group_key"][0].get()); + + ASSERT_EQ(2, res["grouped_hits"][0]["found"].get()); + ASSERT_FLOAT_EQ(4.8, res["grouped_hits"][0]["hits"][0]["document"]["rating"].get()); + ASSERT_EQ(11, res["grouped_hits"][0]["hits"][0]["document"]["size"].get()); + ASSERT_STREQ("5", res["grouped_hits"][0]["hits"][0]["document"]["id"].get().c_str()); + ASSERT_FLOAT_EQ(4.3, res["grouped_hits"][0]["hits"][1]["document"]["rating"].get()); + ASSERT_STREQ("1", res["grouped_hits"][0]["hits"][1]["document"]["id"].get().c_str()); + + ASSERT_EQ(7, res["grouped_hits"][1]["found"].get()); + ASSERT_FLOAT_EQ(4.8, res["grouped_hits"][1]["hits"][0]["document"]["rating"].get()); + ASSERT_STREQ("4", res["grouped_hits"][1]["hits"][0]["document"]["id"].get().c_str()); + ASSERT_FLOAT_EQ(4.6, res["grouped_hits"][1]["hits"][1]["document"]["rating"].get()); + ASSERT_STREQ("3", res["grouped_hits"][1]["hits"][1]["document"]["id"].get().c_str()); + + ASSERT_EQ(3, res["grouped_hits"][2]["found"].get()); + ASSERT_FLOAT_EQ(4.6, res["grouped_hits"][2]["hits"][0]["document"]["rating"].get()); + ASSERT_STREQ("2", res["grouped_hits"][2]["hits"][0]["document"]["id"].get().c_str()); + ASSERT_FLOAT_EQ(4.4, res["grouped_hits"][2]["hits"][1]["document"]["rating"].get()); + ASSERT_STREQ("8", res["grouped_hits"][2]["hits"][1]["document"]["id"].get().c_str()); + + ASSERT_STREQ("brand", res["facet_counts"][0]["field_name"].get().c_str()); + + ASSERT_EQ(3, (int) res["facet_counts"][0]["counts"][0]["count"]); + ASSERT_STREQ("Beta", res["facet_counts"][0]["counts"][0]["value"].get().c_str()); + + ASSERT_EQ(3, (int) res["facet_counts"][0]["counts"][1]["count"]); + ASSERT_STREQ("Omega", res["facet_counts"][0]["counts"][1]["value"].get().c_str()); + + ASSERT_EQ(2, (int) res["facet_counts"][0]["counts"][2]["count"]); + ASSERT_STREQ("Xorp", res["facet_counts"][0]["counts"][2]["value"].get().c_str()); + + ASSERT_EQ(1, (int) res["facet_counts"][0]["counts"][3]["count"]); + ASSERT_STREQ("Zeta", res["facet_counts"][0]["counts"][3]["value"].get().c_str()); + + + ASSERT_STREQ("colors", res["facet_counts"][1]["field_name"].get().c_str()); + + ASSERT_EQ(3, (int) res["facet_counts"][1]["counts"][0]["count"]); + ASSERT_STREQ("blue", res["facet_counts"][1]["counts"][0]["value"].get().c_str()); + + ASSERT_EQ(3, (int) res["facet_counts"][1]["counts"][1]["count"]); + ASSERT_STREQ("white", res["facet_counts"][1]["counts"][1]["value"].get().c_str()); + + ASSERT_EQ(1, (int) res["facet_counts"][1]["counts"][3]["count"]); + ASSERT_STREQ("red", res["facet_counts"][1]["counts"][3]["value"].get().c_str()); } \ No newline at end of file From f1cbb71cdf245319d0d892826ac16c9342f64cfd Mon Sep 17 00:00:00 2001 From: krunal Date: Wed, 11 Oct 2023 19:10:07 +0530 Subject: [PATCH 2/4] correcting test --- test/collection_grouping_test.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/collection_grouping_test.cpp b/test/collection_grouping_test.cpp index def76477..b98fd02f 100644 --- a/test/collection_grouping_test.cpp +++ b/test/collection_grouping_test.cpp @@ -1043,6 +1043,6 @@ TEST_F(CollectionGroupingTest, GroupByMultipleFacetFields) { ASSERT_EQ(3, (int) res["facet_counts"][1]["counts"][1]["count"]); ASSERT_STREQ("white", res["facet_counts"][1]["counts"][1]["value"].get().c_str()); - ASSERT_EQ(1, (int) res["facet_counts"][1]["counts"][3]["count"]); - ASSERT_STREQ("red", res["facet_counts"][1]["counts"][3]["value"].get().c_str()); + ASSERT_EQ(1, (int) res["facet_counts"][1]["counts"][2]["count"]); + ASSERT_STREQ("red", res["facet_counts"][1]["counts"][2]["value"].get().c_str()); } \ No newline at end of file From ef7a08f9d9cc3c8baaf18f320215a381709d7aae Mon Sep 17 00:00:00 2001 From: krunal Date: Wed, 11 Oct 2023 19:11:29 +0530 Subject: [PATCH 3/4] fix for failing test --- src/index.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/index.cpp b/src/index.cpp index 871361fe..962a307f 100644 --- a/src/index.cpp +++ b/src/index.cpp @@ -1464,6 +1464,10 @@ void Index::do_facets(std::vector & facets, facet_query_t & facet_query, } } } + //reset iterator position + for(auto& kv : group_by_field_it_vec) { + kv.it.set_index(0); + } } } From c7c099b8b4b45f121f181f8157607d77d084c545 Mon Sep 17 00:00:00 2001 From: krunal Date: Fri, 13 Oct 2023 11:32:17 +0530 Subject: [PATCH 4/4] intialize iterator per facet field --- src/index.cpp | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/src/index.cpp b/src/index.cpp index 962a307f..a5882ee4 100644 --- a/src/index.cpp +++ b/src/index.cpp @@ -1289,9 +1289,6 @@ void Index::do_facets(std::vector & facets, facet_query_t & facet_query, } std::vector group_by_field_it_vec; - if (group_limit != 0) { - group_by_field_it_vec = get_group_by_field_iterators(group_by_fields); - } size_t total_docs = seq_ids->num_ids(); // assumed that facet fields have already been validated upstream @@ -1369,6 +1366,10 @@ void Index::do_facets(std::vector & facets, facet_query_t & facet_query, posting_list_t::iterator_t facet_index_it = facet_index->new_iterator(); std::vector facet_hashes; + if (group_limit != 0) { + group_by_field_it_vec = get_group_by_field_iterators(group_by_fields); + } + for(size_t i = 0; i < results_size; i++) { // if sampling is enabled, we will skip a portion of the results to speed up things if(estimate_facets) { @@ -1464,10 +1465,6 @@ void Index::do_facets(std::vector & facets, facet_query_t & facet_query, } } } - //reset iterator position - for(auto& kv : group_by_field_it_vec) { - kv.it.set_index(0); - } } }