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 document update failing if reference field was not included. (#1515)

Harpreet Sangarec4311635e1f
diff --git a/include/collection.h b/include/collection.h
index 484862c1..186c414c 100644
--- a/include/collection.h
+++ b/include/collection.h
@@ -431,7 +431,8 @@ public:
static Option<bool> add_reference_helper_fields(nlohmann::json& document, const tsl::htrie_map<char, field>& schema,
const spp::sparse_hash_map<std::string, reference_pair>& reference_fields,
- tsl::htrie_set<char>& object_reference_helper_fields);
+ tsl::htrie_set<char>& object_reference_helper_fields,
+ const bool& is_update);
Option<doc_seq_id_t> to_doc(const std::string& json_str, nlohmann::json& document,
const index_operation_t& operation,
diff --git a/src/collection.cpp b/src/collection.cpp
index bcd5c119..1bab550d 100644
--- a/src/collection.cpp
+++ b/src/collection.cpp
@@ -101,7 +101,8 @@ Option<bool> single_value_filter_query(nlohmann::json& document, const std::stri
Option<bool> Collection::add_reference_helper_fields(nlohmann::json& document, const tsl::htrie_map<char, field>& schema,
const spp::sparse_hash_map<std::string, reference_pair>& reference_fields,
- tsl::htrie_set<char>& object_reference_helper_fields) {
+ tsl::htrie_set<char>& object_reference_helper_fields,
+ const bool& is_update) {
tsl::htrie_set<char> flat_fields;
if (!reference_fields.empty() && document.contains(".flat")) {
for (const auto &item: document[".flat"].get<std::vector<std::string>>()) {
@@ -113,7 +114,9 @@ Option<bool> Collection::add_reference_helper_fields(nlohmann::json& document, c
for (auto const& pair: reference_fields) {
auto field_name = pair.first;
auto optional = schema.at(field_name).optional;
- if (!optional && document.count(field_name) != 1) {
+ // Strict checking for presence of non-optional reference field during indexing operation.
+ auto is_required = !is_update && !optional;
+ if (is_required && document.count(field_name) != 1) {
return Option<bool>(400, "Missing the required reference field `" + field_name
+ "` in the document.");
} else if (document.count(field_name) != 1) {
@@ -5898,7 +5901,7 @@ Option<bool> Collection::detect_new_fields(nlohmann::json& document,
}
auto add_reference_helper_fields_op = add_reference_helper_fields(document, schema, reference_fields,
- object_reference_helper_fields);
+ object_reference_helper_fields, is_update);
if (!add_reference_helper_fields_op.ok()) {
return add_reference_helper_fields_op;
}
diff --git a/test/collection_join_test.cpp b/test/collection_join_test.cpp
index f05d99c2..d08179be 100644
--- a/test/collection_join_test.cpp
+++ b/test/collection_join_test.cpp
@@ -1039,6 +1039,118 @@ TEST_F(CollectionJoinTest, IndexDocumentHavingReferenceField) {
ASSERT_EQ("Cannot add a reference to `coll1.object_array_field` of type `object[]`.", add_doc_op.error());
}
+TEST_F(CollectionJoinTest, UpdateDocumentHavingReferenceField) {
+ auto schema_json =
+ R"({
+ "name": "Products",
+ "fields": [
+ {"name": "product_id", "type": "string"},
+ {"name": "product_name", "type": "string"},
+ {"name": "product_description", "type": "string"}
+ ]
+ })"_json;
+ std::vector<nlohmann::json> documents = {
+ R"({
+ "product_id": "product_a",
+ "product_name": "shampoo",
+ "product_description": "Our new moisturizing shampoo is perfect for those with dry or damaged hair."
+ })"_json,
+ R"({
+ "product_id": "product_b",
+ "product_name": "soap",
+ "product_description": "Introducing our all-natural, organic soap bar made with essential oils and botanical ingredients."
+ })"_json
+ };
+
+ auto collection_create_op = collectionManager.create_collection(schema_json);
+ ASSERT_TRUE(collection_create_op.ok());
+ for (auto const &json: documents) {
+ auto add_op = collection_create_op.get()->add(json.dump());
+ ASSERT_TRUE(add_op.ok());
+ }
+
+ schema_json =
+ R"({
+ "name": "Customers",
+ "fields": [
+ {"name": "customer_id", "type": "string"},
+ {"name": "customer_name", "type": "string", "sort": true},
+ {"name": "product_price", "type": "float"},
+ {"name": "product_id", "type": "string", "reference": "Products.product_id"}
+ ]
+ })"_json;
+ documents = {
+ R"({
+ "customer_id": "customer_a",
+ "customer_name": "Joe",
+ "product_price": 143,
+ "product_id": "product_a"
+ })"_json,
+ R"({
+ "customer_id": "customer_a",
+ "customer_name": "Joe",
+ "product_price": 73.5,
+ "product_id": "product_b"
+ })"_json,
+ R"({
+ "customer_id": "customer_b",
+ "customer_name": "Dan",
+ "product_price": 75,
+ "product_id": "product_a"
+ })"_json,
+ R"({
+ "customer_id": "customer_b",
+ "customer_name": "Dan",
+ "product_price": 140,
+ "product_id": "product_b"
+ })"_json
+ };
+ collection_create_op = collectionManager.create_collection(schema_json);
+ ASSERT_TRUE(collection_create_op.ok());
+ for (auto const &json: documents) {
+ auto add_op = collection_create_op.get()->add(json.dump());
+ ASSERT_TRUE(add_op.ok());
+ }
+
+ std::map<std::string, std::string> req_params = {
+ {"collection", "Customers"},
+ {"q", "*"},
+ {"filter_by", "id: 0"}
+ };
+ nlohmann::json embedded_params;
+ std::string json_res;
+ auto now_ts = std::chrono::duration_cast<std::chrono::microseconds>(
+ std::chrono::system_clock::now().time_since_epoch()).count();
+
+ auto search_op = collectionManager.do_search(req_params, embedded_params, json_res, now_ts);
+ ASSERT_TRUE(search_op.ok());
+
+ auto res_obj = nlohmann::json::parse(json_res);
+ ASSERT_EQ(1, res_obj["found"].get<size_t>());
+ ASSERT_EQ(1, res_obj["hits"].size());
+ ASSERT_EQ("product_a", res_obj["hits"][0]["document"].at("product_id"));
+ ASSERT_EQ(143, res_obj["hits"][0]["document"].at("product_price"));
+
+ auto coll = collection_create_op.get();
+ std::string dirty_values = "REJECT";
+ auto update_op = coll->update_matching_filter("id: 0", R"({"product_price": 0})", dirty_values);
+ ASSERT_TRUE(update_op.ok());
+
+ req_params = {
+ {"collection", "Customers"},
+ {"q", "*"},
+ {"filter_by", "id: 0"}
+ };
+ search_op = collectionManager.do_search(req_params, embedded_params, json_res, now_ts);
+ ASSERT_TRUE(search_op.ok());
+
+ res_obj = nlohmann::json::parse(json_res);
+ ASSERT_EQ(1, res_obj["found"].get<size_t>());
+ ASSERT_EQ(1, res_obj["hits"].size());
+ ASSERT_EQ("product_a", res_obj["hits"][0]["document"].at("product_id"));
+ ASSERT_EQ(0, res_obj["hits"][0]["document"].at("product_price"));
+}
+
TEST_F(CollectionJoinTest, FilterByReference_SingleMatch) {
auto schema_json =
R"({