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

Add reference in field struct.

Harpreet Sangar56d382d1e0c5
diff --git a/include/field.h b/include/field.h
index 242f8f9b..d7960716 100644
--- a/include/field.h
+++ b/include/field.h
@@ -47,6 +47,7 @@ namespace fields {
static const std::string nested_array = "nested_array";
static const std::string num_dim = "num_dim";
static const std::string vec_dist = "vec_dist";
+ static const std::string reference = "reference";
}
enum vector_distance_type_t {
@@ -76,13 +77,15 @@ struct field {
static constexpr int VAL_UNKNOWN = 2;
+ std::string reference; // Reference to another collection.
+
field() {}
field(const std::string &name, const std::string &type, const bool facet, const bool optional = false,
bool index = true, std::string locale = "", int sort = -1, int infix = -1, bool nested = false,
- int nested_array = 0, size_t num_dim = 0, vector_distance_type_t vec_dist = cosine) :
+ int nested_array = 0, size_t num_dim = 0, vector_distance_type_t vec_dist = cosine, std::string reference = "") :
name(name), type(type), facet(facet), optional(optional), index(index), locale(locale),
- nested(nested), nested_array(nested_array), num_dim(num_dim), vec_dist(vec_dist) {
+ nested(nested), nested_array(nested_array), num_dim(num_dim), vec_dist(vec_dist), reference(reference) {
set_computed_defaults(sort, infix);
}
diff --git a/src/field.cpp b/src/field.cpp
index c9df953f..f3a407e3 100644
--- a/src/field.cpp
+++ b/src/field.cpp
@@ -525,6 +525,12 @@ Option<bool> field::json_field_to_field(bool enable_nested_fields, nlohmann::jso
}
}
+ if (field_json.count(fields::reference) != 0 && !field_json.at(fields::reference).is_string()) {
+ return Option<bool>(400, "Reference should be a string.");
+ } else if (field_json.count(fields::reference) == 0) {
+ field_json[fields::reference] = "";
+ }
+
if(field_json["name"] == ".*") {
if(field_json.count(fields::facet) == 0) {
field_json[fields::facet] = false;
@@ -562,6 +568,10 @@ Option<bool> field::json_field_to_field(bool enable_nested_fields, nlohmann::jso
return Option<bool>(400, "Field `.*` must be an index field.");
}
+ if (field_json.count(fields::reference) != 0) {
+ return Option<bool>(400, "Field `.*` cannot be a reference field.");
+ }
+
field fallback_field(field_json["name"], field_json["type"], field_json["facet"],
field_json["optional"], field_json[fields::index], field_json[fields::locale],
field_json[fields::sort], field_json[fields::infix]);
@@ -659,6 +669,10 @@ Option<bool> field::json_field_to_field(bool enable_nested_fields, nlohmann::jso
bool is_obj = field_json[fields::type] == field_types::OBJECT || field_json[fields::type] == field_types::OBJECT_ARRAY;
bool is_regexp_name = field_json[fields::name].get<std::string>().find(".*") != std::string::npos;
+ if (is_regexp_name && field_json.count(fields::reference) != 0) {
+ return Option<bool>(400, "Wildcard field cannot have a reference.");
+ }
+
if(is_obj || (!is_regexp_name && enable_nested_fields &&
field_json[fields::name].get<std::string>().find('.') != std::string::npos)) {
field_json[fields::nested] = true;
@@ -679,7 +693,8 @@ Option<bool> field::json_field_to_field(bool enable_nested_fields, nlohmann::jso
field(field_json[fields::name], field_json[fields::type], field_json[fields::facet],
field_json[fields::optional], field_json[fields::index], field_json[fields::locale],
field_json[fields::sort], field_json[fields::infix], field_json[fields::nested],
- field_json[fields::nested_array], field_json[fields::num_dim], vec_dist)
+ field_json[fields::nested_array], field_json[fields::num_dim], vec_dist,
+ field_json[fields::reference])
);
return Option<bool>(true);
diff --git a/test/collection_join_test.cpp b/test/collection_join_test.cpp
new file mode 100644
index 00000000..0065e8ae
--- /dev/null
+++ b/test/collection_join_test.cpp
@@ -0,0 +1,96 @@
+#include <gtest/gtest.h>
+#include <string>
+#include <vector>
+#include <fstream>
+#include <algorithm>
+#include <collection_manager.h>
+#include "collection.h"
+
+class CollectionJoinTest : public ::testing::Test {
+protected:
+ Store *store;
+ CollectionManager & collectionManager = CollectionManager::get_instance();
+ std::atomic<bool> quit = false;
+
+ std::vector<std::string> query_fields;
+ std::vector<sort_by> sort_fields;
+
+ void setupCollection() {
+ std::string state_dir_path = "/tmp/typesense_test/collection_join";
+ LOG(INFO) << "Truncating and creating: " << state_dir_path;
+ system(("rm -rf "+state_dir_path+" && mkdir -p "+state_dir_path).c_str());
+
+ store = new Store(state_dir_path);
+ collectionManager.init(store, 1.0, "auth_key", quit);
+ collectionManager.load(8, 1000);
+ }
+
+ virtual void SetUp() {
+ setupCollection();
+ }
+
+ virtual void TearDown() {
+ collectionManager.dispose();
+ delete store;
+ }
+};
+
+TEST_F(CollectionJoinTest, SchemaReferenceField) {
+ nlohmann::json schema_json =
+ R"({
+ "name": "Customers",
+ "fields": [
+ {"name": "product_.*", "type": "string", "reference": "Products.product_id"}
+ ]
+ })"_json;
+
+ auto collection_create_op = collectionManager.create_collection(schema_json);
+ ASSERT_FALSE(collection_create_op.ok());
+ ASSERT_EQ("Wildcard field cannot have a reference.", collection_create_op.error());
+
+ schema_json =
+ R"({
+ "name": "Customers",
+ "fields": [
+ {"name": ".*", "type": "auto", "reference": "Products.product_id"}
+ ]
+ })"_json;
+
+ collection_create_op = collectionManager.create_collection(schema_json);
+ ASSERT_FALSE(collection_create_op.ok());
+ ASSERT_EQ("Field `.*` cannot be a reference field.", collection_create_op.error());
+
+ schema_json =
+ R"({
+ "name": "Customers",
+ "fields": [
+ {"name": "product_id", "type": "string", "reference": 123},
+ {"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("Reference should be a string.", collection_create_op.error());
+
+ schema_json =
+ R"({
+ "name": "Customers",
+ "fields": [
+ {"name": "product_id", "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_TRUE(collection_create_op.ok());
+
+ auto collection = collection_create_op.get();
+ auto schema = collection->get_schema();
+
+ ASSERT_EQ(schema.at("customer_name").reference, "");
+ ASSERT_EQ(schema.at("product_id").reference, "Products.product_id");
+ collectionManager.drop_collection("Customers");
+}
\ No newline at end of file