Merge pull request #1129 from happy-san/v0.26-facets

Fix `Collection::get_reference_field`, return field name of current schema.
This commit is contained in:
Kishore Nallan 2023-07-31 15:23:55 +05:30 committed by GitHub
commit 45848721cb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 80 additions and 22 deletions

View File

@ -2711,7 +2711,7 @@ Option<std::string> Collection::get_reference_field(const std::string & collecti
for (auto const& pair: reference_fields) {
auto reference_pair = pair.second;
if (reference_pair.collection == collection_name) {
reference_field_name = reference_pair.field;
reference_field_name = pair.first;
break;
}
}

View File

@ -1436,19 +1436,19 @@ TEST_F(CollectionJoinTest, CascadeDeletion) {
R"({
"name": "Products",
"fields": [
{"name": "product_id", "type": "string"},
{"name": "product_idx", "type": "string"},
{"name": "product_name", "type": "string", "infix": true},
{"name": "product_description", "type": "string"}
]
})"_json;
std::vector<nlohmann::json> documents = {
R"({
"product_id": "product_a",
"product_idx": "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_idx": "product_b",
"product_name": "soap",
"product_description": "Introducing our all-natural, organic soap bar made with essential oils and botanical ingredients."
})"_json
@ -1463,36 +1463,56 @@ TEST_F(CollectionJoinTest, CascadeDeletion) {
schema_json =
R"({
"name": "Customers",
"name": "Users",
"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"}
{"name": "user_id", "type": "string"},
{"name": "user_name", "type": "string"}
]
})"_json;
documents = {
R"({
"customer_id": "customer_a",
"customer_name": "Joe",
"user_id": "user_a",
"user_name": "Joe"
})"_json,
R"({
"user_id": "user_b",
"user_name": "Dan"
})"_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());
ASSERT_TRUE(add_op.ok());
}
schema_json =
R"({
"name": "CustomerProductPrices",
"fields": [
{"name": "product_price", "type": "float"},
{"name": "user_id", "type": "string", "reference": "Users.user_id"},
{"name": "product_id", "type": "string", "reference": "Products.product_idx"}
]
})"_json;
documents = {
R"({
"user_id": "user_a",
"product_price": 143,
"product_id": "product_a"
})"_json,
R"({
"customer_id": "customer_a",
"customer_name": "Joe",
"user_id": "user_a",
"product_price": 73.5,
"product_id": "product_b"
})"_json,
R"({
"customer_id": "customer_b",
"customer_name": "Dan",
"user_id": "user_b",
"product_price": 75,
"product_id": "product_a"
})"_json,
R"({
"customer_id": "customer_b",
"customer_name": "Dan",
"user_id": "user_b",
"product_price": 140,
"product_id": "product_b"
})"_json
@ -1503,9 +1523,12 @@ TEST_F(CollectionJoinTest, CascadeDeletion) {
auto add_op = collection_create_op.get()->add(json.dump());
ASSERT_TRUE(add_op.ok());
}
std::map<std::string, std::string> req_params = {
{"collection", "Customers"},
{"collection", "Products"},
{"q", "*"},
{"filter_by", "$CustomerProductPrices(user_id:= user_a)"},
{"include_fields", "$CustomerProductPrices(product_price)"}
};
nlohmann::json embedded_params;
std::string json_res;
@ -1513,8 +1536,20 @@ TEST_F(CollectionJoinTest, CascadeDeletion) {
std::chrono::system_clock::now().time_since_epoch()).count();
auto 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(2, res_obj["found"].get<size_t>());
ASSERT_EQ("product_b", res_obj["hits"][0]["document"].at("product_idx"));
ASSERT_EQ("product_a", res_obj["hits"][1]["document"].at("product_idx"));
req_params = {
{"collection", "CustomerProductPrices"},
{"q", "*"},
};
search_op = collectionManager.do_search(req_params, embedded_params, json_res, now_ts);
res_obj = nlohmann::json::parse(json_res);
ASSERT_EQ(4, res_obj["found"].get<size_t>());
ASSERT_EQ("product_b", res_obj["hits"][0]["document"].at("product_id"));
ASSERT_EQ("product_a", res_obj["hits"][1]["document"].at("product_id"));
@ -1529,8 +1564,8 @@ TEST_F(CollectionJoinTest, CascadeDeletion) {
res_obj = nlohmann::json::parse(json_res);
ASSERT_EQ(2, res_obj["found"].get<size_t>());
ASSERT_EQ("product_b", res_obj["hits"][0]["document"].at("product_id"));
ASSERT_EQ("product_a", res_obj["hits"][1]["document"].at("product_id"));
ASSERT_EQ("product_b", res_obj["hits"][0]["document"].at("product_idx"));
ASSERT_EQ("product_a", res_obj["hits"][1]["document"].at("product_idx"));
collectionManager.get_collection_unsafe("Products")->remove("0");
@ -1542,10 +1577,10 @@ TEST_F(CollectionJoinTest, CascadeDeletion) {
res_obj = nlohmann::json::parse(json_res);
ASSERT_EQ(1, res_obj["found"].get<size_t>());
ASSERT_EQ("product_b", res_obj["hits"][0]["document"].at("product_id"));
ASSERT_EQ("product_b", res_obj["hits"][0]["document"].at("product_idx"));
req_params = {
{"collection", "Customers"},
{"collection", "CustomerProductPrices"},
{"q", "*"},
};
search_op = collectionManager.do_search(req_params, embedded_params, json_res, now_ts);
@ -1554,4 +1589,27 @@ TEST_F(CollectionJoinTest, CascadeDeletion) {
ASSERT_EQ(2, res_obj["found"].get<size_t>());
ASSERT_EQ("product_b", res_obj["hits"][0]["document"].at("product_id"));
ASSERT_EQ("product_b", res_obj["hits"][1]["document"].at("product_id"));
collectionManager.get_collection_unsafe("Users")->remove("1");
req_params = {
{"collection", "Users"},
{"q", "*"},
};
search_op = collectionManager.do_search(req_params, embedded_params, json_res, now_ts);
res_obj = nlohmann::json::parse(json_res);
ASSERT_EQ(1, res_obj["found"].get<size_t>());
ASSERT_EQ("user_a", res_obj["hits"][0]["document"].at("user_id"));
req_params = {
{"collection", "CustomerProductPrices"},
{"q", "*"},
};
search_op = collectionManager.do_search(req_params, embedded_params, json_res, now_ts);
res_obj = nlohmann::json::parse(json_res);
ASSERT_EQ(1, res_obj["found"].get<size_t>());
ASSERT_EQ("product_b", res_obj["hits"][0]["document"].at("product_id"));
ASSERT_EQ("user_a", res_obj["hits"][0]["document"].at("user_id"));
}