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 facet value hash mapping being cleared on sparsification.

Kishore Nallancdb655289831
diff --git a/src/facet_index.cpp b/src/facet_index.cpp
index 7f209250..6b382155 100644
--- a/src/facet_index.cpp
+++ b/src/facet_index.cpp
@@ -459,6 +459,10 @@ size_t facet_index_t::get_facet_indexes(const std::string& field_name,
for(auto facet_index_map_it = facet_index_map.begin(); facet_index_map_it != facet_index_map.end(); ++facet_index_map_it) {
//auto ids = facet_index_map_it->seq_ids;
auto ids = facet_index_map_it->second.seq_ids;
+ if(!ids) {
+ continue;
+ }
+
ids_t::uncompress(ids, id_list);
// emplacing seq_id => next_facet_id
@@ -560,7 +564,8 @@ size_t facet_index_t::facet_val_num_ids(const string &field_name, const string &
return 0;
}
- return ids_t::num_ids(facet_field_map_it->second.fvalue_seq_ids[fvalue].seq_ids);
+ auto seq_ids = facet_field_map_it->second.fvalue_seq_ids[fvalue].seq_ids;
+ return seq_ids ? ids_t::num_ids(seq_ids) : 0;
}
size_t facet_index_t::facet_node_count(const string &field_name, const string &fvalue) {
@@ -604,8 +609,9 @@ void facet_index_t::check_for_high_cardinality(const string& field_name, size_t
auto& fvalue_seq_ids = facet_field_map_it->second.fvalue_seq_ids;
for(auto it = fvalue_seq_ids.begin(); it != fvalue_seq_ids.end(); ++it) {
ids_t::destroy_list(it->second.seq_ids);
+ it->second.seq_ids = nullptr;
}
- fvalue_seq_ids.clear();
+
facet_field_map_it->second.counts.clear();
facet_field_map_it->second.count_map.clear();
facet_field_map_it->second.has_value_index = false;
diff --git a/test/facet_index_test.cpp b/test/facet_index_test.cpp
index c4a3757b..61e4e868 100644
--- a/test/facet_index_test.cpp
+++ b/test/facet_index_test.cpp
@@ -30,6 +30,32 @@ TEST(FacetIndexTest, FacetValueDeletionString) {
ASSERT_FALSE(findex.facet_value_exists("brand", "nike"));
}
+TEST(FacetIndexTest, HighCardinalityCheck) {
+ facet_index_t findex;
+
+ for(size_t i = 0; i < 101; i++) {
+ findex.initialize("field_" + std::to_string(i));
+ }
+
+ std::unordered_map<facet_value_id_t, std::vector<uint32_t>, facet_value_id_t::Hash> fvalue_to_seq_ids;
+ std::unordered_map<uint32_t, std::vector<facet_value_id_t>> seq_id_to_fvalues;
+
+ facet_value_id_t nike("nike", 1);
+
+ fvalue_to_seq_ids[nike] = {0, 1, 2};
+ seq_id_to_fvalues[0] = {nike};
+ seq_id_to_fvalues[1] = {nike};
+ seq_id_to_fvalues[2] = {nike};
+
+ findex.insert("field_1", fvalue_to_seq_ids, seq_id_to_fvalues, true);
+ ASSERT_EQ(3, findex.facet_val_num_ids("field_1", "nike"));
+
+ findex.check_for_high_cardinality("field_1", 20000);
+ ASSERT_TRUE(findex.facet_value_exists("field_1", "nike"));
+
+ ASSERT_EQ(0, findex.facet_val_num_ids("field_1", "nike"));
+}
+
TEST(FacetIndexTest, FacetValueDeletionOfLongString) {
facet_index_t findex;
findex.initialize("brand");