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
Fix `sort_by` parsing bug. (#1733)
Harpreet Sangar52b9d5a95942
diff --git a/src/collection_manager.cpp b/src/collection_manager.cppindex 4c5af903..ce205811 100644--- a/src/collection_manager.cpp+++ b/src/collection_manager.cpp@@ -924,6 +924,11 @@ bool CollectionManager::parse_sort_by_str(std::string sort_by_str, std::vector<ssort_fields.emplace_back(sort_field_expr, "");sort_field_expr = "";++ // Skip the space in between the sort_by expressions.+ while (i + 1 < sort_by_str.size() && sort_by_str[i + 1] == ' ') {+ i++;+ }continue;} else if (sort_by_str.substr(i, 5) == sort_field_const::eval) {// Optional filtering@@ -940,6 +945,11 @@ bool CollectionManager::parse_sort_by_str(std::string sort_by_str, std::vector<sif (!result) {return false;}++ // Skip the space in between the sort_by expressions.+ while (i + 1 < sort_by_str.size() && sort_by_str[i + 1] == ' ') {+ i++;+ }continue;}}@@ -972,6 +982,11 @@ bool CollectionManager::parse_sort_by_str(std::string sort_by_str, std::vector<ssort_fields.emplace_back(field_name, order_str);sort_field_expr = "";++ // Skip the space in between the sort_by expressions.+ while (i + 1 < sort_by_str.size() && sort_by_str[i + 1] == ' ') {+ i++;+ }} else {sort_field_expr += sort_by_str[i];}diff --git a/test/collection_join_test.cpp b/test/collection_join_test.cppindex 8b3fefeb..d77ce55f 100644--- a/test/collection_join_test.cpp+++ b/test/collection_join_test.cpp@@ -4557,7 +4557,7 @@ TEST_F(CollectionJoinTest, SortByReference) {{"q", "*"},{"query_by", "product_name"},{"filter_by", "$Customers(customer_id: customer_a)"},- {"sort_by", "$Customers(_eval(product_location:(48.87709, 2.33495, 1km)):desc)"}, // Closer to product_a+ {"sort_by", "_eval(id:!foo):desc, $Customers(_eval(product_location:(48.87709, 2.33495, 1km)):desc)"}, // Closer to product_a{"include_fields", "product_id, $Customers(product_price, strategy:merge)"},};search_op = collectionManager.do_search(req_params, embedded_params, json_res, now_ts);diff --git a/test/collection_manager_test.cpp b/test/collection_manager_test.cppindex d01e5dfd..4d3265d9 100644--- a/test/collection_manager_test.cpp+++ b/test/collection_manager_test.cpp@@ -1268,7 +1268,7 @@ TEST_F(CollectionManagerTest, ParseSortByClause) {ASSERT_EQ("DESC", sort_fields[0].order);sort_fields.clear();- sort_by_parsed = CollectionManager::parse_sort_by_str("points:desc,loc(24.56,10.45):ASC,"+ sort_by_parsed = CollectionManager::parse_sort_by_str("points:desc, loc(24.56,10.45):ASC, ""$Customers(product_price:DESC)", sort_fields);ASSERT_TRUE(sort_by_parsed);ASSERT_EQ(3, sort_fields.size());@@ -1278,6 +1278,27 @@ TEST_F(CollectionManagerTest, ParseSortByClause) {ASSERT_EQ("ASC", sort_fields[1].order);ASSERT_EQ("$Customers(product_price:DESC)", sort_fields[2].name);+ sort_fields.clear();+ sort_by_parsed = CollectionManager::parse_sort_by_str("_eval(brand:nike && foo:bar):DESC, "+ "$Customers(product_price:DESC)", sort_fields);+ ASSERT_TRUE(sort_by_parsed);+ ASSERT_EQ(2, sort_fields.size());+ ASSERT_EQ("_eval", sort_fields[0].name);+ ASSERT_FALSE(sort_fields[0].eval_expressions.empty());+ ASSERT_EQ("brand:nike && foo:bar", sort_fields[0].eval_expressions[0]);+ ASSERT_EQ(1, sort_fields[0].eval.scores.size());+ ASSERT_EQ(1, sort_fields[0].eval.scores[0]);+ ASSERT_EQ("DESC", sort_fields[0].order);+ ASSERT_EQ("$Customers(product_price:DESC)", sort_fields[1].name);++ sort_fields.clear();+ sort_by_parsed = CollectionManager::parse_sort_by_str("$foo(bar:ASC), "+ "$Customers(product_price:DESC)", sort_fields);+ ASSERT_TRUE(sort_by_parsed);+ ASSERT_EQ(2, sort_fields.size());+ ASSERT_EQ("$foo(bar:ASC)", sort_fields[0].name);+ ASSERT_EQ("$Customers(product_price:DESC)", sort_fields[1].name);+sort_fields.clear();sort_by_parsed = CollectionManager::parse_sort_by_str("$foo( _eval(brand:nike && foo:bar):DESC,points:desc) ",sort_fields);