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

Handle references for `string`, `string[]`, `int32`, `int32[]`, `int64` and `int64[]` field types.

Harpreet Sangar8878acc0f9bf
diff --git a/include/collection.h b/include/collection.h
index aab3ce31..a34f6a46 100644
--- a/include/collection.h
+++ b/include/collection.h
@@ -351,6 +351,8 @@ public:
uint32_t get_next_seq_id();
+ Option<uint32_t> doc_id_to_seq_id_with_lock(const std::string & doc_id) const;
+
Option<uint32_t> doc_id_to_seq_id(const std::string & doc_id) const;
std::vector<std::string> get_facet_fields();
diff --git a/include/field.h b/include/field.h
index 6407ee5f..c92b1ff4 100644
--- a/include/field.h
+++ b/include/field.h
@@ -35,6 +35,10 @@ namespace field_types {
static bool is_string_or_array(const std::string& type_def) {
return type_def == "string*";
}
+
+ static bool is_array(const std::string& type_def) {
+ return type_def.size() > 2 && type_def[type_def.size() - 2] == '[' && type_def[type_def.size() - 1] == ']';
+ }
}
namespace fields {
diff --git a/src/collection.cpp b/src/collection.cpp
index bdb56b5c..af4ae784 100644
--- a/src/collection.cpp
+++ b/src/collection.cpp
@@ -89,15 +89,37 @@ Option<bool> Collection::add_reference_helper_fields(nlohmann::json& document) {
}
if (reference_field_name == "id") {
- auto value = document[field_name].get<std::string>();
- auto ref_doc_id_op = ref_collection->doc_id_to_seq_id(value);
- if (!ref_doc_id_op.ok()) {
- return Option<bool>(400, "Referenced document having `id: " + value +
+ auto id_field_type_error_op = Option<bool>(400, "Field `" + field_name + "` must have string value.");
+ if (document[field_name].is_array()) {
+ for (const auto &item: document[field_name].items()) {
+ if (!item.value().is_string()) {
+ return id_field_type_error_op;
+ }
+
+ auto id = item.value().get<std::string>();
+ auto ref_doc_id_op = ref_collection->doc_id_to_seq_id_with_lock(id);
+ if (!ref_doc_id_op.ok()) {
+ return Option<bool>(400, "Referenced document having `id: " + id +
"` not found in the collection `" +
reference_collection_name + "`." );
+ }
+
+ document[field_name + REFERENCE_HELPER_FIELD_SUFFIX] += ref_doc_id_op.get();
+ }
+ } else if (document[field_name].is_string()) {
+ auto id = document[field_name].get<std::string>();
+ auto ref_doc_id_op = ref_collection->doc_id_to_seq_id_with_lock(id);
+ if (!ref_doc_id_op.ok()) {
+ return Option<bool>(400, "Referenced document having `id: " + id +
+ "` not found in the collection `" +
+ reference_collection_name + "`." );
+ }
+
+ document[field_name + REFERENCE_HELPER_FIELD_SUFFIX] = ref_doc_id_op.get();
+ } else {
+ return id_field_type_error_op;
}
- document[field_name + REFERENCE_HELPER_FIELD_SUFFIX] = ref_doc_id_op.get();
continue;
}
@@ -106,28 +128,83 @@ Option<bool> Collection::add_reference_helper_fields(nlohmann::json& document) {
"` not found in the collection `" + reference_collection_name + "`.");
}
- if (!ref_collection->get_schema().at(reference_field_name).index) {
+ auto const ref_field = ref_collection->get_schema().at(reference_field_name);
+ if (!ref_field.index) {
return Option<bool>(400, "Referenced field `" + reference_field_name +
"` in the collection `" + reference_collection_name + "` must be indexed.");
}
- // Get the doc id of the referenced document.
- auto value = document[field_name].get<std::string>();
- filter_result_t filter_result;
- ref_collection->get_filter_ids(reference_field_name + ":=" + value, filter_result);
+ // Create filter query from the value(s) in the reference field and get the reference doc id(s).
+ std::string filter_query = reference_field_name + ": ";
+ std::string ref_field_type = ref_field.is_string() ? field_types::STRING :
+ ref_field.is_int32() ? field_types::INT32 :
+ ref_field.is_int64() ? field_types::INT64 : field_types::NIL;
- if (filter_result.count != 1) {
- auto match = " `" + reference_field_name + ": " + value + "` ";
+ if (ref_field_type == field_types::NIL) {
+ return Option<bool>(400, "Cannot add a reference to `" + reference_collection_name + "." + reference_field_name +
+ "` of type `" + ref_field.type + "`.");
+ }
- // Constraints similar to foreign key apply here. The reference match must be unique and not null.
- return Option<bool>(400, filter_result.count < 1 ?
- "Referenced document having" + match + "not found in the collection `"
- + reference_collection_name + "`." :
- "Multiple documents having" + match + "found in the collection `" +
- reference_collection_name + "`.");
+ if (document[field_name].is_array()) {
+ if (ref_field_type == field_types::STRING) {
+ filter_query[filter_query.size() - 1] = '=';
+ filter_query += " [";
+ } else {
+ filter_query += "[";
+ }
+ for (const auto &item: document[field_name].items()) {
+ auto const& item_value = item.value();
+ if (item_value.is_string() && ref_field_type == field_types::STRING) {
+ filter_query += item_value.get<std::string>();
+ } else if (item_value.is_number_integer() && (ref_field_type == field_types::INT64 ||
+ (ref_field_type == field_types::INT32 && StringUtils::is_int32_t(std::to_string(item_value.get<int64_t>()))))) {
+ filter_query += std::to_string(item_value.get<int64_t>());
+ } else {
+ return Option<bool>(400, "Field `" + field_name + "` must only have `" + ref_field_type + "` values.");
+ }
+ filter_query += ",";
+ }
+ 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.");
+ }
+ }
+
+ 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;
}
- document[field_name + REFERENCE_HELPER_FIELD_SUFFIX] = filter_result.docs[0];
+ if (document[field_name].is_array()) {
+ if (filter_result.count == 0) {
+ return Option<bool>(400, "No reference document matching `" + filter_query + "` found in the collection `"
+ + reference_collection_name + "`.");
+ }
+
+ for (uint32_t i = 0; i < filter_result.count; i++) {
+ document[field_name + REFERENCE_HELPER_FIELD_SUFFIX] += filter_result.docs[i];
+ }
+ } else {
+ 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[field_name + REFERENCE_HELPER_FIELD_SUFFIX] = filter_result.docs[0];
+ }
}
return Option<bool>(true);
@@ -3922,6 +3999,11 @@ uint32_t Collection::get_collection_id() const {
return collection_id.load();
}
+Option<uint32_t> Collection::doc_id_to_seq_id_with_lock(const std::string & doc_id) const {
+ std::shared_lock lock(mutex);
+ return doc_id_to_seq_id(doc_id);
+}
+
Option<uint32_t> Collection::doc_id_to_seq_id(const std::string & doc_id) const {
std::string seq_id_str;
StoreStatus status = store->get(get_doc_id_key(doc_id), seq_id_str);
diff --git a/src/field.cpp b/src/field.cpp
index 7beef41c..894fbb4e 100644
--- a/src/field.cpp
+++ b/src/field.cpp
@@ -329,7 +329,8 @@ Option<bool> field::json_field_to_field(bool enable_nested_fields, nlohmann::jso
// computation while searching.
the_fields.emplace_back(
field(field_json[fields::name].get<std::string>() + Collection::REFERENCE_HELPER_FIELD_SUFFIX,
- "int64", false, field_json[fields::optional], true)
+ field_types::is_array(field_json[fields::type].get<std::string>()) ? field_types::INT64_ARRAY : field_types::INT64,
+ false, field_json[fields::optional], true)
);
}
diff --git a/test/collection_join_test.cpp b/test/collection_join_test.cpp
index 0494e2c6..2007d8f8 100644
--- a/test/collection_join_test.cpp
+++ b/test/collection_join_test.cpp
@@ -218,7 +218,7 @@ TEST_F(CollectionJoinTest, IndexDocumentHavingReferenceField) {
ASSERT_TRUE(collection_create_op.ok());
add_doc_op = customer_collection->add(customer_json.dump());
- ASSERT_EQ("Referenced document having `product_id: a` not found in the collection `Products`.", add_doc_op.error());
+ ASSERT_EQ("Reference document having `product_id:= a` not found in the collection `Products`.", add_doc_op.error());
std::vector<nlohmann::json> products = {
R"({
@@ -242,7 +242,7 @@ TEST_F(CollectionJoinTest, IndexDocumentHavingReferenceField) {
customer_json["reference_id"] = "product_a";
add_doc_op = customer_collection->add(customer_json.dump());
- ASSERT_EQ("Multiple documents having `product_id: product_a` found in the collection `Products`.", add_doc_op.error());
+ ASSERT_EQ("Multiple documents having `product_id:= product_a` found in the collection `Products`.", add_doc_op.error());
collectionManager.drop_collection("Products");
products[1]["product_id"] = "product_b";
@@ -282,7 +282,7 @@ TEST_F(CollectionJoinTest, IndexDocumentHavingReferenceField) {
customer_collection = collection_create_op.get();
add_doc_op = customer_collection->add(customer_json.dump());
ASSERT_TRUE(add_doc_op.ok());
- ASSERT_EQ(customer_collection->get("0").get().count("reference_id_sequence_id"), 1);
+ ASSERT_EQ(0, customer_collection->get("0").get().at("reference_id_sequence_id"));
nlohmann::json document;
// Referenced document's sequence_id must be valid.
@@ -298,8 +298,8 @@ TEST_F(CollectionJoinTest, IndexDocumentHavingReferenceField) {
R"({
"name": "id_ref",
"fields": [
- {"name": "id", "type": "string"},
- {"name": "reference", "type": "string", "reference": "Products.id"}
+ {"name": "id_reference", "type": "string", "reference": "Products.id", "optional": true},
+ {"name": "multi_id_reference", "type": "string[]", "reference": "Products.id", "optional": true}
]
})"_json;
collection_create_op = collectionManager.create_collection(id_ref_schema_json);
@@ -307,23 +307,460 @@ TEST_F(CollectionJoinTest, IndexDocumentHavingReferenceField) {
auto id_ref_collection = collection_create_op.get();
auto id_ref_json = R"({
- "id": "0",
- "reference": "foo"
+ "id_reference": 123
})"_json;
add_doc_op = id_ref_collection->add(id_ref_json.dump());
ASSERT_FALSE(add_doc_op.ok());
+ ASSERT_EQ("Field `id_reference` must have string value.", add_doc_op.error());
+
+ id_ref_json = R"({
+ "id_reference": "foo"
+ })"_json;
+ add_doc_op = id_ref_collection->add(id_ref_json.dump());
+ ASSERT_FALSE(add_doc_op.ok());
ASSERT_EQ("Referenced document having `id: foo` not found in the collection `Products`.", add_doc_op.error());
id_ref_json = R"({
- "id": "0",
- "reference": "1"
+ "id_reference": "0"
})"_json;
add_doc_op = id_ref_collection->add(id_ref_json.dump());
ASSERT_TRUE(add_doc_op.ok());
+ id_ref_json = R"({
+ "multi_id_reference": ["0", 1]
+ })"_json;
+ add_doc_op = id_ref_collection->add(id_ref_json.dump());
+ ASSERT_FALSE(add_doc_op.ok());
+ ASSERT_EQ("Field `multi_id_reference` must have string value.", add_doc_op.error());
+
+ id_ref_json = R"({
+ "multi_id_reference": ["0", "foo"]
+ })"_json;
+ add_doc_op = id_ref_collection->add(id_ref_json.dump());
+ ASSERT_FALSE(add_doc_op.ok());
+ ASSERT_EQ("Referenced document having `id: foo` not found in the collection `Products`.", add_doc_op.error());
+
+ id_ref_json = R"({
+ "multi_id_reference": ["1"]
+ })"_json;
+ add_doc_op = id_ref_collection->add(id_ref_json.dump());
+ ASSERT_TRUE(add_doc_op.ok());
+
+ id_ref_json = R"({
+ "multi_id_reference": ["0", "1"]
+ })"_json;
+ add_doc_op = id_ref_collection->add(id_ref_json.dump());
+ ASSERT_TRUE(add_doc_op.ok());
+
+ auto result = id_ref_collection->search("*", {}, "", {}, {}, {0}).get();
+ ASSERT_EQ(3, result["found"].get<size_t>());
+ ASSERT_EQ(3, result["hits"].size());
+ ASSERT_EQ(2, result["hits"][0]["document"]["multi_id_reference_sequence_id"].size());
+ ASSERT_EQ(1, result["hits"][1]["document"]["multi_id_reference_sequence_id"].size());
+ ASSERT_EQ(0, result["hits"][2]["document"]["id_reference_sequence_id"]);
+
collectionManager.drop_collection("Customers");
collectionManager.drop_collection("Products");
collectionManager.drop_collection("id_ref");
+
+ auto schema_json =
+ R"({
+ "name": "coll1",
+ "enable_nested_fields": true,
+ "fields": [
+ {"name": "string_field", "type": "string", "optional": true},
+ {"name": "string_array_field", "type": "string[]", "optional": true},
+ {"name": "int32_field", "type": "int32", "optional": true},
+ {"name": "int32_array_field", "type": "int32[]", "optional": true},
+ {"name": "int64_field", "type": "int64", "optional": true},
+ {"name": "int64_array_field", "type": "int64[]", "optional": true},
+ {"name": "float_field", "type": "float", "optional": true},
+ {"name": "float_array_field", "type": "float[]", "optional": true},
+ {"name": "bool_field", "type": "bool", "optional": true},
+ {"name": "bool_array_field", "type": "bool[]", "optional": true},
+ {"name": "geopoint_field", "type": "geopoint", "optional": true},
+ {"name": "geopoint_array_field", "type": "geopoint[]", "optional": true},
+ {"name": "object_field", "type": "object", "optional": true},
+ {"name": "object_array_field", "type": "object[]", "optional": true}
+ ]
+ })"_json;
+ collection_create_op = collectionManager.create_collection(schema_json);
+ ASSERT_TRUE(collection_create_op.ok());
+ auto coll1 = collection_create_op.get();
+
+ schema_json =
+ R"({
+ "name": "coll2",
+ "enable_nested_fields": true,
+ "fields": [
+ {"name": "ref_string_field", "type": "string", "optional": true, "reference": "coll1.string_field"},
+ {"name": "ref_string_array_field", "type": "string[]", "optional": true, "reference": "coll1.string_array_field"},
+ {"name": "ref_int32_field", "type": "int32", "optional": true, "reference": "coll1.int32_field"},
+ {"name": "ref_int32_array_field", "type": "int32[]", "optional": true, "reference": "coll1.int32_array_field"},
+ {"name": "ref_int64_field", "type": "int64", "optional": true, "reference": "coll1.int64_field"},
+ {"name": "ref_int64_array_field", "type": "int64[]", "optional": true, "reference": "coll1.int64_array_field"},
+ {"name": "ref_float_field", "type": "float", "optional": true, "reference": "coll1.float_field"},
+ {"name": "ref_float_array_field", "type": "float[]", "optional": true, "reference": "coll1.float_array_field"},
+ {"name": "ref_bool_field", "type": "bool", "optional": true, "reference": "coll1.bool_field"},
+ {"name": "ref_bool_array_field", "type": "bool[]", "optional": true, "reference": "coll1.bool_array_field"},
+ {"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"}
+ ]
+ })"_json;
+ collection_create_op = collectionManager.create_collection(schema_json);
+ ASSERT_TRUE(collection_create_op.ok());
+ auto coll2 = collection_create_op.get();
+
+ // string/string[] reference fields
+ auto doc_json = R"({
+ "string_field": "a",
+ "string_array_field": ["b", "c"]
+ })"_json;
+ add_doc_op = coll1->add(doc_json.dump());
+ ASSERT_TRUE(add_doc_op.ok());
+ doc_json = R"({
+ "string_field": "d",
+ "string_array_field": ["e", "f"]
+ })"_json;
+ add_doc_op = coll1->add(doc_json.dump());
+ ASSERT_TRUE(add_doc_op.ok());
+
+ doc_json = R"({
+ "ref_string_field": 1
+ })"_json;
+ add_doc_op = coll2->add(doc_json.dump());
+ ASSERT_FALSE(add_doc_op.ok());
+ ASSERT_EQ("Field `ref_string_field` must have `string` value.", add_doc_op.error());
+
+ doc_json = R"({
+ "ref_string_array_field": ["a", 1]
+ })"_json;
+ add_doc_op = coll2->add(doc_json.dump());
+ ASSERT_FALSE(add_doc_op.ok());
+ ASSERT_EQ("Field `ref_string_array_field` must only have `string` values.", add_doc_op.error());
+
+ doc_json = R"({
+ "ref_string_array_field": ["a", "d"]
+ })"_json;
+ add_doc_op = coll2->add(doc_json.dump());
+ ASSERT_FALSE(add_doc_op.ok());
+ ASSERT_EQ("No reference document matching `string_array_field:= [a,d]` found in the collection `coll1`.", add_doc_op.error());
+
+ doc_json = R"({
+ "ref_string_array_field": ["b", "foo"]
+ })"_json;
+ add_doc_op = coll2->add(doc_json.dump());
+ ASSERT_TRUE(add_doc_op.ok());
+
+ result = coll2->search("*", {}, "", {}, {}, {0}).get();
+ ASSERT_EQ(1, result["hits"][0]["document"]["ref_string_array_field_sequence_id"].size());
+ ASSERT_EQ(0, result["hits"][0]["document"]["ref_string_array_field_sequence_id"][0]);
+
+ doc_json = R"({
+ "ref_string_array_field": ["c", "e"]
+ })"_json;
+ add_doc_op = coll2->add(doc_json.dump());
+ ASSERT_TRUE(add_doc_op.ok());
+
+ result = coll2->search("*", {}, "", {}, {}, {0}).get();
+ ASSERT_EQ(2, result["hits"][0]["document"]["ref_string_array_field_sequence_id"].size());
+ ASSERT_EQ(0, result["hits"][0]["document"]["ref_string_array_field_sequence_id"][0]);
+ ASSERT_EQ(1, result["hits"][0]["document"]["ref_string_array_field_sequence_id"][1]);
+
+ // int32/int32[] reference fields
+ doc_json = R"({
+ "int32_field": 1
+ })"_json;
+ add_doc_op = coll1->add(doc_json.dump());
+ ASSERT_TRUE(add_doc_op.ok());
+
+ doc_json = R"({
+ "int32_field": 1,
+ "int32_array_field": [2, -2147483648]
+ })"_json;
+ add_doc_op = coll1->add(doc_json.dump());
+ ASSERT_TRUE(add_doc_op.ok());
+
+ doc_json = R"({
+ "int32_field": 4,
+ "int32_array_field": [5, 2147483647]
+ })"_json;
+ add_doc_op = coll1->add(doc_json.dump());
+ ASSERT_TRUE(add_doc_op.ok());
+
+ doc_json = R"({
+ "ref_int32_field": "1"
+ })"_json;
+ add_doc_op = coll2->add(doc_json.dump());
+ ASSERT_FALSE(add_doc_op.ok());
+ ASSERT_EQ("Field `ref_int32_field` must have `int32` value.", add_doc_op.error());
+
+ doc_json = R"({
+ "ref_int32_field": 2147483648
+ })"_json;
+ add_doc_op = coll2->add(doc_json.dump());
+ ASSERT_FALSE(add_doc_op.ok());
+ ASSERT_EQ("Field `ref_int32_field` must have `int32` value.", add_doc_op.error());
+
+ doc_json = R"({
+ "ref_int32_field": 0
+ })"_json;
+ add_doc_op = coll2->add(doc_json.dump());
+ ASSERT_FALSE(add_doc_op.ok());
+ ASSERT_EQ("Reference document having `int32_field: 0` not found in the collection `coll1`.", add_doc_op.error());
+
+ doc_json = R"({
+ "ref_int32_field": 1
+ })"_json;
+ add_doc_op = coll2->add(doc_json.dump());
+ ASSERT_FALSE(add_doc_op.ok());
+ ASSERT_EQ("Multiple documents having `int32_field: 1` found in the collection `coll1`.", add_doc_op.error());
+
+ doc_json = R"({
+ "ref_int32_field": 4
+ })"_json;
+ add_doc_op = coll2->add(doc_json.dump());
+ ASSERT_TRUE(add_doc_op.ok());
+
+ result = coll2->search("*", {}, "", {}, {}, {0}).get();
+ ASSERT_EQ(4, result["hits"][0]["document"]["ref_int32_field_sequence_id"]);
+
+ doc_json = R"({
+ "ref_int32_array_field": [1, "2"]
+ })"_json;
+ add_doc_op = coll2->add(doc_json.dump());
+ ASSERT_FALSE(add_doc_op.ok());
+ ASSERT_EQ("Field `ref_int32_array_field` must only have `int32` values.", add_doc_op.error());
+
+ doc_json = R"({
+ "ref_int32_array_field": [1]
+ })"_json;
+ add_doc_op = coll2->add(doc_json.dump());
+ ASSERT_FALSE(add_doc_op.ok());
+ ASSERT_EQ("No reference document matching `int32_array_field: [1]` found in the collection `coll1`.", add_doc_op.error());
+
+ doc_json = R"({
+ "ref_int32_array_field": [1, -2147483649]
+ })"_json;
+ add_doc_op = coll2->add(doc_json.dump());
+ ASSERT_FALSE(add_doc_op.ok());
+ ASSERT_EQ("Field `ref_int32_array_field` must only have `int32` values.", add_doc_op.error());
+
+ doc_json = R"({
+ "ref_int32_array_field": [1, 2147483648]
+ })"_json;
+ add_doc_op = coll2->add(doc_json.dump());
+ ASSERT_FALSE(add_doc_op.ok());
+ ASSERT_EQ("Field `ref_int32_array_field` must only have `int32` values.", add_doc_op.error());
+
+ doc_json = R"({
+ "ref_int32_array_field": [1, 2]
+ })"_json;
+ add_doc_op = coll2->add(doc_json.dump());
+ ASSERT_TRUE(add_doc_op.ok());
+
+ result = coll2->search("*", {}, "", {}, {}, {0}).get();
+ ASSERT_EQ(1, result["hits"][0]["document"]["ref_int32_array_field_sequence_id"].size());
+ ASSERT_EQ(3, result["hits"][0]["document"]["ref_int32_array_field_sequence_id"][0]);
+
+ doc_json = R"({
+ "ref_int32_array_field": [2, 5]
+ })"_json;
+ add_doc_op = coll2->add(doc_json.dump());
+ ASSERT_TRUE(add_doc_op.ok());
+
+ result = coll2->search("*", {}, "", {}, {}, {0}).get();
+ ASSERT_EQ(2, result["hits"][0]["document"]["ref_int32_array_field_sequence_id"].size());
+ ASSERT_EQ(3, result["hits"][0]["document"]["ref_int32_array_field_sequence_id"][0]);
+ ASSERT_EQ(4, result["hits"][0]["document"]["ref_int32_array_field_sequence_id"][1]);
+
+ doc_json = R"({
+ "ref_int32_array_field": [-2147483648]
+ })"_json;
+ add_doc_op = coll2->add(doc_json.dump());
+ ASSERT_TRUE(add_doc_op.ok());
+
+ result = coll2->search("*", {}, "", {}, {}, {0}).get();
+ ASSERT_EQ(1, result["hits"][0]["document"]["ref_int32_array_field_sequence_id"].size());
+ ASSERT_EQ(3, result["hits"][0]["document"]["ref_int32_array_field_sequence_id"][0]);
+
+ // int64/int64[] reference fields
+ doc_json = R"({
+ "int64_field": 1
+ })"_json;
+ add_doc_op = coll1->add(doc_json.dump());
+ ASSERT_TRUE(add_doc_op.ok());
+
+ doc_json = R"({
+ "int64_field": 1,
+ "int64_array_field": [2, -9223372036854775808]
+ })"_json;
+ add_doc_op = coll1->add(doc_json.dump());
+ ASSERT_TRUE(add_doc_op.ok());
+
+ doc_json = R"({
+ "int64_field": 4,
+ "int64_array_field": [5, 9223372036854775807]
+ })"_json;
+ add_doc_op = coll1->add(doc_json.dump());
+ ASSERT_TRUE(add_doc_op.ok());
+
+ doc_json = R"({
+ "ref_int64_field": "1"
+ })"_json;
+ add_doc_op = coll2->add(doc_json.dump());
+ ASSERT_FALSE(add_doc_op.ok());
+ ASSERT_EQ("Field `ref_int64_field` must have `int64` value.", add_doc_op.error());
+
+ doc_json = R"({
+ "ref_int64_field": 0
+ })"_json;
+ add_doc_op = coll2->add(doc_json.dump());
+ ASSERT_FALSE(add_doc_op.ok());
+ ASSERT_EQ("Reference document having `int64_field: 0` not found in the collection `coll1`.", add_doc_op.error());
+
+ doc_json = R"({
+ "ref_int64_field": 1
+ })"_json;
+ add_doc_op = coll2->add(doc_json.dump());
+ ASSERT_FALSE(add_doc_op.ok());
+ ASSERT_EQ("Multiple documents having `int64_field: 1` found in the collection `coll1`.", add_doc_op.error());
+
+ doc_json = R"({
+ "ref_int64_field": 4
+ })"_json;
+ add_doc_op = coll2->add(doc_json.dump());
+ ASSERT_TRUE(add_doc_op.ok());
+
+ result = coll2->search("*", {}, "", {}, {}, {0}).get();
+ ASSERT_EQ(7, result["hits"][0]["document"]["ref_int64_field_sequence_id"]);
+
+ doc_json = R"({
+ "ref_int64_array_field": [1, "2"]
+ })"_json;
+ add_doc_op = coll2->add(doc_json.dump());
+ ASSERT_FALSE(add_doc_op.ok());
+ ASSERT_EQ("Field `ref_int64_array_field` must only have `int64` values.", add_doc_op.error());
+
+ doc_json = R"({
+ "ref_int64_array_field": [1]
+ })"_json;
+ add_doc_op = coll2->add(doc_json.dump());
+ ASSERT_FALSE(add_doc_op.ok());
+ ASSERT_EQ("No reference document matching `int64_array_field: [1]` found in the collection `coll1`.", add_doc_op.error());
+
+ doc_json = R"({
+ "ref_int64_array_field": [1, -9223372036854775809]
+ })"_json;
+ add_doc_op = coll2->add(doc_json.dump());
+ ASSERT_FALSE(add_doc_op.ok());
+ ASSERT_EQ("Field `ref_int64_array_field` must only have `int64` values.", add_doc_op.error());
+
+ doc_json = R"({
+ "ref_int64_array_field": [1, 1.5]
+ })"_json;
+ add_doc_op = coll2->add(doc_json.dump());
+ ASSERT_FALSE(add_doc_op.ok());
+ ASSERT_EQ("Field `ref_int64_array_field` must only have `int64` values.", add_doc_op.error());
+
+ doc_json = R"({
+ "ref_int64_array_field": [1, 2]
+ })"_json;
+ add_doc_op = coll2->add(doc_json.dump());
+ ASSERT_TRUE(add_doc_op.ok());
+
+ result = coll2->search("*", {}, "", {}, {}, {0}).get();
+ ASSERT_EQ(1, result["hits"][0]["document"]["ref_int64_array_field_sequence_id"].size());
+ ASSERT_EQ(6, result["hits"][0]["document"]["ref_int64_array_field_sequence_id"][0]);
+
+ doc_json = R"({
+ "ref_int64_array_field": [2, 5]
+ })"_json;
+ add_doc_op = coll2->add(doc_json.dump());
+ ASSERT_TRUE(add_doc_op.ok());
+
+ result = coll2->search("*", {}, "", {}, {}, {0}).get();
+ ASSERT_EQ(2, result["hits"][0]["document"]["ref_int64_array_field_sequence_id"].size());
+ ASSERT_EQ(6, result["hits"][0]["document"]["ref_int64_array_field_sequence_id"][0]);
+ ASSERT_EQ(7, result["hits"][0]["document"]["ref_int64_array_field_sequence_id"][1]);
+
+ doc_json = R"({
+ "ref_int64_array_field": [-9223372036854775808]
+ })"_json;
+ add_doc_op = coll2->add(doc_json.dump());
+ ASSERT_TRUE(add_doc_op.ok());
+
+ result = coll2->search("*", {}, "", {}, {}, {0}).get();
+ ASSERT_EQ(1, result["hits"][0]["document"]["ref_int64_array_field_sequence_id"].size());
+ ASSERT_EQ(6, result["hits"][0]["document"]["ref_int64_array_field_sequence_id"][0]);
+
+ // float/float[] reference fields
+ doc_json = R"({
+ "ref_float_field": 1.5
+ })"_json;
+ add_doc_op = coll2->add(doc_json.dump());
+ ASSERT_FALSE(add_doc_op.ok());
+ ASSERT_EQ("Cannot add a reference to `coll1.float_field` of type `float`.", add_doc_op.error());
+
+ doc_json = R"({
+ "ref_float_array_field": [1.5]
+ })"_json;
+ add_doc_op = coll2->add(doc_json.dump());
+ ASSERT_FALSE(add_doc_op.ok());
+ ASSERT_EQ("Cannot add a reference to `coll1.float_array_field` of type `float[]`.", add_doc_op.error());
+
+ // bool/bool[] reference fields
+ doc_json = R"({
+ "ref_bool_field": "true"
+ })"_json;
+ add_doc_op = coll2->add(doc_json.dump());
+ ASSERT_FALSE(add_doc_op.ok());
+ ASSERT_EQ("Cannot add a reference to `coll1.bool_field` of type `bool`.", add_doc_op.error());
+
+ doc_json = R"({
+ "ref_bool_array_field": ["true"]
+ })"_json;
+ add_doc_op = coll2->add(doc_json.dump());
+ ASSERT_FALSE(add_doc_op.ok());
+ ASSERT_EQ("Cannot add a reference to `coll1.bool_array_field` of type `bool[]`.", add_doc_op.error());
+
+ // geopoint/geopoint[] reference fields
+ doc_json = R"({
+ "ref_geopoint_field": [13.12631, 80.20252]
+ })"_json;
+ add_doc_op = coll2->add(doc_json.dump());
+ ASSERT_FALSE(add_doc_op.ok());
+ ASSERT_EQ("Cannot add a reference to `coll1.geopoint_field` of type `geopoint`.", add_doc_op.error());
+
+ doc_json = R"({
+ "ref_geopoint_array_field": [[13.12631, 80.20252]]
+ })"_json;
+ add_doc_op = coll2->add(doc_json.dump());
+ ASSERT_FALSE(add_doc_op.ok());
+ ASSERT_EQ("Cannot add a reference to `coll1.geopoint_array_field` of type `geopoint[]`.", add_doc_op.error());
+
+ // object/object[] reference fields
+ doc_json = R"({
+ "ref_object_field": {
+ "foo": "bar"
+ }
+ })"_json;
+ add_doc_op = coll2->add(doc_json.dump());
+ ASSERT_FALSE(add_doc_op.ok());
+ ASSERT_EQ("Cannot add a reference to `coll1.object_field` of type `object`.", add_doc_op.error());
+
+ doc_json = R"({
+ "ref_object_array_field": [
+ {
+ "foo": "bar"
+ }
+ ]
+ })"_json;
+ add_doc_op = coll2->add(doc_json.dump());
+ ASSERT_FALSE(add_doc_op.ok());
+ ASSERT_EQ("Cannot add a reference to `coll1.object_array_field` of type `object[]`.", add_doc_op.error());
}
TEST_F(CollectionJoinTest, FilterByReference_SingleMatch) {