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

Numerical facet field values must be fully removed.

Kishore Nallan31559f15b29a
diff --git a/include/index.h b/include/index.h
index b30c1cc4..20ee3bd5 100644
--- a/include/index.h
+++ b/include/index.h
@@ -513,6 +513,9 @@ private:
bool common_results_exist(std::vector<art_leaf*>& leaves, bool must_match_phrase) const;
+ static void remove_facet_token(const field& search_field, spp::sparse_hash_map<std::string, art_tree*>& search_index,
+ const std::string& token, uint32_t seq_id);
+
public:
// for limiting number of results on multiple candidates / query rewrites
enum {TYPO_TOKENS_THRESHOLD = 1};
diff --git a/src/index.cpp b/src/index.cpp
index 41b0befd..b740e7ae 100644
--- a/src/index.cpp
+++ b/src/index.cpp
@@ -4931,6 +4931,22 @@ inline uint32_t Index::next_suggestion(const std::vector<token_candidates> &toke
return total_cost;
}
+void Index::remove_facet_token(const field& search_field, spp::sparse_hash_map<std::string, art_tree*>& search_index,
+ const std::string& token, uint32_t seq_id) {
+ const unsigned char *key = (const unsigned char *) token.c_str();
+ int key_len = (int) (token.length() + 1);
+ const std::string& field_name = search_field.faceted_name();
+
+ art_leaf* leaf = (art_leaf *) art_search(search_index.at(field_name), key, key_len);
+ if(leaf != nullptr) {
+ posting_t::erase(leaf->values, seq_id);
+ if (posting_t::num_ids(leaf->values) == 0) {
+ void* values = art_delete(search_index.at(field_name), key, key_len);
+ posting_t::destroy_list(values);
+ }
+ }
+}
+
void Index::remove_field(uint32_t seq_id, const nlohmann::json& document, const std::string& field_name) {
const auto& search_field_it = search_schema.find(field_name);
if(search_field_it == search_schema.end()) {
@@ -4975,6 +4991,9 @@ void Index::remove_field(uint32_t seq_id, const nlohmann::json& document, const
for(int32_t value: values) {
num_tree_t* num_tree = numerical_index.at(field_name);
num_tree->remove(value, seq_id);
+ if(search_field.facet) {
+ remove_facet_token(search_field, search_index, std::to_string(value), seq_id);
+ }
}
} else if(search_field.is_int64()) {
const std::vector<int64_t>& values = search_field.is_single_integer() ?
@@ -4983,6 +5002,9 @@ void Index::remove_field(uint32_t seq_id, const nlohmann::json& document, const
for(int64_t value: values) {
num_tree_t* num_tree = numerical_index.at(field_name);
num_tree->remove(value, seq_id);
+ if(search_field.facet) {
+ remove_facet_token(search_field, search_index, std::to_string(value), seq_id);
+ }
}
} else if(search_field.is_float()) {
const std::vector<float>& values = search_field.is_single_float() ?
@@ -4992,6 +5014,9 @@ void Index::remove_field(uint32_t seq_id, const nlohmann::json& document, const
num_tree_t* num_tree = numerical_index.at(field_name);
int64_t fintval = float_to_in64_t(value);
num_tree->remove(fintval, seq_id);
+ if(search_field.facet) {
+ remove_facet_token(search_field, search_index, StringUtils::float_to_str(value), seq_id);
+ }
}
if(search_field.num_dim) {
@@ -5006,6 +5031,9 @@ void Index::remove_field(uint32_t seq_id, const nlohmann::json& document, const
num_tree_t* num_tree = numerical_index.at(field_name);
int64_t bool_int64 = value ? 1 : 0;
num_tree->remove(bool_int64, seq_id);
+ if(search_field.facet) {
+ remove_facet_token(search_field, search_index, std::to_string(value), seq_id);
+ }
}
} else if(search_field.is_geopoint()) {
auto geo_index = geopoint_index[field_name];
@@ -5044,7 +5072,6 @@ void Index::remove_field(uint32_t seq_id, const nlohmann::json& document, const
// remove facets
const auto& field_facets_it = facet_index_v3.find(field_name);
-
if(field_facets_it != facet_index_v3.end()) {
const auto& fvalues_it = field_facets_it->second[seq_id % ARRAY_FACET_DIM]->find(seq_id);
if(fvalues_it != field_facets_it->second[seq_id % ARRAY_FACET_DIM]->end()) {
diff --git a/test/collection_specific_more_test.cpp b/test/collection_specific_more_test.cpp
index 1d50435a..e233b632 100644
--- a/test/collection_specific_more_test.cpp
+++ b/test/collection_specific_more_test.cpp
@@ -1339,3 +1339,45 @@ TEST_F(CollectionSpecificMoreTest, AutoSchemaWithObjectValueAsFirstDoc) {
auto res = coll1->search("*", {}, "num:100", {}, {}, {2}, 10, 1, FREQUENCY, {true}).get();
ASSERT_EQ(1, res["hits"].size());
}
+
+TEST_F(CollectionSpecificMoreTest, VerifyDeletionOfFacetStringIndex) {
+ nlohmann::json schema = R"({
+ "name": "coll1",
+ "fields": [
+ {"name": "title", "type": "string", "facet": true},
+ {"name": "i32", "type": "int32", "facet": true},
+ {"name": "float", "type": "float", "facet": true},
+ {"name": "i64", "type": "int64", "facet": true},
+ {"name": "i32arr", "type": "int32[]", "facet": true},
+ {"name": "floatarr", "type": "float[]", "facet": true},
+ {"name": "i64arr", "type": "int64[]", "facet": true}
+ ]
+ })"_json;
+
+ Collection* coll1 = collectionManager.create_collection(schema).get();
+
+ nlohmann::json doc;
+ doc["id"] = "0";
+ doc["title"] = "Title";
+ doc["i32"] = 100;
+ doc["float"] = 2.40;
+ doc["i64"] = 10000;
+ doc["i32arr"] = {100};
+ doc["floatarr"] = {2.50};
+ doc["i64arr"] = {10000};
+
+ ASSERT_TRUE(coll1->add(doc.dump()).ok());
+
+ auto search_index = coll1->_get_index()->_get_search_index();
+ ASSERT_EQ(7, search_index.size());
+ for(const auto& kv: search_index) {
+ ASSERT_EQ(1, kv.second->size);
+ }
+
+ coll1->remove("0");
+ ASSERT_EQ(7, search_index.size());
+
+ for(const auto& kv: search_index) {
+ ASSERT_EQ(0, kv.second->size);
+ }
+}