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

Add test for updates.

Kishore Nallan54ddf63f2ed8
diff --git a/src/index.cpp b/src/index.cpp
index 4cccf134..210b333a 100644
--- a/src/index.cpp
+++ b/src/index.cpp
@@ -5519,6 +5519,11 @@ void Index::scrub_reindex_doc(const tsl::htrie_map<char, field>& search_schema,
nlohmann::json& update_doc,
nlohmann::json& del_doc,
const nlohmann::json& old_doc) {
+
+ /*LOG(INFO) << "update_doc: " << update_doc;
+ LOG(INFO) << "old_doc: " << old_doc;
+ LOG(INFO) << "del_doc: " << del_doc;*/
+
// del_doc contains fields that exist in both update doc and old doc
// But we will only remove fields that are different
diff --git a/test/collection_nested_fields_test.cpp b/test/collection_nested_fields_test.cpp
index daa19ec4..60f887cb 100644
--- a/test/collection_nested_fields_test.cpp
+++ b/test/collection_nested_fields_test.cpp
@@ -1139,4 +1139,96 @@ TEST_F(CollectionNestedFieldsTest, GroupByOnNestedFieldsWithWildcardSchema) {
ASSERT_EQ(5000, results["grouped_hits"][1]["group_key"][0].get<size_t>());
ASSERT_EQ(1, results["grouped_hits"][1]["hits"].size());
ASSERT_EQ("0", results["grouped_hits"][1]["hits"][0]["document"]["id"].get<std::string>());
-}
\ No newline at end of file
+}
+
+TEST_F(CollectionNestedFieldsTest, UpdateOfNestFields) {
+ nlohmann::json schema = R"({
+ "name": "coll1",
+ "fields": [
+ {"name": ".*", "type": "auto"}
+ ]
+ })"_json;
+
+ auto op = collectionManager.create_collection(schema);
+ ASSERT_TRUE(op.ok());
+ Collection* coll1 = op.get();
+
+ auto doc1 = R"({
+ "id": "0",
+ "company": {"num_employees": 2000, "founded": 1976},
+ "studies": [{"name": "College 1"}]
+ })"_json;
+
+ ASSERT_TRUE(coll1->add(doc1.dump(), CREATE).ok());
+
+ auto doc_update = R"({
+ "id": "0",
+ "company": {"num_employees": 2000, "founded": 1976, "year": 2000},
+ "studies": [{"name": "College Alpha", "year": 1967},{"name": "College Beta", "year": 1978}]
+ })"_json;
+ ASSERT_TRUE(coll1->add(doc_update.dump(), UPDATE).ok());
+
+ auto results = coll1->search("*", {}, "company.year: 2000", {}, {}, {0}, 10, 1, FREQUENCY, {false}).get();
+ ASSERT_EQ(1, results["found"].get<size_t>());
+
+ results = coll1->search("*", {}, "studies.year: 1967", {}, {}, {0}, 10, 1, FREQUENCY, {false}).get();
+ ASSERT_EQ(1, results["found"].get<size_t>());
+
+ results = coll1->search("*", {}, "studies.year: 1978", {}, {}, {0}, 10, 1, FREQUENCY, {false}).get();
+ ASSERT_EQ(1, results["found"].get<size_t>());
+
+ results = coll1->search("alpha", {"studies.name"}, "", {}, {}, {0}, 10, 1, FREQUENCY, {false}).get();
+ ASSERT_EQ(1, results["found"].get<size_t>());
+
+ results = coll1->search("beta", {"studies.name"}, "", {}, {}, {0}, 10, 1, FREQUENCY, {false}).get();
+ ASSERT_EQ(1, results["found"].get<size_t>());
+
+ // try removing fields via upsert
+ doc_update = R"({
+ "id": "0",
+ "company": {"num_employees": 2000, "founded": 1976},
+ "studies": [{"name": "College Alpha"}]
+ })"_json;
+ ASSERT_TRUE(coll1->add(doc_update.dump(), UPSERT).ok());
+
+ results = coll1->search("*", {}, "company.year: 2000", {}, {}, {0}, 10, 1, FREQUENCY, {false}).get();
+ ASSERT_EQ(0, results["found"].get<size_t>());
+
+ results = coll1->search("*", {}, "studies.year: 1967", {}, {}, {0}, 10, 1, FREQUENCY, {false}).get();
+ ASSERT_EQ(0, results["found"].get<size_t>());
+
+ results = coll1->search("*", {}, "studies.year: 1978", {}, {}, {0}, 10, 1, FREQUENCY, {false}).get();
+ ASSERT_EQ(0, results["found"].get<size_t>());
+
+ results = coll1->search("*", {}, "", {}, {}, {0}, 10, 1, FREQUENCY, {false}).get();
+ ASSERT_EQ(1, results["found"].get<size_t>());
+ ASSERT_EQ(3, results["hits"][0]["document"].size());
+ ASSERT_EQ(2, results["hits"][0]["document"]["company"].size());
+ ASSERT_EQ(2000, results["hits"][0]["document"]["company"]["num_employees"].get<size_t>());
+ ASSERT_EQ(1976, results["hits"][0]["document"]["company"]["founded"].get<size_t>());
+ ASSERT_EQ(1, results["hits"][0]["document"]["studies"].size());
+ ASSERT_EQ(1, results["hits"][0]["document"]["studies"][0].size());
+ ASSERT_EQ("College Alpha", results["hits"][0]["document"]["studies"][0]["name"].get<std::string>());
+
+ // via update (should not remove, since document can be partial)
+ doc_update = R"({
+ "id": "0",
+ "company": {"num_employees": 2000},
+ "studies": [{"name": "College Alpha"}]
+ })"_json;
+ ASSERT_TRUE(coll1->add(doc_update.dump(), UPDATE).ok());
+
+ results = coll1->search("*", {}, "company.founded: 1976", {}, {}, {0}, 10, 1, FREQUENCY, {false}).get();
+ ASSERT_EQ(1, results["found"].get<size_t>());
+
+ // via emplace (should not remove, since document can be partial)
+ doc_update = R"({
+ "id": "0",
+ "company": {},
+ "studies": [{"name": "College Alpha", "year": 1977}]
+ })"_json;
+ ASSERT_TRUE(coll1->add(doc_update.dump(), EMPLACE).ok());
+
+ results = coll1->search("*", {}, "company.num_employees: 2000", {}, {}, {0}, 10, 1, FREQUENCY, {false}).get();
+ ASSERT_EQ(1, results["found"].get<size_t>());
+}
diff --git a/test/collection_specific_more_test.cpp b/test/collection_specific_more_test.cpp
index cd64d6c2..b3479d1a 100644
--- a/test/collection_specific_more_test.cpp
+++ b/test/collection_specific_more_test.cpp
@@ -1034,3 +1034,65 @@ TEST_F(CollectionSpecificMoreTest, SearchingForMinusCharacter) {
ASSERT_EQ(1, results["hits"].size());
}
+
+TEST_F(CollectionSpecificMoreTest, UpsertUpdateEmplaceShouldAllRemoveIndex) {
+ nlohmann::json schema = R"({
+ "name": "coll1",
+ "fields": [
+ {"name": "title1", "type": "string", "optional": true},
+ {"name": "title2", "type": "string", "optional": true},
+ {"name": "title3", "type": "string", "optional": true}
+ ]
+ })"_json;
+
+ auto op = collectionManager.create_collection(schema);
+ ASSERT_TRUE(op.ok());
+ Collection* coll1 = op.get();
+
+ auto doc1 = R"({
+ "id": "0",
+ "title1": "Foo",
+ "title2": "Bar",
+ "title3": "Baz",
+ "data": "abcdefghijk"
+ })"_json;
+
+ ASSERT_TRUE(coll1->add(doc1.dump(), CREATE).ok());
+
+ // via upsert
+
+ auto doc_update = R"({
+ "id": "0",
+ "title2": "Bar",
+ "title3": "Baz"
+ })"_json;
+ ASSERT_TRUE(coll1->add(doc_update.dump(), UPSERT).ok());
+
+ auto results = coll1->search("foo", {"title1"}, "", {}, {}, {0}, 10, 1, FREQUENCY, {false}).get();
+ ASSERT_EQ(0, results["found"].get<size_t>());
+
+ results = coll1->search("bar", {"title2"}, "", {}, {}, {0}, 10, 1, FREQUENCY, {false}).get();
+ ASSERT_EQ(1, results["found"].get<size_t>());
+ ASSERT_EQ(3, results["hits"][0]["document"].size());
+
+ // via update, existing index should not be removed because update can send partial doc
+
+ doc_update = R"({
+ "id": "0",
+ "title3": "Baz"
+ })"_json;
+ ASSERT_TRUE(coll1->add(doc_update.dump(), UPDATE).ok());
+
+ results = coll1->search("bar", {"title2"}, "", {}, {}, {0}, 10, 1, FREQUENCY, {false}).get();
+ ASSERT_EQ(1, results["found"].get<size_t>());
+
+ // via emplace, existing index should not be removed because emplace could send partial doc
+
+ doc_update = R"({
+ "id": "0"
+ })"_json;
+ ASSERT_TRUE(coll1->add(doc_update.dump(), EMPLACE).ok());
+
+ results = coll1->search("baz", {"title3"}, "", {}, {}, {0}, 10, 1, FREQUENCY, {false}).get();
+ ASSERT_EQ(1, results["found"].get<size_t>());
+}