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

More fine grain locking during updates.

Kishore Nallan0727f8a5b752
diff --git a/src/index.cpp b/src/index.cpp
index 83278f55..11614743 100644
--- a/src/index.cpp
+++ b/src/index.cpp
@@ -390,16 +390,21 @@ Option<uint32_t> Index::validate_index_in_memory(nlohmann::json& document, uint3
void Index::scrub_reindex_doc(nlohmann::json& update_doc, nlohmann::json& del_doc, nlohmann::json& old_doc) {
std::vector<std::string> del_keys;
- std::shared_lock lock(mutex);
for(auto it = del_doc.cbegin(); it != del_doc.cend(); it++) {
const std::string& field_name = it.key();
+
+ std::shared_lock lock(mutex);
+
const auto& search_field_it = search_schema.find(field_name);
if(search_field_it == search_schema.end()) {
continue;
}
- const auto& search_field = search_field_it->second;
+ const auto search_field = search_field_it->second; // copy, don't use reference!
+
+ lock.unlock();
+
bool arrays_match = false;
// compare values between old and update docs:
diff --git a/test/collection_test.cpp b/test/collection_test.cpp
index 38cb00d9..e52e745f 100644
--- a/test/collection_test.cpp
+++ b/test/collection_test.cpp
@@ -1063,6 +1063,12 @@ TEST_F(CollectionTest, ImportDocumentsUpsert) {
auto results = coll_mul_fields->search("*", query_fields, "starring:= [Will Ferrell]", {"starring"}, sort_fields, 0, 30, 1, FREQUENCY, false).get();
ASSERT_EQ(2, results["hits"].size());
+ // update existing record verbatim
+ std::vector<std::string> existing_records = {R"({"id": "0", "title": "Wake Up, Ron Burgundy: The Lost Movie"})"};
+ import_response = coll_mul_fields->add_many(existing_records, document, UPDATE);
+ ASSERT_TRUE(import_response["success"].get<bool>());
+ ASSERT_EQ(1, import_response["num_imported"].get<int>());
+
// update + upsert records
std::vector<std::string> more_records = {R"({"id": "0", "title": "The Fifth Harry", "starring": "Will Ferrell"})",
R"({"id": "2", "cast": ["Chris Fisher", "Rand Alan"]})",