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

Fix query_by crash.

Harpreet Sangar0726c178044c
diff --git a/src/collection.cpp b/src/collection.cpp
index 3e93af08..b43f5218 100644
--- a/src/collection.cpp
+++ b/src/collection.cpp
@@ -1806,7 +1806,8 @@ Option<nlohmann::json> Collection::search(std::string raw_query,
// `id` field needs to be handled separately, we will not handle for now
std::string error = "Cannot use `id` as a query by field.";
return Option<nlohmann::json>(400, error);
- } else if (field_name[0] == '$') {
+ } else if (field_name[0] == '$' && field_name.find('(') != std::string::npos &&
+ field_name.find(')') != std::string::npos) {
return Option<nlohmann::json>(400, "Query by reference is not yet supported.");
}
@@ -1817,6 +1818,9 @@ Option<nlohmann::json> Collection::search(std::string raw_query,
}
for(const auto& expanded_search_field: expanded_search_fields) {
+ if (search_schema.count(expanded_search_field) == 0) {
+ return Option<nlohmann::json>(400, "Could not find `" + expanded_search_field + "` field in the schema.");
+ }
auto search_field = search_schema.at(expanded_search_field);
if(search_field.num_dim > 0) {
diff --git a/test/collection_join_test.cpp b/test/collection_join_test.cpp
index 0babc851..f05d99c2 100644
--- a/test/collection_join_test.cpp
+++ b/test/collection_join_test.cpp
@@ -4980,4 +4980,13 @@ TEST_F(CollectionJoinTest, QueryByReference) {
auto search_op = collectionManager.do_search(req_params, embedded_params, json_res, now_ts);
ASSERT_FALSE(search_op.ok());
ASSERT_EQ("Query by reference is not yet supported.", search_op.error());
+
+ req_params = {
+ {"collection", "Products"},
+ {"q", "*"},
+ {"query_by", "$Customers(customer_name"}
+ };
+ search_op = collectionManager.do_search(req_params, embedded_params, json_res, now_ts);
+ ASSERT_FALSE(search_op.ok());
+ ASSERT_EQ("Could not find `$Customers(customer_name` field in the schema.", search_op.error());
}
\ No newline at end of file