From 13b7d8ed71bb219473f3babf8f704ca725ad2032 Mon Sep 17 00:00:00 2001 From: Harpreet Sangar Date: Mon, 25 Sep 2023 13:21:53 +0530 Subject: [PATCH] Support join on bases of query match with `$foo(id: *)`. --- src/filter.cpp | 6 +++++- src/filter_result_iterator.cpp | 3 ++- test/collection_filtering_test.cpp | 7 +++++++ test/collection_join_test.cpp | 15 +++++++++++++++ 4 files changed, 29 insertions(+), 2 deletions(-) diff --git a/src/filter.cpp b/src/filter.cpp index 10354ea0..6415500d 100644 --- a/src/filter.cpp +++ b/src/filter.cpp @@ -418,7 +418,11 @@ Option toFilter(const std::string expression, filter_exp = {field_name, {}, {}}; NUM_COMPARATOR id_comparator = EQUALS; size_t filter_value_index = 0; - if (raw_value[0] == '=') { + if (raw_value == "*") { // Match all. + // NOT_EQUALS comparator with no id match will get all the ids. + id_comparator = NOT_EQUALS; + filter_exp.apply_not_equals = true; + } else if (raw_value[0] == '=') { id_comparator = EQUALS; while (++filter_value_index < raw_value.size() && raw_value[filter_value_index] == ' '); } else if (raw_value.size() >= 2 && raw_value[0] == '!' && raw_value[1] == '=') { diff --git a/src/filter_result_iterator.cpp b/src/filter_result_iterator.cpp index 7f4a45b9..3eff372e 100644 --- a/src/filter_result_iterator.cpp +++ b/src/filter_result_iterator.cpp @@ -659,7 +659,8 @@ void filter_result_iterator_t::init() { values.push_back(std::to_string(result.docs[i])); } - filter filter_exp = {get_reference_field_op.get(), std::move(values), {EQUALS}}; + filter filter_exp = {get_reference_field_op.get(), std::move(values), + std::vector(result.count, EQUALS)}; auto filter_tree_root = new filter_node_t(filter_exp); std::unique_ptr filter_tree_root_guard(filter_tree_root); diff --git a/test/collection_filtering_test.cpp b/test/collection_filtering_test.cpp index e82d083a..e572c32f 100644 --- a/test/collection_filtering_test.cpp +++ b/test/collection_filtering_test.cpp @@ -1327,6 +1327,13 @@ TEST_F(CollectionFilteringTest, FilteringViaDocumentIds) { ASSERT_EQ(0, results["found"].get()); + // match all IDs + results = coll1->search("*", + {}, "id: *", + {}, sort_fields, {0}, 10, 1, FREQUENCY, {true}).get(); + + ASSERT_EQ(4, results["found"].get()); + collectionManager.drop_collection("coll1"); } diff --git a/test/collection_join_test.cpp b/test/collection_join_test.cpp index 495f9ba4..0494e2c6 100644 --- a/test/collection_join_test.cpp +++ b/test/collection_join_test.cpp @@ -482,6 +482,21 @@ TEST_F(CollectionJoinTest, FilterByReference_SingleMatch) { ASSERT_EQ(1, res_obj["hits"].size()); ASSERT_EQ("soap", result["hits"][0]["document"]["product_name"].get()); + req_params = { + {"collection", "Customers"}, + {"q", "Dan"}, + {"query_by", "customer_name"}, + {"filter_by", "$Products(id:*) && product_price:<100"}, + }; + + search_op_bool = collectionManager.do_search(req_params, embedded_params, json_res, now_ts); + ASSERT_TRUE(search_op_bool.ok()); + + res_obj = nlohmann::json::parse(json_res); + ASSERT_EQ(1, res_obj["found"].get()); + ASSERT_EQ(1, res_obj["hits"].size()); + ASSERT_EQ("soap", result["hits"][0]["document"]["product_name"].get()); + collectionManager.drop_collection("Customers"); collectionManager.drop_collection("Products"); }