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

Allow a field inside an object to have `reference`.

Harpreet Sangaraa9ff7d37e49
diff --git a/include/collection.h b/include/collection.h
index c6c4ee43..b769b4a1 100644
--- a/include/collection.h
+++ b/include/collection.h
@@ -147,6 +147,10 @@ private:
/// collection_name -> field_name
spp::sparse_hash_map<std::string, std::string> referenced_in;
+ /// Reference helper fields that are part of an object. The reference doc of these fields will be included in the
+ /// object rather than in the document.
+ tsl::htrie_set<char> object_reference_helper_fields;
+
// Keep index as the last field since it is initialized in the constructor via init_index(). Add a new field before it.
Index* index;
@@ -208,7 +212,9 @@ private:
const std::string& fallback_field_type,
bool is_update,
std::vector<field>& new_fields,
- bool enable_nested_fields);
+ bool enable_nested_fields,
+ const spp::sparse_hash_map<std::string, reference_pair>& reference_fields,
+ tsl::htrie_set<char>& object_reference_helper_fields);
static bool facet_count_compare(const facet_count_t& a, const facet_count_t& b) {
return std::tie(a.count, a.fhash) > std::tie(b.count, b.fhash);
@@ -397,9 +403,13 @@ public:
tsl::htrie_map<char, field> get_embedding_fields_unsafe();
+ tsl::htrie_set<char> get_object_reference_helper_fields();
+
std::string get_default_sorting_field();
- Option<bool> add_reference_helper_fields(nlohmann::json& document);
+ 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);
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 92a6e15b..04f67d00 100644
--- a/src/collection.cpp
+++ b/src/collection.cpp
@@ -72,11 +72,36 @@ uint32_t Collection::get_next_seq_id() {
return next_seq_id++;
}
-Option<bool> Collection::add_reference_helper_fields(nlohmann::json& document) {
+Option<bool> single_value_filter_query(nlohmann::json& document, const std::string& field_name,
+ const std::string& ref_field_type, std::string& filter_query) {
+ auto const& value = document[field_name];
+ if (value.is_string() && ref_field_type == field_types::STRING) {
+ filter_query[filter_query.size() - 1] = '=';
+ filter_query += (" " + value.get<std::string>());
+ } else if (value.is_number_integer() && (ref_field_type == field_types::INT64 ||
+ (ref_field_type == field_types::INT32 && StringUtils::is_int32_t(std::to_string(value.get<int64_t>()))))) {
+ filter_query += std::to_string(value.get<int64_t>());
+ } else {
+ return Option<bool>(400, "Field `" + field_name + "` must have `" + ref_field_type + "` value.");
+ }
+
+ return Option<bool>(true);
+}
+
+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> flat_fields;
+ if (!reference_fields.empty() && document.contains(".flat")) {
+ for (const auto &item: document[".flat"].get<std::vector<std::string>>()) {
+ flat_fields.insert(item);
+ }
+ }
+
// Add reference helper fields in the document.
for (auto const& pair: reference_fields) {
auto field_name = pair.first;
- auto optional = get_schema().at(field_name).optional;
+ auto optional = schema.at(field_name).optional;
if (!optional && document.count(field_name) != 1) {
return Option<bool>(400, "Missing the required reference field `" + field_name
+ "` in the document.");
@@ -159,6 +184,61 @@ Option<bool> Collection::add_reference_helper_fields(nlohmann::json& document) {
"` of type `" + ref_field.type + "`.");
}
+ bool is_object_reference_field = flat_fields.count(field_name) != 0;
+ std::string object_key;
+ bool is_object_array;
+ if (is_object_reference_field) {
+ object_reference_helper_fields.insert(reference_helper_field);
+
+ std::vector<std::string> tokens;
+ StringUtils::split(field_name, tokens, ".");
+ if (schema.count(tokens[0]) == 0) {
+ return Option<bool>(400, "Could not find `" + tokens[0] + "` object/object[] field in the schema.");
+ }
+ object_key = tokens[0];
+ is_object_array = schema.at(object_key).is_array();
+ }
+
+ if (is_object_reference_field && is_object_array) {
+ if (!document[field_name].is_array()) {
+ return Option<bool>(400, "Expected `" + field_name + "` to be an array.");
+ } else if (document[object_key].size() != document[field_name].size()) {
+ return Option<bool>(400, "Expected the same number of elements in `" + field_name + "` and `" +
+ object_key + "`.");
+ }
+
+ document[reference_helper_field] = nlohmann::json::array();
+ nlohmann::json temp_doc; // To store singular values of `field_name` field.
+
+ for (const auto &item: document[field_name]) {
+ temp_doc[field_name] = item;
+ auto single_value_filter_query_op = single_value_filter_query(temp_doc, field_name, ref_field_type,
+ filter_query);
+ if (!single_value_filter_query_op.ok()) {
+ return single_value_filter_query_op;
+ }
+
+ filter_result_t filter_result;
+ auto filter_ids_op = ref_collection->get_filter_ids(filter_query, filter_result);
+ if (!filter_ids_op.ok()) {
+ return filter_ids_op;
+ }
+
+ if (filter_result.count != 1) {
+ // Constraints similar to foreign key apply here. The reference match must be unique and not null.
+ return Option<bool>(400, filter_result.count < 1 ?
+ "Reference document having `" + filter_query + "` not found in the collection `"
+ + reference_collection_name + "`." :
+ "Multiple documents having `" + filter_query + "` found in the collection `" +
+ reference_collection_name + "`.");
+ }
+
+ document[reference_helper_field] += filter_result.docs[0];
+ filter_query = reference_field_name + ": ";
+ }
+ continue;
+ }
+
if (document[field_name].is_array()) {
if (ref_field_type == field_types::STRING) {
filter_query[filter_query.size() - 1] = '=';
@@ -180,15 +260,10 @@ Option<bool> Collection::add_reference_helper_fields(nlohmann::json& document) {
}
filter_query[filter_query.size() - 1] = ']';
} else {
- auto const& value = document[field_name];
- if (value.is_string() && ref_field_type == field_types::STRING) {
- filter_query[filter_query.size() - 1] = '=';
- filter_query += (" " + value.get<std::string>());
- } else if (value.is_number_integer() && (ref_field_type == field_types::INT64 ||
- (ref_field_type == field_types::INT32 && StringUtils::is_int32_t(std::to_string(value.get<int64_t>()))))) {
- filter_query += std::to_string(value.get<int64_t>());
- } else {
- return Option<bool>(400, "Field `" + field_name + "` must have `" + ref_field_type + "` value.");
+ auto single_value_filter_query_op = single_value_filter_query(document, field_name, ref_field_type,
+ filter_query);
+ if (!single_value_filter_query_op.ok()) {
+ return single_value_filter_query_op;
}
}
@@ -199,7 +274,7 @@ Option<bool> Collection::add_reference_helper_fields(nlohmann::json& document) {
}
if (document[field_name].is_array()) {
- document[field_name + fields::REFERENCE_HELPER_FIELD_SUFFIX] = nlohmann::json::array();
+ document[reference_helper_field] = nlohmann::json::array();
for (uint32_t i = 0; i < filter_result.count; i++) {
document[reference_helper_field] += filter_result.docs[i];
}
@@ -256,11 +331,6 @@ Option<doc_seq_id_t> Collection::to_doc(const std::string & json_str, nlohmann::
uint32_t seq_id = get_next_seq_id();
document["id"] = std::to_string(seq_id);
- auto add_reference_helper_fields_op = add_reference_helper_fields(document);
- if (!add_reference_helper_fields_op.ok()) {
- return Option<doc_seq_id_t>(add_reference_helper_fields_op.code(), add_reference_helper_fields_op.error());
- }
-
return Option<doc_seq_id_t>(doc_seq_id_t{seq_id, true});
} else {
if(!document["id"].is_string()) {
@@ -297,11 +367,6 @@ Option<doc_seq_id_t> Collection::to_doc(const std::string & json_str, nlohmann::
// for UPSERT, EMPLACE or CREATE, if a document with given ID is not found, we will treat it as a new doc
uint32_t seq_id = get_next_seq_id();
- auto add_reference_helper_fields_op = add_reference_helper_fields(document);
- if (!add_reference_helper_fields_op.ok()) {
- return Option<doc_seq_id_t>(add_reference_helper_fields_op.code(), add_reference_helper_fields_op.error());
- }
-
return Option<doc_seq_id_t>(doc_seq_id_t{seq_id, true});
}
}
@@ -448,7 +513,7 @@ nlohmann::json Collection::add_many(std::vector<std::string>& json_lines, nlohma
batch_doc_ids.insert(doc_id);
// if `fallback_field_type` or `dynamic_fields` is enabled, update schema first before indexing
- if(!fallback_field_type.empty() || !dynamic_fields.empty() || !nested_fields.empty()) {
+ if(!fallback_field_type.empty() || !dynamic_fields.empty() || !nested_fields.empty() || !reference_fields.empty()) {
std::vector<field> new_fields;
std::unique_lock lock(mutex);
@@ -458,7 +523,8 @@ nlohmann::json Collection::add_many(std::vector<std::string>& json_lines, nlohma
fallback_field_type,
record.is_update,
new_fields,
- enable_nested_fields);
+ enable_nested_fields,
+ reference_fields, object_reference_helper_fields);
if(!new_fields_op.ok()) {
record.index_failure(new_fields_op.code(), new_fields_op.error());
}
@@ -4269,6 +4335,11 @@ tsl::htrie_map<char, field> Collection::get_embedding_fields() {
return embedding_fields;
};
+tsl::htrie_set<char> Collection::get_object_reference_helper_fields() {
+ std::shared_lock lock(mutex);
+ return object_reference_helper_fields;
+}
+
std::string Collection::get_meta_key(const std::string & collection_name) {
return std::string(COLLECTION_META_PREFIX) + "_" + collection_name;
}
@@ -5007,14 +5078,42 @@ Option<bool> Collection::prune_doc(nlohmann::json& doc,
if (!get_references_op.ok()) {
continue;
}
- result.count = ids.size();
- result.docs = &ids[0];
- add_reference_fields_op = add_reference_fields(doc, ref_include.collection_name,
- ref_collection.get(), ref_include.alias, result,
- ref_include_fields_full, ref_exclude_fields_full, error_prefix,
- collection->search_schema.at(field_name).is_array(),
- ref_include.nest_ref_doc);
+ if (collection->object_reference_helper_fields.count(field_name) != 0) {
+ std::vector<std::string> keys;
+ StringUtils::split(field_name, keys, ".");
+ if (!doc.contains(keys[0])) {
+ return Option<bool>(400, "Could not find `" + keys[0] +
+ "` in the document to include the referenced document.");
+ }
+
+ result.count = ids.size();
+ result.docs = &ids[0];
+ if (doc[keys[0]].is_array()) {
+ for (uint32_t i = 0; i < result.count; i++) {
+ add_reference_fields_op = add_reference_fields(doc[keys[0]][i], ref_include.collection_name,
+ ref_collection.get(), ref_include.alias,
+ reference_filter_result_t(1, new uint32_t[1] {result.docs[i]}),
+ ref_include_fields_full, ref_exclude_fields_full, error_prefix,
+ false, ref_include.nest_ref_doc);
+ }
+ } else {
+ add_reference_fields_op = add_reference_fields(doc[keys[0]], ref_include.collection_name,
+ ref_collection.get(), ref_include.alias, result,
+ ref_include_fields_full, ref_exclude_fields_full, error_prefix,
+ collection->search_schema.at(field_name).is_array(),
+ ref_include.nest_ref_doc);
+ }
+ } else {
+ result.count = ids.size();
+ result.docs = &ids[0];
+ add_reference_fields_op = add_reference_fields(doc, ref_include.collection_name,
+ ref_collection.get(), ref_include.alias, result,
+ ref_include_fields_full, ref_exclude_fields_full, error_prefix,
+ collection->search_schema.at(field_name).is_array(),
+ ref_include.nest_ref_doc);
+ }
+
result.docs = nullptr;
} else if (joined_coll_has_reference) {
auto joined_collection = cm.get_collection(joined_coll_having_reference);
@@ -5301,7 +5400,8 @@ Option<bool> Collection::validate_alter_payload(nlohmann::json& schema_changes,
updated_nested_fields,
fallback_field_type, false,
new_fields,
- enable_nested_fields);
+ enable_nested_fields,
+ reference_fields, object_reference_helper_fields);
if(!new_fields_op.ok()) {
return new_fields_op;
}
@@ -5485,7 +5585,9 @@ Option<bool> Collection::detect_new_fields(nlohmann::json& document,
const std::string& fallback_field_type,
bool is_update,
std::vector<field>& new_fields,
- const bool enable_nested_fields) {
+ const bool enable_nested_fields,
+ const spp::sparse_hash_map<std::string, reference_pair>& reference_fields,
+ tsl::htrie_set<char>& object_reference_helper_fields) {
auto kv = document.begin();
while(kv != document.end()) {
@@ -5569,6 +5671,12 @@ 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);
+ if (!add_reference_helper_fields_op.ok()) {
+ return add_reference_helper_fields_op;
+ }
+
return Option<bool>(true);
}
diff --git a/src/field.cpp b/src/field.cpp
index 2ee6bc37..0054a003 100644
--- a/src/field.cpp
+++ b/src/field.cpp
@@ -314,6 +314,14 @@ Option<bool> field::json_field_to_field(bool enable_nested_fields, nlohmann::jso
if (tokens.size() < 2) {
return Option<bool>(400, "Invalid reference `" + field_json[fields::reference].get<std::string>() + "`.");
}
+
+ tokens.clear();
+ StringUtils::split(field_json[fields::name].get<std::string>(), tokens, ".");
+
+ if (tokens.size() > 2) {
+ return Option<bool>(400, "`" + field_json[fields::name].get<std::string>() + "` field cannot have a reference."
+ " Only the top-level field of an object is allowed.");
+ }
}
the_fields.emplace_back(
diff --git a/test/collection_join_test.cpp b/test/collection_join_test.cpp
index 9c8bf772..50779e48 100644
--- a/test/collection_join_test.cpp
+++ b/test/collection_join_test.cpp
@@ -88,6 +88,21 @@ TEST_F(CollectionJoinTest, SchemaReferenceField) {
ASSERT_FALSE(collection_create_op.ok());
ASSERT_EQ("Invalid reference `foo`.", collection_create_op.error());
+ schema_json =
+ R"({
+ "name": "Customers",
+ "fields": [
+ {"name": "Object.object.field", "type": "string", "reference": "Products.product_id"},
+ {"name": "customer_name", "type": "string"},
+ {"name": "product_price", "type": "float"}
+ ]
+ })"_json;
+
+ collection_create_op = collectionManager.create_collection(schema_json);
+ ASSERT_FALSE(collection_create_op.ok());
+ ASSERT_EQ("`Object.object.field` field cannot have a reference. Only the top-level field of an object is allowed.",
+ collection_create_op.error());
+
schema_json =
R"({
"name": "Customers",
@@ -442,7 +457,14 @@ TEST_F(CollectionJoinTest, IndexDocumentHavingReferenceField) {
{"name": "ref_geopoint_field", "type": "geopoint", "optional": true, "reference": "coll1.geopoint_field"},
{"name": "ref_geopoint_array_field", "type": "geopoint[]", "optional": true, "reference": "coll1.geopoint_array_field"},
{"name": "ref_object_field", "type": "object", "optional": true, "reference": "coll1.object_field"},
- {"name": "ref_object_array_field", "type": "object[]", "optional": true, "reference": "coll1.object_array_field"}
+ {"name": "ref_object_array_field", "type": "object[]", "optional": true, "reference": "coll1.object_array_field"},
+ {"name": "non_indexed_object.ref_field", "type": "string", "optional": true, "reference": "coll1.string_field"},
+ {"name": "object.ref_field", "type": "string", "optional": true, "reference": "coll1.string_field"},
+ {"name": "object.ref_array_field", "type": "string[]", "optional": true, "reference": "coll1.string_array_field"},
+ {"name": "object", "type": "object", "optional": true},
+ {"name": "object_array.ref_field", "type": "string", "optional": true, "reference": "coll1.string_field"},
+ {"name": "object_array.ref_array_field", "type": "string[]", "optional": true, "reference": "coll1.string_array_field"},
+ {"name": "object_array", "type": "object[]", "optional": true}
]
})"_json;
auto temp_json = schema_json;
@@ -498,7 +520,7 @@ TEST_F(CollectionJoinTest, IndexDocumentHavingReferenceField) {
ASSERT_EQ("ref_string_field_sequence_id", doc[".ref"][0]);
doc_json = R"({
- "ref_string_array_field": ["a", "d"]
+ "ref_string_array_field": ["foo"]
})"_json;
add_doc_op = coll2->add(doc_json.dump());
ASSERT_TRUE(add_doc_op.ok());
@@ -801,6 +823,151 @@ TEST_F(CollectionJoinTest, IndexDocumentHavingReferenceField) {
ASSERT_EQ(1, doc["ref_int64_array_field_sequence_id"].size());
ASSERT_EQ(6, doc["ref_int64_array_field_sequence_id"][0]);
+ // reference field inside object/object[]
+ doc_json = R"({
+ "non_indexed_object": {
+ "ref_field": "foo"
+ }
+ })"_json;
+ add_doc_op = coll2->add(doc_json.dump());
+ ASSERT_FALSE(add_doc_op.ok());
+ ASSERT_EQ("Could not find `non_indexed_object` object/object[] field in the schema.", add_doc_op.error());
+
+ doc_json = R"({
+ "object": {
+ "ref_field": 1
+ }
+ })"_json;
+ add_doc_op = coll2->add(doc_json.dump());
+ ASSERT_FALSE(add_doc_op.ok());
+ ASSERT_EQ("Field `object.ref_field` must have `string` value.", add_doc_op.error());
+
+ doc_json = R"({
+ "object": {
+ "ref_array_field": [1]
+ }
+ })"_json;
+ add_doc_op = coll2->add(doc_json.dump());
+ ASSERT_FALSE(add_doc_op.ok());
+ ASSERT_EQ("Field `object.ref_array_field` must only have `string` values.", add_doc_op.error());
+
+ doc_json = R"({
+ "object_array": [
+ {
+ "ref_field": 1
+ }
+ ]
+ })"_json;
+ add_doc_op = coll2->add(doc_json.dump());
+ ASSERT_FALSE(add_doc_op.ok());
+ ASSERT_EQ("Field `object_array.ref_field` must have `string` value.", add_doc_op.error());
+
+ doc_json = R"({
+ "object_array": [
+ {
+ "ref_field": "foo"
+ }
+ ]
+ })"_json;
+ add_doc_op = coll2->add(doc_json.dump());
+ ASSERT_FALSE(add_doc_op.ok());
+ ASSERT_EQ("Reference document having `string_field:= foo` not found in the collection `coll1`.", add_doc_op.error());
+
+ doc_json = R"({
+ "object_array": [
+ {
+ "ref_field": "a"
+ }
+ ]
+ })"_json;
+ add_doc_op = coll2->add(doc_json.dump());
+ ASSERT_FALSE(add_doc_op.ok());
+ ASSERT_EQ("Field `object_array.ref_field` has an incorrect type."
+ " Hint: field inside an array of objects must be an array type as well.", add_doc_op.error());
+
+ doc_json = R"({
+ "object_array": [
+ {
+ "ref_array_field": "foo"
+ }
+ ]
+ })"_json;
+ add_doc_op = coll2->add(doc_json.dump());
+ ASSERT_FALSE(add_doc_op.ok());
+ ASSERT_EQ("Reference document having `string_array_field:= foo` not found in the collection `coll1`.", add_doc_op.error());
+
+ collectionManager.drop_collection("coll2");
+ temp_json = schema_json;
+ collection_create_op = collectionManager.create_collection(temp_json);
+ ASSERT_TRUE(collection_create_op.ok());
+ coll2 = collection_create_op.get();
+
+ doc_json = R"({
+ "object": {
+ "ref_field": "d"
+ }
+ })"_json;
+ add_doc_op = coll2->add(doc_json.dump());
+ ASSERT_TRUE(add_doc_op.ok());
+
+ doc = coll2->get("0").get();
+ ASSERT_EQ(1, doc.count("object.ref_field_sequence_id"));
+ ASSERT_EQ(1, doc["object.ref_field_sequence_id"]);
+ ASSERT_EQ(1, doc.count(".ref"));
+ ASSERT_EQ(1, doc[".ref"].size());
+ ASSERT_EQ("object.ref_field_sequence_id", doc[".ref"][0]);
+ ASSERT_EQ(1, coll2->get_object_reference_helper_fields().count("object.ref_field_sequence_id"));
+
+ doc_json = R"({
+ "object": {
+ "ref_array_field": ["foo"]
+ }
+ })"_json;
+ add_doc_op = coll2->add(doc_json.dump());
+ ASSERT_TRUE(add_doc_op.ok());
+
+ doc = coll2->get("1").get();
+ ASSERT_EQ(1, doc.count("object.ref_array_field_sequence_id"));
+ ASSERT_EQ(0, doc["object.ref_array_field_sequence_id"].size());
+ ASSERT_EQ(1, doc.count(".ref"));
+ ASSERT_EQ(1, doc[".ref"].size());
+ ASSERT_EQ("object.ref_array_field_sequence_id", doc[".ref"][0]);
+
+ doc_json = R"({
+ "object": {
+ "ref_array_field": ["b", "foo"]
+ }
+ })"_json;
+ add_doc_op = coll2->add(doc_json.dump());
+ ASSERT_TRUE(add_doc_op.ok());
+
+ doc = coll2->get("2").get();
+ ASSERT_EQ(1, doc.count("object.ref_array_field_sequence_id"));
+ ASSERT_EQ(1, doc["object.ref_array_field_sequence_id"].size());
+ ASSERT_EQ(0, doc["object.ref_array_field_sequence_id"][0]);
+
+ doc_json = R"({
+ "object_array": [
+ {
+ "ref_array_field": "c"
+ },
+ {
+ "ref_array_field": "e"
+ }
+ ]
+ })"_json;
+ add_doc_op = coll2->add(doc_json.dump());
+ ASSERT_TRUE(add_doc_op.ok());
+
+ doc = coll2->get("3").get();
+ ASSERT_EQ(1, doc.count("object_array.ref_array_field_sequence_id"));
+ ASSERT_EQ(2, doc["object_array.ref_array_field_sequence_id"].size());
+ ASSERT_EQ(0, doc["object_array.ref_array_field_sequence_id"][0]);
+ ASSERT_EQ(1, doc["object_array.ref_array_field_sequence_id"][1]);
+ ASSERT_EQ(1, doc.count(".ref"));
+ ASSERT_EQ(1, doc[".ref"].size());
+ ASSERT_EQ("object_array.ref_array_field_sequence_id", doc[".ref"][0]);
+
// float/float[] reference fields
doc_json = R"({
"ref_float_field": 1.5
@@ -2594,6 +2761,132 @@ TEST_F(CollectionJoinTest, FilterByReferenceArrayField) {
ASSERT_EQ("Corduroy", res_obj["hits"][2]["document"]["song.title"][0]);
}
+TEST_F(CollectionJoinTest, FilterByObjectReferenceField) {
+ auto schema_json =
+ R"({
+ "name": "Portions",
+ "fields": [
+ {"name": "portion_id", "type": "string"},
+ {"name": "quantity", "type": "int32"},
+ {"name": "unit", "type": "string"}
+ ]
+ })"_json;
+ std::vector<nlohmann::json> documents = {
+ R"({
+ "portion_id": "portion_a",
+ "quantity": 500,
+ "unit": "g"
+ })"_json,
+ R"({
+ "portion_id": "portion_b",
+ "quantity": 1,
+ "unit": "lt"
+ })"_json,
+ R"({
+ "portion_id": "portion_c",
+ "quantity": 500,
+ "unit": "ml"
+ })"_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());
+ if (!add_op.ok()) {
+ LOG(INFO) << add_op.error();
+ }
+ ASSERT_TRUE(add_op.ok());
+ }
+
+ schema_json =
+ R"({
+ "name": "Foods",
+ "fields": [
+ {"name": "name", "type": "string"},
+ {"name": "portions", "type": "object[]"},
+ {"name": "portions.portion_id", "type": "string[]", "reference": "Portions.portion_id"}
+ ],
+ "enable_nested_fields": true
+ })"_json;
+ documents = {
+ R"({
+ "name": "Bread",
+ "portions": [
+ {
+ "portion_id": "portion_a",
+ "count": 10
+ }
+ ]
+ })"_json,
+ R"({
+ "name": "Milk",
+ "portions": [
+ {
+ "portion_id": "portion_b",
+ "count": 3
+ },
+ {
+ "portion_id": "portion_c",
+ "count": 1
+ }
+ ]
+ })"_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());
+ if (!add_op.ok()) {
+ LOG(INFO) << add_op.error();
+ }
+ ASSERT_TRUE(add_op.ok());
+ }
+
+ std::map<std::string, std::string> req_params = {
+ {"collection", "Foods"},
+ {"q", "*"},
+ {"include_fields", "$Portions(*:merge)"}
+ };
+ 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_bool = collectionManager.do_search(req_params, embedded_params, json_res, now_ts);
+ ASSERT_TRUE(search_op_bool.ok());
+
+ auto res_obj = nlohmann::json::parse(json_res);
+ ASSERT_EQ(2, res_obj["found"].get<size_t>());
+ ASSERT_EQ(2, res_obj["hits"].size());
+ ASSERT_EQ(3, res_obj["hits"][0]["document"].size());
+ ASSERT_EQ(1, res_obj["hits"][0]["document"].count("name"));
+
+ ASSERT_EQ("Milk", res_obj["hits"][0]["document"]["name"]);
+ ASSERT_EQ(1, res_obj["hits"][0]["document"].count("portions"));
+ ASSERT_EQ(2, res_obj["hits"][0]["document"]["portions"].size());
+ ASSERT_EQ(5, res_obj["hits"][0]["document"]["portions"][0].size());
+ ASSERT_EQ("portion_b", res_obj["hits"][0]["document"]["portions"][0].at("portion_id"));
+ ASSERT_EQ(1 , res_obj["hits"][0]["document"]["portions"][0].at("quantity"));
+ ASSERT_EQ("lt", res_obj["hits"][0]["document"]["portions"][0].at("unit"));
+ ASSERT_EQ(3 , res_obj["hits"][0]["document"]["portions"][0].at("count"));
+ ASSERT_EQ(5, res_obj["hits"][0]["document"]["portions"][1].size());
+ ASSERT_EQ("portion_c", res_obj["hits"][0]["document"]["portions"][1].at("portion_id"));
+ ASSERT_EQ(500 , res_obj["hits"][0]["document"]["portions"][1].at("quantity"));
+ ASSERT_EQ("ml", res_obj["hits"][0]["document"]["portions"][1].at("unit"));
+ ASSERT_EQ(1 , res_obj["hits"][0]["document"]["portions"][1].at("count"));
+
+ ASSERT_EQ("Bread", res_obj["hits"][1]["document"]["name"]);
+ ASSERT_EQ(1, res_obj["hits"][1]["document"].count("portions"));
+ ASSERT_EQ(1, res_obj["hits"][1]["document"]["portions"].size());
+ ASSERT_EQ(5, res_obj["hits"][1]["document"]["portions"][0].size());
+ ASSERT_EQ("portion_a", res_obj["hits"][1]["document"]["portions"][0].at("portion_id"));
+ ASSERT_EQ(500 , res_obj["hits"][1]["document"]["portions"][0].at("quantity"));
+ ASSERT_EQ("g", res_obj["hits"][1]["document"]["portions"][0].at("unit"));
+ ASSERT_EQ(10 , res_obj["hits"][1]["document"]["portions"][0].at("count"));
+}
+
TEST_F(CollectionJoinTest, CascadeDeletion) {
auto schema_json =
R"({