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

Index document containing a reference field.

Harpreet Sangar49f9750095ba
diff --git a/src/index.cpp b/src/index.cpp
index 6f9e8d7b..1debfda0 100644
--- a/src/index.cpp
+++ b/src/index.cpp
@@ -23,6 +23,7 @@
#include <or_iterator.h>
#include <timsort.hpp>
#include "logger.h"
+#include <collection_manager.h>
#define RETURN_CIRCUIT_BREAKER if((std::chrono::duration_cast<std::chrono::microseconds>( \
std::chrono::system_clock::now().time_since_epoch()).count() - search_begin_us) > search_stop_us) { \
@@ -431,6 +432,49 @@ Option<uint32_t> Index::validate_index_in_memory(nlohmann::json& document, uint3
continue;
}
+ if (!a_field.reference.empty()) {
+ // Add foo_sequence_id field in the document.
+
+ std::vector<std::string> tokens;
+ StringUtils::split(a_field.reference, tokens, ".");
+
+ if (tokens.size() < 2) {
+ return Option<>(400, "Invalid reference `" + a_field.reference + "`.");
+ }
+
+ auto& cm = CollectionManager::get_instance();
+ auto collection = cm.get_collection(tokens[0]);
+ if (collection == nullptr) {
+ return Option<>(400, "Referenced collection `" + tokens[0] + "` not found.");
+ }
+
+ if (collection->get_schema().count(tokens[1]) == 0) {
+ return Option<>(400, "Referenced field `" + tokens[1] + "` not found in the collection `"
+ + tokens[0] + "`.");
+ }
+
+ auto referenced_field_name = tokens[1];
+ if (!collection->get_schema().at(referenced_field_name).index) {
+ return Option<>(400, "Referenced field `" + tokens[1] + "` in the collection `"
+ + tokens[0] + "` must be indexed.");
+ }
+
+ std::vector<std::pair<size_t, uint32_t*>> documents;
+ auto value = document[a_field.name].get<std::string>();
+ collection->get_filter_ids(referenced_field_name + ":=" + value, documents);
+
+ if (documents[0].first != 1) {
+ auto match = " `" + referenced_field_name + "` = `" + value + "` ";
+ return Option<>(400, documents[0].first < 1 ?
+ "Referenced document having" + match + "not found in the collection `" + tokens[0] + "`." :
+ "Multiple documents having" + match + "found in the collection `" + tokens[0] + "`.");
+ }
+
+ document[a_field.name + "_sequence_id"] = collection->get_seq_id_collection_prefix() + std::to_string(*(documents[0].second));
+
+ delete [] documents[0].second;
+ }
+
if(document.count(field_name) == 0) {
return Option<>(400, "Field `" + field_name + "` has been declared in the schema, "
"but is not found in the document.");
diff --git a/test/collection_join_test.cpp b/test/collection_join_test.cpp
index f57895d4..e79f76d4 100644
--- a/test/collection_join_test.cpp
+++ b/test/collection_join_test.cpp
@@ -98,4 +98,171 @@ TEST_F(CollectionJoinTest, SchemaReferenceField) {
ASSERT_TRUE(schema.at("product_id_sequence_id").index);
collectionManager.drop_collection("Customers");
+}
+
+TEST_F(CollectionJoinTest, IndexReferenceField) {
+ auto products_schema_json =
+ R"({
+ "name": "Products",
+ "fields": [
+ {"name": "product_id", "type": "string", "index": false, "optional": true},
+ {"name": "product_name", "type": "string"},
+ {"name": "product_description", "type": "string"}
+ ]
+ })"_json;
+ auto collection_create_op = collectionManager.create_collection(products_schema_json);
+ ASSERT_TRUE(collection_create_op.ok());
+
+ auto customers_schema_json =
+ R"({
+ "name": "Customers",
+ "fields": [
+ {"name": "customer_id", "type": "string"},
+ {"name": "customer_name", "type": "string"},
+ {"name": "product_price", "type": "float"},
+ {"name": "product_id", "type": "string", "reference": "foo"}
+ ]
+ })"_json;
+ collection_create_op = collectionManager.create_collection(customers_schema_json);
+ ASSERT_TRUE(collection_create_op.ok());
+
+ nlohmann::json customer_json = R"({
+ "customer_id": "customer_a",
+ "customer_name": "Joe",
+ "product_price": 143,
+ "product_id": "a"
+ })"_json;
+
+ auto customer_collection = collection_create_op.get();
+ auto add_op = customer_collection->add(customer_json.dump());
+ ASSERT_FALSE(add_op.ok());
+ ASSERT_EQ("Invalid reference `foo`.", add_op.error());
+ collectionManager.drop_collection("Customers");
+
+ customers_schema_json =
+ R"({
+ "name": "Customers",
+ "fields": [
+ {"name": "customer_id", "type": "string"},
+ {"name": "customer_name", "type": "string"},
+ {"name": "product_price", "type": "float"},
+ {"name": "product_id", "type": "string", "reference": "products.product_id"}
+ ]
+ })"_json;
+ collection_create_op = collectionManager.create_collection(customers_schema_json);
+ ASSERT_TRUE(collection_create_op.ok());
+
+ customer_collection = collection_create_op.get();
+ add_op = customer_collection->add(customer_json.dump());
+ ASSERT_FALSE(add_op.ok());
+ ASSERT_EQ("Referenced collection `products` not found.", add_op.error());
+ collectionManager.drop_collection("Customers");
+
+ customers_schema_json =
+ R"({
+ "name": "Customers",
+ "fields": [
+ {"name": "customer_id", "type": "string"},
+ {"name": "customer_name", "type": "string"},
+ {"name": "product_price", "type": "float"},
+ {"name": "product_id", "type": "string", "reference": "Products.id"}
+ ]
+ })"_json;
+ collection_create_op = collectionManager.create_collection(customers_schema_json);
+ ASSERT_TRUE(collection_create_op.ok());
+
+ customer_collection = collection_create_op.get();
+ add_op = customer_collection->add(customer_json.dump());
+ ASSERT_FALSE(add_op.ok());
+ ASSERT_EQ("Referenced field `id` not found in the collection `Products`.", add_op.error());
+ collectionManager.drop_collection("Customers");
+
+ customers_schema_json =
+ R"({
+ "name": "Customers",
+ "fields": [
+ {"name": "customer_id", "type": "string"},
+ {"name": "customer_name", "type": "string"},
+ {"name": "product_price", "type": "float"},
+ {"name": "product_id", "type": "string", "reference": "Products.product_id"}
+ ]
+ })"_json;
+ collection_create_op = collectionManager.create_collection(customers_schema_json);
+ ASSERT_TRUE(collection_create_op.ok());
+
+ customer_collection = collection_create_op.get();
+ add_op = customer_collection->add(customer_json.dump());
+ ASSERT_FALSE(add_op.ok());
+ ASSERT_EQ("Referenced field `product_id` in the collection `Products` must be indexed.", add_op.error());
+
+ collectionManager.drop_collection("Products");
+ products_schema_json =
+ R"({
+ "name": "Products",
+ "fields": [
+ {"name": "product_id", "type": "string"},
+ {"name": "product_name", "type": "string"},
+ {"name": "product_description", "type": "string"}
+ ]
+ })"_json;
+ collection_create_op = collectionManager.create_collection(products_schema_json);
+ ASSERT_TRUE(collection_create_op.ok());
+
+ add_op = customer_collection->add(customer_json.dump());
+ ASSERT_EQ("Referenced document having `product_id` = `a` not found in the collection `Products`.", add_op.error());
+
+ std::vector<nlohmann::json> products = {
+ R"({
+ "product_id": "product_a",
+ "product_name": "shampoo",
+ "product_description": "Our new moisturizing shampoo is perfect for those with dry or damaged hair."
+ })"_json,
+ R"({
+ "product_id": "product_a",
+ "product_name": "soap",
+ "product_description": "Introducing our all-natural, organic soap bar made with essential oils and botanical ingredients."
+ })"_json
+ };
+ for (auto const &json: products){
+ auto add_op = collection_create_op.get()->add(json.dump());
+ if (!add_op.ok()) {
+ LOG(INFO) << add_op.error();
+ }
+ ASSERT_TRUE(add_op.ok());
+ }
+
+ customer_json["product_id"] = "product_a";
+ add_op = customer_collection->add(customer_json.dump());
+ ASSERT_EQ("Multiple documents having `product_id` = `product_a` found in the collection `Products`.", add_op.error());
+
+ collectionManager.drop_collection("Products");
+ products[1]["product_id"] = "product_b";
+ products_schema_json =
+ R"({
+ "name": "Products",
+ "fields": [
+ {"name": "product_id", "type": "string"},
+ {"name": "product_name", "type": "string"},
+ {"name": "product_description", "type": "string"}
+ ]
+ })"_json;
+ collection_create_op = collectionManager.create_collection(products_schema_json);
+ ASSERT_TRUE(collection_create_op.ok());
+ for (auto const &json: products){
+ auto add_op = collection_create_op.get()->add(json.dump());
+ if (!add_op.ok()) {
+ LOG(INFO) << add_op.error();
+ }
+ ASSERT_TRUE(add_op.ok());
+ }
+
+ add_op = customer_collection->add(customer_json.dump());
+ ASSERT_TRUE(add_op.ok());
+
+ auto result = customer_collection->search("*", {"customer_id"}, "", {}, {}, {0}).get();
+ ASSERT_EQ(result["hits"][0]["document"].count("product_id"), 1);
+// ASSERT_EQ(result["hits"][0]["document"].count("product_id_sequence_id"), 0);
+
+ collectionManager.drop_collection("Customers");
+ collectionManager.drop_collection("Products");
}
\ No newline at end of file