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

Fix pinned_hits_str ids order (#1578)

Krunal Gandhif514c42e2d63
diff --git a/src/index.cpp b/src/index.cpp
index e20e422e..1e712213 100644
--- a/src/index.cpp
+++ b/src/index.cpp
@@ -3764,6 +3764,9 @@ void Index::process_curated_ids(const std::vector<std::pair<uint32_t, uint32_t>>
included_ids_vec.push_back(seq_id_pos.first);
}
+ //sort the included ids to keep unidirectional iterators valid
+ std::sort(included_ids_vec.begin(), included_ids_vec.end());
+
if(group_limit != 0) {
// if one `id` of a group is present in curated hits, we have to exclude that entire group from results
auto group_by_field_it_vec = get_group_by_field_iterators(group_by_fields);
@@ -3778,8 +3781,6 @@ void Index::process_curated_ids(const std::vector<std::pair<uint32_t, uint32_t>>
}
}
- std::sort(included_ids_vec.begin(), included_ids_vec.end());
-
// if `filter_curated_hits` is enabled, we will remove curated hits that don't match filter condition
std::set<uint32_t> included_ids_set;
diff --git a/test/collection_grouping_test.cpp b/test/collection_grouping_test.cpp
index 693ebda8..2a88e36b 100644
--- a/test/collection_grouping_test.cpp
+++ b/test/collection_grouping_test.cpp
@@ -1183,4 +1183,66 @@ TEST_F(CollectionGroupingTest, GroupByMultipleFacetFieldsWithPinning) {
ASSERT_EQ(1, (int) res["facet_counts"][1]["counts"][2]["count"]);
ASSERT_STREQ("Xorp", res["facet_counts"][1]["counts"][2]["value"].get<std::string>().c_str());
+}
+
+TEST_F(CollectionGroupingTest, GroupByPinnedHitsOrder) {
+ auto res = coll_group->search("*", {"title"}, "size:=[12,11]", {}, {}, {0}, 50, 0, NOT_SET,
+ {false}, Index::DROP_TOKENS_THRESHOLD,
+ spp::sparse_hash_set<std::string>(),
+ spp::sparse_hash_set<std::string>(), 10, "", 30, 4,
+ "", 1,
+ {"6:1,1:2"}, {}, {"size"}, 1).get();
+
+ ASSERT_EQ(2, res["found"].get<size_t>());
+ ASSERT_EQ(2, res["grouped_hits"].size());
+
+ ASSERT_EQ(12, res["grouped_hits"][0]["group_key"][0].get<size_t>());
+ ASSERT_EQ(1, res["grouped_hits"][0]["hits"].size());
+ ASSERT_EQ("6", res["grouped_hits"][0]["hits"][0]["document"]["id"]);
+
+ ASSERT_EQ(11, res["grouped_hits"][1]["group_key"][0].get<size_t>());
+ ASSERT_EQ(1, res["grouped_hits"][1]["hits"].size());
+ ASSERT_EQ("1", res["grouped_hits"][1]["hits"][0]["document"]["id"]);
+
+ //try with pinned hits in other order
+ res = coll_group->search("*", {"title"}, "size:=[12,11]", {}, {}, {0}, 50, 0, NOT_SET,
+ {false}, Index::DROP_TOKENS_THRESHOLD,
+ spp::sparse_hash_set<std::string>(),
+ spp::sparse_hash_set<std::string>(), 10, "", 30, 4,
+ "", 1,
+ {"5:1,8:2"}, {}, {"size"}, 1).get();
+
+ ASSERT_EQ(2, res["found"].get<size_t>());
+ ASSERT_EQ(2, res["grouped_hits"].size());
+
+ ASSERT_EQ(11, res["grouped_hits"][0]["group_key"][0].get<size_t>());
+ ASSERT_EQ(1, res["grouped_hits"][0]["hits"].size());
+ ASSERT_EQ("5", res["grouped_hits"][0]["hits"][0]["document"]["id"]);
+
+ ASSERT_EQ(12, res["grouped_hits"][1]["group_key"][0].get<size_t>());
+ ASSERT_EQ(1, res["grouped_hits"][1]["hits"].size());
+ ASSERT_EQ("8", res["grouped_hits"][1]["hits"][0]["document"]["id"]);
+
+ //random order
+ res = coll_group->search("*", {"title"}, "size:=[12,11,10]", {}, {}, {0}, 50, 0, NOT_SET,
+ {false}, Index::DROP_TOKENS_THRESHOLD,
+ spp::sparse_hash_set<std::string>(),
+ spp::sparse_hash_set<std::string>(), 10, "", 30, 4,
+ "", 1,
+ {"5:1,8:2,0:3"}, {}, {"size"}, 1).get();
+
+ ASSERT_EQ(3, res["found"].get<size_t>());
+ ASSERT_EQ(3, res["grouped_hits"].size());
+
+ ASSERT_EQ(11, res["grouped_hits"][0]["group_key"][0].get<size_t>());
+ ASSERT_EQ(1, res["grouped_hits"][0]["hits"].size());
+ ASSERT_EQ("5", res["grouped_hits"][0]["hits"][0]["document"]["id"]);
+
+ ASSERT_EQ(12, res["grouped_hits"][1]["group_key"][0].get<size_t>());
+ ASSERT_EQ(1, res["grouped_hits"][1]["hits"].size());
+ ASSERT_EQ("8", res["grouped_hits"][1]["hits"][0]["document"]["id"]);
+
+ ASSERT_EQ(10, res["grouped_hits"][2]["group_key"][0].get<size_t>());
+ ASSERT_EQ(1, res["grouped_hits"][2]["hits"].size());
+ ASSERT_EQ("0", res["grouped_hits"][2]["hits"][0]["document"]["id"]);
}
\ No newline at end of file