mirror of
https://github.com/typesense/typesense.git
synced 2025-05-22 14:55:26 +08:00
Return error on name collision.
This commit is contained in:
parent
ce6a5c2f3f
commit
e0dc11f57a
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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"},
|
||||
|
Loading…
x
Reference in New Issue
Block a user