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
C++ algoliadatastoreelasticsearchenterprise-searchfacetingfull-text-searchfuzzy-searchgeosearchin-memoryinstantsearchmerchandisingpineconesearchsearch-enginesemantic-searchsimilarity-searchsite-searchsynonymstypo-tolerancevector-search
Add guards for filter node.
Kishore Nallan26e3407d1dc4
diff --git a/src/collection.cpp b/src/collection.cppindex 35a7a697..e0087da6 100644--- a/src/collection.cpp+++ b/src/collection.cpp@@ -1035,6 +1035,8 @@ Option<nlohmann::json> Collection::search(const std::string & raw_query,filter_node_t* filter_tree_root = nullptr;Option<bool> parse_filter_op = filter::parse_filter_query(filter_query, search_schema,store, doc_id_prefix, filter_tree_root);+ std::unique_ptr<filter_node_t> filter_tree_root_guard(filter_tree_root);+if(!parse_filter_op.ok()) {return Option<nlohmann::json>(parse_filter_op.code(), parse_filter_op.error());}@@ -1277,6 +1279,7 @@ Option<nlohmann::json> Collection::search(const std::string & raw_query,min_len_1typo, min_len_2typo, max_candidates, infixes,max_extra_prefix, max_extra_suffix, facet_query_num_typos,filter_curated_hits, split_join_tokens, vector_query);+ std::unique_ptr<search_args> search_params_guard(search_params);index->run_search(search_params);@@ -1804,11 +1807,6 @@ Option<nlohmann::json> Collection::search(const std::string & raw_query,result["facet_counts"].push_back(facet_result);}- // free search params- delete search_params;-- delete filter_tree_root;-result["search_cutoff"] = search_cutoff;result["request_params"] = nlohmann::json::object();diff --git a/src/field.cpp b/src/field.cppindex 18ac472a..ba9c67f2 100644--- a/src/field.cpp+++ b/src/field.cpp@@ -372,7 +372,7 @@ Option<bool> toParseTree(std::queue<std::string>& postfix, filter_node_t*& root,const std::string expression = postfix.front();postfix.pop();- filter_node_t* filter_node;+ filter_node_t* filter_node = nullptr;if (isOperator(expression)) {auto message = "Could not parse the filter query: unbalanced `" + expression + "` operands.";@@ -383,6 +383,7 @@ Option<bool> toParseTree(std::queue<std::string>& postfix, filter_node_t*& root,nodeStack.pop();if (nodeStack.empty()) {+ delete operandB;return Option<bool>(400, message);}auto operandA = nodeStack.top();@@ -393,6 +394,11 @@ Option<bool> toParseTree(std::queue<std::string>& postfix, filter_node_t*& root,filter filter_exp;Option<bool> toFilter_op = toFilter(expression, filter_exp, search_schema, store, doc_id_prefix);if (!toFilter_op.ok()) {+ while(!nodeStack.empty()) {+ auto filterNode = nodeStack.top();+ delete filterNode;+ nodeStack.pop();+ }return toFilter_op;}diff --git a/test/collection_filtering_test.cpp b/test/collection_filtering_test.cppindex e8587544..c28ab159 100644--- a/test/collection_filtering_test.cpp+++ b/test/collection_filtering_test.cpp@@ -353,6 +353,16 @@ TEST_F(CollectionFilteringTest, HandleBadlyFormedFilterQuery) {nlohmann::json results = coll_array_fields->search("Jeremy", query_fields, "tagzz: gold", facets, sort_fields, {0}, 10, 1, FREQUENCY, {false}).get();ASSERT_EQ(0, results["hits"].size());+ // compound filter expression containing an unknown field+ results = coll_array_fields->search("Jeremy", query_fields,+ "(age:>0 || timestamps:> 0) || tagzz: gold", facets, sort_fields, {0}, 10, 1, FREQUENCY, {false}).get();+ ASSERT_EQ(0, results["hits"].size());++ // unbalanced paranthesis+ results = coll_array_fields->search("Jeremy", query_fields,+ "(age:>0 || timestamps:> 0) || ", facets, sort_fields, {0}, 10, 1, FREQUENCY, {false}).get();+ ASSERT_EQ(0, results["hits"].size());+// searching using a string for a numeric fieldresults = coll_array_fields->search("Jeremy", query_fields, "age: abcdef", facets, sort_fields, {0}, 10, 1, FREQUENCY, {false}).get();ASSERT_EQ(0, results["hits"].size());