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

Bug fix: incomplete string array deletion.

kishorenc38bac77c437d
diff --git a/include/collection.h b/include/collection.h
index 9dbf5597..04c15526 100644
--- a/include/collection.h
+++ b/include/collection.h
@@ -250,6 +250,8 @@ public:
static void prune_document(nlohmann::json &document, const spp::sparse_hash_set<std::string> & include_fields,
const spp::sparse_hash_set<std::string> & exclude_fields);
+ const std::vector<Index *> &_get_indexes() const;
+
// strings under this length will be fully highlighted, instead of showing a snippet of relevant portion
enum {SNIPPET_STR_ABOVE_LEN = 30};
diff --git a/include/index.h b/include/index.h
index bc72ad04..6e03265a 100644
--- a/include/index.h
+++ b/include/index.h
@@ -240,6 +240,8 @@ public:
const std::unordered_map<std::string, field> & search_schema,
const std::map<std::string, field> & facet_schema);
+ const spp::sparse_hash_map<std::string, art_tree *> &_get_search_index() const;
+
// for limiting number of results on multiple candidates / query rewrites
enum {SEARCH_LIMIT_NUM = 100};
diff --git a/src/collection.cpp b/src/collection.cpp
index 69e45d6c..3524214d 100644
--- a/src/collection.cpp
+++ b/src/collection.cpp
@@ -1130,3 +1130,7 @@ Option<bool> Collection::get_document_from_store(const std::string &seq_id_key,
return Option<bool>(true);
}
+
+const std::vector<Index *> &Collection::_get_indexes() const {
+ return indices;
+}
diff --git a/src/index.cpp b/src/index.cpp
index c0a29bcd..a6266cb5 100644
--- a/src/index.cpp
+++ b/src/index.cpp
@@ -1555,7 +1555,10 @@ Option<uint32_t> Index::remove(const uint32_t seq_id, nlohmann::json & document)
if(name_field.second.type == field_types::STRING) {
StringUtils::split(document[name_field.first], tokens, " ");
} else if(name_field.second.type == field_types::STRING_ARRAY) {
- tokens = document[name_field.first].get<std::vector<std::string>>();
+ std::vector<std::string> values = document[name_field.first].get<std::vector<std::string>>();
+ for(const std::string & value: values) {
+ StringUtils::split(value, tokens, " ");
+ }
} else if(name_field.second.type == field_types::INT32) {
const int KEY_LEN = 8;
unsigned char key[KEY_LEN];
@@ -1677,4 +1680,8 @@ Option<uint32_t> Index::remove(const uint32_t seq_id, nlohmann::json & document)
art_leaf* Index::get_token_leaf(const std::string & field_name, const unsigned char* token, uint32_t token_len) {
const art_tree *t = search_index.at(field_name);
return (art_leaf*) art_search(t, token, (int) token_len);
-}
\ No newline at end of file
+}
+
+const spp::sparse_hash_map<std::string, art_tree *> &Index::_get_search_index() const {
+ return search_index;
+}
diff --git a/test/collection_test.cpp b/test/collection_test.cpp
index edf8206b..e3395ad6 100644
--- a/test/collection_test.cpp
+++ b/test/collection_test.cpp
@@ -2378,6 +2378,70 @@ TEST_F(CollectionTest, DeletionOfADocument) {
collectionManager.drop_collection("collection_for_del");
}
+TEST_F(CollectionTest, DeletionOfDocumentArrayFields) {
+ Collection *coll1;
+
+ std::vector<field> fields = {field("strarray", field_types::STRING_ARRAY, false),
+ field("int32array", field_types::INT32_ARRAY, false),
+ field("int64array", field_types::INT64_ARRAY, false),
+ field("floatarray", field_types::FLOAT_ARRAY, false),
+ field("boolarray", field_types::BOOL_ARRAY, false),
+ field("points", field_types::INT32, false)};
+
+ std::vector<sort_by> sort_fields = { sort_by("points", "DESC") };
+
+ coll1 = collectionManager.get_collection("coll1");
+ if(coll1 == nullptr) {
+ coll1 = collectionManager.create_collection("coll1", fields, "points").get();
+ }
+
+ nlohmann::json doc;
+ doc["id"] = "100";
+ doc["strarray"] = {"Cell Phones", "Cell Phone Accessories", "Cell Phone Cases & Clips"};
+ doc["int32array"] = {100, 200, 300};
+ doc["int64array"] = {1582369739000, 1582369739000, 1582369739000};
+ doc["floatarray"] = {19.99, 400.999, 500};
+ doc["boolarray"] = {true, false, true};
+ doc["points"] = 25;
+
+ Option<nlohmann::json> add_op = coll1->add(doc.dump());
+ ASSERT_TRUE(add_op.ok());
+
+ nlohmann::json res = coll1->search("phone", {"strarray"}, "", {}, sort_fields, 0, 10, 1,
+ token_ordering::FREQUENCY, true, 10, spp::sparse_hash_set<std::string>(),
+ spp::sparse_hash_set<std::string>(), 10, 500).get();
+
+ ASSERT_EQ(1, res["found"]);
+
+ Option<std::string> rem_op = coll1->remove("100");
+
+ ASSERT_TRUE(rem_op.ok());
+
+ res = coll1->search("phone", {"strarray"}, "", {}, sort_fields, 0, 10, 1,
+ token_ordering::FREQUENCY, true, 10, spp::sparse_hash_set<std::string>(),
+ spp::sparse_hash_set<std::string>(), 10, 500).get();
+
+ ASSERT_EQ(0, res["found"].get<int32_t>());
+
+ // also assert against the actual index
+ Index *index = coll1->_get_indexes()[0]; // seq id will always be zero for first document
+ auto search_index = index->_get_search_index();
+
+ auto strarray_tree = search_index["strarray"];
+ auto int32array_tree = search_index["int32array"];
+ auto int64array_tree = search_index["int64array"];
+ auto floatarray_tree = search_index["floatarray"];
+ auto boolarray_tree = search_index["boolarray"];
+
+ ASSERT_EQ(0, art_size(strarray_tree));
+ ASSERT_EQ(0, art_size(int32array_tree));
+ ASSERT_EQ(0, art_size(int64array_tree));
+ ASSERT_EQ(0, art_size(floatarray_tree));
+ ASSERT_EQ(0, art_size(boolarray_tree));
+
+ collectionManager.drop_collection("coll1");
+}
+
nlohmann::json get_prune_doc() {
nlohmann::json document;
document["one"] = 1;