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

Persist `reference` field property.

Harpreet Sangarc25d69ea8579
diff --git a/include/field.h b/include/field.h
index d7960716..ca29fa43 100644
--- a/include/field.h
+++ b/include/field.h
@@ -310,6 +310,10 @@ struct field {
field_val[fields::vec_dist] = field.vec_dist == ip ? "ip" : "cosine";
}
+ if (!field.reference.empty()) {
+ field_val[fields::reference] = field.reference;
+ }
+
fields_json.push_back(field_val);
if(!field.has_valid_type()) {
diff --git a/src/collection.cpp b/src/collection.cpp
index cd904bc5..1559c181 100644
--- a/src/collection.cpp
+++ b/src/collection.cpp
@@ -175,6 +175,10 @@ nlohmann::json Collection::get_summary_json() const {
field_json[fields::num_dim] = coll_field.num_dim;
}
+ if (!coll_field.reference.empty()) {
+ field_json[fields::reference] = coll_field.reference;
+ }
+
fields_arr.push_back(field_json);
}
diff --git a/src/collection_manager.cpp b/src/collection_manager.cpp
index 509d312a..01853fd3 100644
--- a/src/collection_manager.cpp
+++ b/src/collection_manager.cpp
@@ -54,6 +54,10 @@ Collection* CollectionManager::init_collection(const nlohmann::json & collection
field_obj[fields::num_dim] = 0;
}
+ if (field_obj.count(fields::reference) == 0) {
+ field_obj[fields::reference] = "";
+ }
+
vector_distance_type_t vec_dist_type = vector_distance_type_t::cosine;
if(field_obj.count(fields::vec_dist) != 0) {
@@ -66,7 +70,7 @@ Collection* CollectionManager::init_collection(const nlohmann::json & collection
field f(field_obj[fields::name], field_obj[fields::type], field_obj[fields::facet],
field_obj[fields::optional], field_obj[fields::index], field_obj[fields::locale],
-1, field_obj[fields::infix], field_obj[fields::nested], field_obj[fields::nested_array],
- field_obj[fields::num_dim], vec_dist_type);
+ field_obj[fields::num_dim], vec_dist_type, field_obj[fields::reference]);
// value of `sort` depends on field type
if(field_obj.count(fields::sort) == 0) {
diff --git a/src/field.cpp b/src/field.cpp
index a7c0819e..814c3c54 100644
--- a/src/field.cpp
+++ b/src/field.cpp
@@ -568,7 +568,7 @@ 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) {
+ if (!field_json[fields::reference].get<std::string>().empty()) {
return Option<bool>(400, "Field `.*` cannot be a reference field.");
}
@@ -669,7 +669,7 @@ 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) {
+ if (is_regexp_name && !field_json[fields::reference].get<std::string>().empty()) {
return Option<bool>(400, "Wildcard field cannot have a reference.");
}
diff --git a/test/collection_manager_test.cpp b/test/collection_manager_test.cpp
index 747c4171..1f258206 100644
--- a/test/collection_manager_test.cpp
+++ b/test/collection_manager_test.cpp
@@ -36,7 +36,8 @@ protected:
{"name": "not_stored", "type": "string", "optional": true, "index": false},
{"name": "points", "type": "int32"},
{"name": "person", "type": "object", "optional": true},
- {"name": "vec", "type": "float[]", "num_dim": 128, "optional": true}
+ {"name": "vec", "type": "float[]", "num_dim": 128, "optional": true},
+ {"name": "product_id", "type": "string", "reference": "Products.product_id"}
],
"default_sorting_field": "points",
"symbols_to_index":["+"],
@@ -44,7 +45,9 @@ protected:
})"_json;
sort_fields = { sort_by("points", "DESC") };
- collection1 = collectionManager.create_collection(schema).get();
+ auto op = collectionManager.create_collection(schema);
+ ASSERT_TRUE(op.ok());
+ collection1 = op.get();
}
virtual void SetUp() {
@@ -210,6 +213,18 @@ TEST_F(CollectionManagerTest, CollectionCreation) {
"sort":false,
"type":"float[]",
"vec_dist":"cosine"
+ },
+ {
+ "facet":false,
+ "index":true,
+ "infix":false,
+ "locale":"",
+ "name":"product_id",
+ "nested":false,
+ "optional":false,
+ "sort":false,
+ "type":"string",
+ "reference":"Products.product_id"
}
],
"id":0,