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
C++ algoliadatastoreelasticsearchenterprise-searchfacetingfull-text-searchfuzzy-searchgeosearchin-memoryinstantsearchmerchandisingpineconesearchsearch-enginesemantic-searchsimilarity-searchsite-searchsynonymstypo-tolerancevector-search
Facet index unit tests.
Kishore Nallan976d62657fd0
diff --git a/include/facet_index.h b/include/facet_index.hindex 998471c0..af643fe8 100644--- a/include/facet_index.h+++ b/include/facet_index.h@@ -40,7 +40,7 @@ struct docid_count_t {};class facet_index_t {-private:+public:struct facet_count_t {facet_count_t() = delete;@@ -63,7 +63,7 @@ private:uint32_t count;uint32_t facet_id;};-+private:struct facet_id_seq_ids_t {void* seq_ids;uint32_t facet_id;@@ -156,8 +156,10 @@ public://get fhash=>int64 map for statsconst spp::sparse_hash_map<uint32_t, int64_t>& get_fhash_int64_map(const std::string& field_name);- void update_count_nodes(std::list<facet_count_t>& count_list,+ static void update_count_nodes(std::list<facet_count_t>& count_list,std::map<uint32_t, std::list<facet_count_t>::iterator>& count_map,uint32_t old_count, uint32_t new_count,- std::list<facet_count_t>::iterator& curr) const;+ std::list<facet_count_t>::iterator& curr) ;++ bool facet_value_exists(const std::string& field_name, const std::string& fvalue);};\ No newline at end of filediff --git a/src/facet_index.cpp b/src/facet_index.cppindex 119f6e7c..7efa8d72 100644--- a/src/facet_index.cpp+++ b/src/facet_index.cpp@@ -2,7 +2,6 @@#include <tokenizer.h>#include "string_utils.h"#include "array_utils.h"-#include "bazel-src/external/onnx_runtime/cmake/external/protobuf/src/google/protobuf/map.h"void facet_index_t::initialize(const std::string& field) {const auto facet_field_map_it = facet_field_map.find(field);@@ -121,7 +120,7 @@ void facet_index_t::insert(const std::string& field_name,std::unordered_map<facevoid facet_index_t::update_count_nodes(std::list<facet_count_t>& count_list,std::map<uint32_t, std::list<facet_count_t>::iterator>& count_map,uint32_t old_count, uint32_t new_count,- std::list<facet_count_t>::iterator& curr) const {+ std::list<facet_count_t>::iterator& curr) {auto count_map_it = count_map.lower_bound(new_count);@@ -226,8 +225,7 @@ void facet_index_t::remove(const std::string& field_name, const uint32_t seq_id)if(ids_t::num_ids(ids) == 0) {ids_t::destroy_list(ids);- std::string dead_fvalue;- dead_fvalues.push_back(dead_fvalue);+ dead_fvalues.push_back(facet_ids_seq_ids->first);//remove from int64 lookup map firstauto& fhash_int64_map = facet_field_it->second.fhash_to_int64_map;@@ -236,10 +234,6 @@ void facet_index_t::remove(const std::string& field_name, const uint32_t seq_id)count_map.erase(new_count);count_list.erase(curr);-- auto node = facet_index_map.extract(facet_ids_seq_ids->first);- node.key() = dead_fvalue;- facet_index_map.insert(std::move(node));}}}@@ -467,4 +461,14 @@ const spp::sparse_hash_map<uint32_t , int64_t >& facet_index_t::get_fhash_int64_const auto& facet_index = facet_field_map_it->second;return facet_index.fhash_to_int64_map;-}\ No newline at end of file+}++bool facet_index_t::facet_value_exists(const std::string& field_name, const std::string& fvalue) {+ const auto facet_field_map_it = facet_field_map.find(field_name);+ if(facet_field_map_it == facet_field_map.end()) {+ return false;+ }++ const auto& facet_index = facet_field_map_it->second;+ return facet_index.fvalue_seq_ids.find(fvalue) != facet_index.fvalue_seq_ids.end();+}diff --git a/test/facet_index_test.cpp b/test/facet_index_test.cppnew file mode 100644index 00000000..7859e392--- /dev/null+++ b/test/facet_index_test.cpp@@ -0,0 +1,101 @@+#include <gtest/gtest.h>+#include "facet_index.h"++TEST(FacetIndexTest, FacetValueDeletion) {+ facet_index_t findex;+ 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("brands", fvalue_to_seq_ids, seq_id_to_fvalues, true);+ findex.remove("nike", 0);+ findex.remove("nike", 1);+ findex.remove("nike", 2);++ ASSERT_FALSE(findex.facet_value_exists("brands", "nike"));+}++TEST(FacetIndexTest, UpdateWhenAllCountsLessThanNewCount) {+ // 5, 4, [4 -> 7]+ std::list<facet_index_t::facet_count_t> count_list;+ count_list.push_back(facet_index_t::facet_count_t("", 5, 0));+ count_list.push_back(facet_index_t::facet_count_t("", 4, 1));+ count_list.push_back(facet_index_t::facet_count_t("", 4, 2));++ std::map<uint32_t, std::list<facet_index_t::facet_count_t>::iterator> count_map;+ count_map.emplace(5, count_list.begin());+ count_map.emplace(4, std::prev(count_list.end()));++ auto curr = std::prev(count_list.end());+ curr->count = 7;+ facet_index_t::update_count_nodes(count_list, count_map, 4, 7, curr);++ // New order: 7, 5, 4+ ASSERT_EQ(7, count_list.begin()->count);+ ASSERT_EQ(5, std::next(count_list.begin(), 1)->count);+ ASSERT_EQ(4, std::next(count_list.begin(), 2)->count);++ ASSERT_EQ(3, count_map.size());+ ASSERT_EQ(count_list.begin(), count_map[7]);+ ASSERT_EQ(std::next(count_list.begin(), 1), count_map[5]);+ ASSERT_EQ(std::next(count_list.begin(), 2), count_map[4]);++ // 5, 4, [4 -> 7], 3++ count_list.clear();+ count_map.clear();++ count_list.push_back(facet_index_t::facet_count_t("", 5, 0));+ count_list.push_back(facet_index_t::facet_count_t("", 4, 1));+ count_list.push_back(facet_index_t::facet_count_t("", 4, 2));+ count_list.push_back(facet_index_t::facet_count_t("", 3, 3));++ count_map.emplace(5, count_list.begin());+ count_map.emplace(4, std::next(count_list.begin(), 2));+ count_map.emplace(3, std::next(count_list.begin(), 3));++ curr = std::next(count_list.begin(), 2);+ curr->count = 7;+ facet_index_t::update_count_nodes(count_list, count_map, 4, 7, curr);++ // New order: 7, 5, 4, 3+ ASSERT_EQ(7, count_list.begin()->count);+ ASSERT_EQ(5, std::next(count_list.begin(), 1)->count);+ ASSERT_EQ(4, std::next(count_list.begin(), 2)->count);+ ASSERT_EQ(3, std::next(count_list.begin(), 3)->count);++ ASSERT_EQ(4, count_map.size());+ ASSERT_EQ(count_list.begin(), count_map[7]);+ ASSERT_EQ(std::next(count_list.begin(), 1), count_map[5]);+ ASSERT_EQ(std::next(count_list.begin(), 2), count_map[4]);+ ASSERT_EQ(std::next(count_list.begin(), 3), count_map[3]);++ // 5, [4 -> 7]++ count_list.clear();+ count_map.clear();++ count_list.push_back(facet_index_t::facet_count_t("", 5, 0));+ count_list.push_back(facet_index_t::facet_count_t("", 4, 1));++ count_map.emplace(5, count_list.begin());+ count_map.emplace(4, std::next(count_list.begin(), 1));++ curr = std::next(count_list.begin(), 1);+ curr->count = 7;+ facet_index_t::update_count_nodes(count_list, count_map, 4, 7, curr);++ // New order: 7, 5+ ASSERT_EQ(7, count_list.begin()->count);+ ASSERT_EQ(5, std::next(count_list.begin(), 1)->count);++ ASSERT_EQ(2, count_map.size());+ ASSERT_EQ(count_list.begin(), count_map[7]);+ ASSERT_EQ(std::next(count_list.begin(), 1), count_map[5]);+}