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

Reference `include_fields`.

Harpreet Sangare7949e650ab2
diff --git a/test/collection_join_test.cpp b/test/collection_join_test.cpp
index 98e7663f..f302d3dc 100644
--- a/test/collection_join_test.cpp
+++ b/test/collection_join_test.cpp
@@ -549,4 +549,164 @@ TEST_F(CollectionJoinTest, FilterByReference_MultipleMatch) {
collectionManager.drop_collection("Users");
collectionManager.drop_collection("Repos");
collectionManager.drop_collection("Links");
+}
+
+TEST_F(CollectionJoinTest, IncludeFieldsByReference_SingleMatch) {
+ auto schema_json =
+ R"({
+ "name": "Products",
+ "fields": [
+ {"name": "product_id", "type": "string"},
+ {"name": "product_name", "type": "string"},
+ {"name": "product_description", "type": "string"}
+ ]
+ })"_json;
+ std::vector<nlohmann::json> documents = {
+ 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_b",
+ "product_name": "soap",
+ "product_description": "Introducing our all-natural, organic soap bar made with essential oils and botanical ingredients."
+ })"_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": "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;
+ documents = {
+ R"({
+ "customer_id": "customer_a",
+ "customer_name": "Joe",
+ "product_price": 143,
+ "product_id": "product_a"
+ })"_json,
+ R"({
+ "customer_id": "customer_a",
+ "customer_name": "Joe",
+ "product_price": 73.5,
+ "product_id": "product_b"
+ })"_json,
+ R"({
+ "customer_id": "customer_b",
+ "customer_name": "Dan",
+ "product_price": 75,
+ "product_id": "product_a"
+ })"_json,
+ R"({
+ "customer_id": "customer_b",
+ "customer_name": "Dan",
+ "product_price": 140,
+ "product_id": "product_b"
+ })"_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", "Products"},
+ {"q", "s"},
+ {"query_by", "product_name"},
+ {"filter_by", "$Customers(customer_id:=customer_a && product_price:<100)"},
+ };
+
+ 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();
+
+ req_params["include_fields"] = "$foo.bar";
+ auto search_op = collectionManager.do_search(req_params, embedded_params, json_res, now_ts);
+ ASSERT_FALSE(search_op.ok());
+ ASSERT_EQ("Invalid reference in include_fields, expected `$CollectionName(fieldA, ...)`.", search_op.error());
+
+ req_params["include_fields"] = "$foo(bar";
+ search_op = collectionManager.do_search(req_params, embedded_params, json_res, now_ts);
+ ASSERT_FALSE(search_op.ok());
+ ASSERT_EQ("Invalid reference in include_fields, expected `$CollectionName(fieldA, ...)`.", search_op.error());
+
+// req_params["include_fields"] = "$foo(bar)";
+// search_op = collectionManager.do_search(req_params, embedded_params, json_res, now_ts);
+// ASSERT_FALSE(search_op.ok());
+// ASSERT_EQ("Referenced collection `foo` not found.", search_op.error());
+//
+// req_params["include_fields"] = "$Customers(bar)";
+// search_op = collectionManager.do_search(req_params, embedded_params, json_res, now_ts);
+// ASSERT_TRUE(search_op.ok());
+//
+// nlohmann::json res_obj = nlohmann::json::parse(json_res);
+// ASSERT_EQ(1, res_obj["found"].get<size_t>());
+// ASSERT_EQ(1, res_obj["hits"].size());
+// ASSERT_EQ(0, res_obj["hits"][0]["document"].size());
+//
+// req_params["include_fields"] = "$Customers(product_price)";
+// search_op = collectionManager.do_search(req_params, embedded_params, json_res, now_ts);
+// ASSERT_TRUE(search_op.ok());
+//
+// res_obj = nlohmann::json::parse(json_res);
+// ASSERT_EQ(1, res_obj["found"].get<size_t>());
+// ASSERT_EQ(1, res_obj["hits"].size());
+// ASSERT_EQ(1, res_obj["hits"][0]["document"].size());
+// ASSERT_EQ(1, res_obj["hits"][0]["document"].count("product_price"));
+// ASSERT_EQ(73.5, res_obj["hits"][0]["document"].at("product_price"));
+//
+// req_params["include_fields"] = "$Customers(product_price, customer_id)";
+// search_op = collectionManager.do_search(req_params, embedded_params, json_res, now_ts);
+// ASSERT_TRUE(search_op.ok());
+//
+// res_obj = nlohmann::json::parse(json_res);
+// ASSERT_EQ(1, res_obj["found"].get<size_t>());
+// ASSERT_EQ(1, res_obj["hits"].size());
+// ASSERT_EQ(2, res_obj["hits"][0]["document"].size());
+// ASSERT_EQ(1, res_obj["hits"][0]["document"].count("product_price"));
+// ASSERT_EQ(73.5, res_obj["hits"][0]["document"].at("product_price"));
+// ASSERT_EQ(1, res_obj["hits"][0]["document"].count("customer_id"));
+// ASSERT_EQ("customer_a", res_obj["hits"][0]["document"].at("customer_id"));
+//
+// req_params["include_fields"] = "*, $Customers(product_price, customer_id)";
+// search_op = collectionManager.do_search(req_params, embedded_params, json_res, now_ts);
+// ASSERT_TRUE(search_op.ok());
+//
+// res_obj = nlohmann::json::parse(json_res);
+// ASSERT_EQ(1, res_obj["found"].get<size_t>());
+// ASSERT_EQ(1, res_obj["hits"].size());
+// // 3 fields in Products document and 2 fields from Customers document
+// ASSERT_EQ(5, res_obj["hits"][0]["document"].size());
+//
+// req_params["include_fields"] = "*, $Customers(product*)";
+// search_op = collectionManager.do_search(req_params, embedded_params, json_res, now_ts);
+// ASSERT_TRUE(search_op.ok());
+//
+// res_obj = nlohmann::json::parse(json_res);
+// ASSERT_EQ(1, res_obj["found"].get<size_t>());
+// ASSERT_EQ(1, res_obj["hits"].size());
+// // 3 fields in Products document and 2 fields from Customers document
+// ASSERT_EQ(5, res_obj["hits"][0]["document"].size());
+// ASSERT_EQ(1, res_obj["hits"][0]["document"].count("product_id_sequence_id"));
}
\ No newline at end of file