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

Return error on name collision.

Harpreet Sangare0dc11f57a9b
diff --git a/src/collection.cpp b/src/collection.cpp
index ed423356..1090f39f 100644
--- a/src/collection.cpp
+++ b/src/collection.cpp
@@ -4499,19 +4499,27 @@ Option<bool> Collection::add_reference_fields(nlohmann::json& doc,
}
if (nest_ref_doc && !ref_doc.empty()) {
- auto field_name = alias.empty() ? ref_collection_name : alias;
- doc[field_name] += ref_doc;
- } else {
- if (!alias.empty()) {
- auto temp_doc = ref_doc;
- ref_doc.clear();
- for (const auto &item: temp_doc.items()) {
- ref_doc[alias + item.key()] = item.value();
- }
+ auto key = alias.empty() ? ref_collection_name : alias;
+ if (doc.contains(key) && !doc[key].is_array()) {
+ return Option<bool>(400, "Could not include the reference document of `" + ref_collection_name +
+ "` collection. Expected `" + key + "` to be an array. Try " +
+ (alias.empty() ? "adding an" : "renaming the") + " alias.");
}
+
+ doc[key] += ref_doc;
+ } else {
for (auto ref_doc_it = ref_doc.begin(); ref_doc_it != ref_doc.end(); ref_doc_it++) {
+ auto const& ref_doc_key = ref_doc_it.key();
+ auto const& doc_key = alias + ref_doc_key;
+ if (doc.contains(doc_key) && !doc[doc_key].is_array()) {
+ return Option<bool>(400, "Could not include the value of `" + ref_doc_key +
+ "` key of the reference document of `" + ref_collection_name +
+ "` collection. Expected `" + doc_key + "` to be an array. Try " +
+ (alias.empty() ? "adding an" : "renaming the") + " alias.");
+ }
+
// Add the values of ref_doc as JSON array into doc.
- doc[ref_doc_it.key()] += ref_doc_it.value();
+ doc[doc_key] += ref_doc_it.value();
}
}
}
diff --git a/test/collection_join_test.cpp b/test/collection_join_test.cpp
index 77d35afa..daee7be7 100644
--- a/test/collection_join_test.cpp
+++ b/test/collection_join_test.cpp
@@ -2078,6 +2078,30 @@ TEST_F(CollectionJoinTest, IncludeExcludeFieldsByReference) {
ASSERT_EQ(73.5, res_obj["hits"][0]["document"].at("product_price"));
// Add alias using `as`
+ req_params = {
+ {"collection", "Products"},
+ {"q", "soap"},
+ {"query_by", "product_name"},
+ {"filter_by", "$Customers(id:*)"},
+ {"include_fields", "id, $Customers(id :merge)"}
+ };
+ search_op = collectionManager.do_search(req_params, embedded_params, json_res, now_ts);
+ ASSERT_FALSE(search_op.ok());
+ ASSERT_EQ("Could not include the value of `id` key of the reference document of `Customers` collection."
+ " Expected `id` to be an array. Try adding an alias.", search_op.error());
+
+ req_params = {
+ {"collection", "Products"},
+ {"q", "soap"},
+ {"query_by", "product_name"},
+ {"filter_by", "$Customers(id:*)"},
+ {"include_fields", "id, $Customers(id :nest) as id"}
+ };
+ search_op = collectionManager.do_search(req_params, embedded_params, json_res, now_ts);
+ ASSERT_FALSE(search_op.ok());
+ ASSERT_EQ("Could not include the reference document of `Customers` collection."
+ " Expected `id` to be an array. Try renaming the alias.", search_op.error());
+
req_params = {
{"collection", "Customers"},
{"q", "Joe"},