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 infix index deletion edge case.

Kishore Nallanf47325436fa7
diff --git a/src/index.cpp b/src/index.cpp
index f883c127..2e828aab 100644
--- a/src/index.cpp
+++ b/src/index.cpp
@@ -6025,13 +6025,13 @@ void Index::remove_field(uint32_t seq_id, const nlohmann::json& document, const
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);
- }
- }
- if(search_field.infix) {
- auto strhash = StringUtils::hash_wy(key, token.size());
- const auto& infix_sets = infix_index.at(search_field.name);
- infix_sets[strhash % 4]->erase(token);
+ if(search_field.infix) {
+ auto strhash = StringUtils::hash_wy(key, token.size());
+ const auto& infix_sets = infix_index.at(search_field.name);
+ infix_sets[strhash % 4]->erase(token);
+ }
+ }
}
}
} else if(search_field.is_int32()) {
diff --git a/test/collection_infix_search_test.cpp b/test/collection_infix_search_test.cpp
index f1b11067..bdf67e27 100644
--- a/test/collection_infix_search_test.cpp
+++ b/test/collection_infix_search_test.cpp
@@ -462,7 +462,7 @@ TEST_F(CollectionInfixSearchTest, InfixDeleteAndUpdate) {
collectionManager.drop_collection("coll1");
}
-TEST_F(CollectionInfixSearchTest, MultiFielInfixSearch) {
+TEST_F(CollectionInfixSearchTest, MultiFieldInfixSearch) {
std::vector<field> fields = {field("title", field_types::STRING, false, false, true, "", -1, 1),
field("mpn", field_types::STRING, false, false, true, "", -1, 1),
field("points", field_types::INT32, false),};
@@ -494,3 +494,53 @@ TEST_F(CollectionInfixSearchTest, MultiFielInfixSearch) {
collectionManager.drop_collection("coll1");
}
+
+TEST_F(CollectionInfixSearchTest, DeleteDocWithInfixIndex) {
+ std::vector<field> fields = {field("title", field_types::STRING, false, false, true, "", -1, 1),
+ field("mpn", field_types::STRING, false, false, true, "", -1, 1),
+ field("points", field_types::INT32, false),};
+
+ Collection* coll1 = collectionManager.create_collection("coll1", 1, fields, "points").get();
+
+ nlohmann::json doc;
+ doc["id"] = "0";
+ doc["title"] = "Running Shoe";
+ doc["mpn"] = "HYDGHSGAH";
+ doc["points"] = 100;
+ ASSERT_TRUE(coll1->add(doc.dump()).ok());
+
+ doc["id"] = "1";
+ doc["title"] = "Running Band";
+ doc["mpn"] = "GHX100037IN";
+ doc["points"] = 100;
+ ASSERT_TRUE(coll1->add(doc.dump()).ok());
+
+ auto results = coll1->search("nni",
+ {"title"}, "", {}, {}, {0}, 3, 1, FREQUENCY, {true}, 5,
+ spp::sparse_hash_set<std::string>(),
+ spp::sparse_hash_set<std::string>(), 10, "", 30, 4, "title", 20, {}, {}, {}, 0,
+ "<mark>", "</mark>", {}, 1000, true, false, true, "", false, 6000 * 1000, 4, 7, fallback,
+ 4, {always}).get();
+
+ ASSERT_EQ(2, results["found"].get<size_t>());
+ ASSERT_EQ(2, results["hits"].size());
+
+ // drop one document
+
+ coll1->remove("0");
+
+ // search again
+
+ results = coll1->search("nni",
+ {"title"}, "", {}, {}, {0}, 3, 1, FREQUENCY, {true}, 5,
+ spp::sparse_hash_set<std::string>(),
+ spp::sparse_hash_set<std::string>(), 10, "", 30, 4, "title", 20, {}, {}, {}, 0,
+ "<mark>", "</mark>", {}, 1000, true, false, true, "", false, 6000 * 1000, 4, 7, fallback,
+ 4, {always}).get();
+
+ ASSERT_EQ(1, results["found"].get<size_t>());
+ ASSERT_EQ(1, results["hits"].size());
+ ASSERT_STREQ("1", results["hits"][0]["document"]["id"].get<std::string>().c_str());
+
+ collectionManager.drop_collection("coll1");
+}
\ No newline at end of file