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
sort by other field
krunal46796826c683
diff --git a/include/field.h b/include/field.hindex 23341921..50a133ba 100644--- a/include/field.h+++ b/include/field.h@@ -577,6 +577,8 @@ struct facet_count_t {// used to fetch the actual document and value for representationuint32_t doc_id = 0;uint32_t array_pos = 0;+ //for sorting based on other field+ int64_t sort_field_val;};struct facet_stats_t {@@ -615,6 +617,8 @@ struct facet {std::string sort_order="";+ std::string sort_field="";+bool get_range(int64_t key, std::pair<int64_t, std::string>& range_pair) {if(facet_range_map.empty()) {LOG (ERROR) << "Facet range is not defined!!!";@@ -632,9 +636,11 @@ struct facet {}explicit facet(const std::string& field_name, std::map<int64_t, std::string> facet_range = {},- bool is_range_q = false, bool sort_by_alpha=false, const std::string& order="")- :field_name(field_name), facet_range_map(facet_range),- is_range_query(is_range_q), is_sort_by_alpha(sort_by_alpha), sort_order(order) {+ bool is_range_q = false, bool sort_by_alpha=false, const std::string& order="",+ const std::string& sort_by_field="")+ : field_name(field_name), facet_range_map(facet_range),+ is_range_query(is_range_q), is_sort_by_alpha(sort_by_alpha), sort_order(order),+ sort_field(sort_by_field) {}};@@ -657,6 +663,7 @@ struct facet_value_t {std::string value;std::string highlighted;uint32_t count;+ int64_t sort_field_val;};struct facet_hash_values_t {diff --git a/src/collection.cpp b/src/collection.cppindex ca6ce93a..ddfaf34a 100644--- a/src/collection.cpp+++ b/src/collection.cpp@@ -2028,6 +2028,7 @@ Option<nlohmann::json> Collection::search(std::string raw_query,for (const auto & kv : a_facet.result_map) {facet_count_t v = kv.second;v.fhash = kv.first;+ v.sort_field_val = kv.second.sort_field_val;facet_counts.emplace_back(v);}@@ -2159,7 +2160,7 @@ Option<nlohmann::json> Collection::search(std::string raw_query,highlightedss << value[i];i++;}- facet_value_t facet_value = {value, highlightedss.str(), facet_count.count};+ facet_value_t facet_value = {value, highlightedss.str(), facet_count.count, facet_count.sort_field_val};facet_values.emplace_back(facet_value);}}@@ -2174,6 +2175,16 @@ Option<nlohmann::json> Collection::search(std::string raw_query,return fv1.value > fv2.value;});+ } else if(!a_facet.sort_field.empty()) {+ bool is_asc = a_facet.sort_order == "asc";+ std::stable_sort(facet_values.begin(), facet_values.end(),+ [&] (const auto& fv1, const auto& fv2) {+ if(is_asc) {+ return fv1.sort_field_val < fv2.sort_field_val;+ }++ return fv1.sort_field_val > fv2.sort_field_val;+ });} else {std::stable_sort(facet_values.begin(), facet_values.end(), Collection::facet_count_str_compare);}@@ -5009,6 +5020,7 @@ Option<bool> Collection::parse_facet(const std::string& facet_field, std::vector// normal facetstd::string order = "";bool sort_alpha = false;+ std::string sort_field = "";std::string facet_field_copy = facet_field;auto pos = facet_field_copy.find("(");if(pos != std::string::npos) {@@ -5020,24 +5032,41 @@ Option<bool> Collection::parse_facet(const std::string& facet_field, std::vectorreturn Option<bool>(404, error);}- const field &a_field = search_schema.at(facet_field_copy);-if (facet_field.find("sort") != std::string::npos) { //sort params are supplied with facet- if(!a_field.is_string()) {- std::string error = "Facet field should be string type to apply alpha sort.";- return Option<bool>(400, error);+ pos = facet_field.find("sort_field");+ if(pos == std::string::npos) { //alpha sort+ const field &a_field = search_schema.at(facet_field_copy);+ if (!a_field.is_string()) {+ std::string error = "Facet field should be string type to apply alpha sort.";+ return Option<bool>(400, error);+ }+ sort_alpha = true;+ } else { //sort_field based sort+ auto sort_field_fixed_len = strlen("sort_field:");+ auto sort_field_len = facet_field.size() - pos - sort_field_fixed_len - 1;+ sort_field = facet_field.substr(pos + sort_field_fixed_len, sort_field_len);++ if (search_schema.count(sort_field) == 0 || !search_schema.at(sort_field).facet) {+ std::string error = "Could not find a facet field named `" + sort_field + "` in the schema.";+ return Option<bool>(404, error);+ }++ const field &a_field = search_schema.at(sort_field);+ if (a_field.is_string()) {+ std::string error = "Sort field should be non string type to apply sort.";+ return Option<bool>(400, error);+ }}if (facet_field.find("asc") != std::string::npos) {order = "asc";- sort_alpha = true;} else if (facet_field.find("desc") != std::string::npos) {order = "desc";- sort_alpha = true;}}- facets.emplace_back(facet(facet_field_copy, {}, false, sort_alpha, order));+ facets.emplace_back(facet(facet_field_copy, {}, false, sort_alpha,+ order, sort_field));}return Option<bool>(true);diff --git a/src/index.cpp b/src/index.cppindex 567c4bba..369cc75f 100644--- a/src/index.cpp+++ b/src/index.cpp@@ -1278,32 +1278,20 @@ void Index::do_facets(std::vector<facet> & facets, facet_query_t & facet_query,bool is_wildcard_no_filter_query = is_wildcard_query && no_filters_provided;bool facet_value_index_exists = facet_index_v4->has_value_index(facet_field.name);- if(a_facet.is_sort_by_alpha) {- a_facet.is_intersected = true;-- std::map<std::string, uint32_t> facet_results;-- facet_index_v4->intersect(facet_field.name, result_ids,- results_size, max_facet_count, facet_results, is_wildcard_no_filter_query,- a_facet.sort_order);-- for(const auto& kv : facet_results) {- facet_count_t &facet_count = a_facet.value_result_map[kv.first];- facet_count.count = kv.second;- }-#ifdef TEST_BUILD- } else if(facet_index_type == VALUE) {+ if(facet_index_type == VALUE) {#else- } else if(facet_value_index_exists && use_value_index) {+ if(facet_value_index_exists && use_value_index) {#endif// LOG(INFO) << "Using intersection to find facets";a_facet.is_intersected = true;std::map<std::string, uint32_t> facet_results;+ std::string sort_order = a_facet.is_sort_by_alpha ? a_facet.sort_order : "";facet_index_v4->intersect(facet_field.name, result_ids,- results_size, max_facet_count, facet_results, is_wildcard_no_filter_query);+ results_size, max_facet_count, facet_results,+ is_wildcard_no_filter_query, sort_order);for(const auto& kv : facet_results) {//range facet processing@@ -1423,6 +1411,11 @@ void Index::do_facets(std::vector<facet> & facets, facet_query_t & facet_query,//LOG (INFO) << "adding hash tokens for hash " << fhash;a_facet.hash_tokens[fhash] = fquery_hashes.at(fhash);}+ if(!a_facet.sort_field.empty()) {+ sort_index_it = sort_index.find(a_facet.sort_field);+ facet_count.sort_field_val = get_doc_val_from_sort_index(sort_index_it, doc_seq_id);+ //LOG(INFO) << "found sort_field val " << facet_count.sort_field;+ }}}}@@ -2814,7 +2807,8 @@ Option<bool> Index::search(std::vector<query_tokens_t>& field_query_tokens, consfor(size_t i = 0; i < num_threads; i++) {for(const auto& this_facet: facets) {facet_batches[i].emplace_back(facet(this_facet.field_name, this_facet.facet_range_map,- this_facet.is_range_query));+ this_facet.is_range_query, this_facet.is_sort_by_alpha, this_facet.sort_order,+ this_facet.sort_field));}}@@ -2870,6 +2864,12 @@ Option<bool> Index::search(std::vector<query_tokens_t>& field_query_tokens, consfor(size_t fi = 0; fi < facet_batch.size(); fi++) {auto& this_facet = facet_batch[fi];auto& acc_facet = facets[fi];++ acc_facet.is_intersected = this_facet.is_intersected;+ acc_facet.is_sort_by_alpha = this_facet.is_sort_by_alpha;+ acc_facet.sort_order = this_facet.sort_order;+ acc_facet.sort_field = this_facet.sort_field;+for(auto & facet_kv: this_facet.result_map) {uint32_t fhash = 0;if(group_limit) {@@ -2892,7 +2892,7 @@ Option<bool> Index::search(std::vector<query_tokens_t>& field_query_tokens, consacc_facet.result_map[facet_kv.first].doc_id = facet_kv.second.doc_id;acc_facet.result_map[facet_kv.first].array_pos = facet_kv.second.array_pos;- acc_facet.is_intersected = this_facet.is_intersected;+ acc_facet.result_map[facet_kv.first].sort_field_val = facet_kv.second.sort_field_val;acc_facet.hash_tokens[facet_kv.first] = this_facet.hash_tokens[facet_kv.first];}@@ -2910,7 +2910,6 @@ Option<bool> Index::search(std::vector<query_tokens_t>& field_query_tokens, consacc_facet.value_result_map[facet_kv.first].doc_id = facet_kv.second.doc_id;acc_facet.value_result_map[facet_kv.first].array_pos = facet_kv.second.array_pos;- acc_facet.is_intersected = this_facet.is_intersected;acc_facet.fvalue_tokens[facet_kv.first] = this_facet.fvalue_tokens[facet_kv.first];}@@ -4404,9 +4403,11 @@ void Index::compute_facet_infos(const std::vector<facet>& facets, facet_query_t&facet_field.type != field_types::INT64_ARRAY);size_t num_facet_values = facet_index_v4->get_facet_count(facet_field.name);- facet_infos[findex].use_value_index = (group_limit == 0) && ( is_wildcard_no_filter_query ||- (all_result_ids_len > 1000 && num_facet_values < 250) ||- (all_result_ids_len > 1000 && all_result_ids_len * 2 > total_docs));+ facet_infos[findex].use_value_index = (group_limit == 0) && (a_facet.sort_field.empty()) &&+ ( is_wildcard_no_filter_query ||+ (all_result_ids_len > 1000 && num_facet_values < 250) ||+ (all_result_ids_len > 1000 && all_result_ids_len * 2 > total_docs) ||+ (a_facet.is_sort_by_alpha));bool facet_value_index_exists = facet_index_v4->has_value_index(facet_field.name);diff --git a/test/collection_faceting_test.cpp b/test/collection_faceting_test.cppindex 426f1d73..c4bd83b6 100644--- a/test/collection_faceting_test.cpp+++ b/test/collection_faceting_test.cpp@@ -2098,4 +2098,118 @@ TEST_F(CollectionFacetingTest, FacetSortByAlpha) {ASSERT_EQ(400, search_op.code());ASSERT_EQ("Facet field should be string type to apply alpha sort.", search_op.error());+}++TEST_F(CollectionFacetingTest, FacetSortByOtherField) {+ nlohmann::json schema = R"({+ "name": "coll1",+ "enable_nested_fields": true,+ "fields": [+ {"name": "receipe", "type": "object", "optional": false, "facet": true }+ ]+ })"_json;++ auto op = collectionManager.create_collection(schema);+ ASSERT_TRUE(op.ok());+ Collection* coll1 = op.get();++ nlohmann::json doc1 = R"({+ "receipe": {+ "name": "cheese pizza",+ "calories": 300,+ "origin": "america"+ }+ })"_json;++ auto add_op = coll1->add(doc1.dump(), CREATE);+ ASSERT_TRUE(add_op.ok());++ nlohmann::json doc2 = R"({+ "receipe": {+ "name": "noodles",+ "calories": 250,+ "origin": "china"+ }+ })"_json;++ add_op = coll1->add(doc2.dump(), CREATE);+ ASSERT_TRUE(add_op.ok());++ nlohmann::json doc3 = R"({+ "receipe": {+ "name": "hamburger",+ "calories": 350,+ "origin": "america"+ }+ })"_json;++ add_op = coll1->add(doc3.dump(), CREATE);+ ASSERT_TRUE(add_op.ok());++ nlohmann::json doc4 = R"({+ "receipe": {+ "name": "schezwan rice",+ "calories": 150,+ "origin": "china"+ }+ })"_json;++ add_op = coll1->add(doc4.dump(), CREATE);+ ASSERT_TRUE(add_op.ok());++ nlohmann::json doc5 = R"({+ "receipe": {+ "name": "butter chicken",+ "calories": 270,+ "origin": "india"+ }+ })"_json;++ add_op = coll1->add(doc5.dump(), CREATE);+ ASSERT_TRUE(add_op.ok());++ //search by calories in asc order+ auto search_op = coll1->search("*", {},"",+ {"receipe.name(sort:asc, sort_field:receipe.calories)"},+ {}, {2});++ if(!search_op.ok()) {+ LOG(ERROR) << search_op.error();+ FAIL();+ }+ auto results = search_op.get();++ ASSERT_EQ(1, results["facet_counts"].size());+ ASSERT_EQ(5, results["facet_counts"][0]["counts"].size());+ ASSERT_EQ("schezwan rice", results["facet_counts"][0]["counts"][0]["value"]);+ ASSERT_EQ("noodles", results["facet_counts"][0]["counts"][1]["value"]);+ ASSERT_EQ("butter chicken", results["facet_counts"][0]["counts"][2]["value"]);+ ASSERT_EQ("cheese pizza", results["facet_counts"][0]["counts"][3]["value"]);+ ASSERT_EQ("hamburger", results["facet_counts"][0]["counts"][4]["value"]);++ //search by calories in desc order+ search_op = coll1->search("*", {},"",+ {"receipe.name(sort:desc, sort_field:receipe.calories)"},+ {}, {2});++ if(!search_op.ok()) {+ LOG(ERROR) << search_op.error();+ FAIL();+ }+ results = search_op.get();++ ASSERT_EQ(1, results["facet_counts"].size());+ ASSERT_EQ(5, results["facet_counts"][0]["counts"].size());+ ASSERT_EQ("hamburger", results["facet_counts"][0]["counts"][0]["value"]);+ ASSERT_EQ("cheese pizza", results["facet_counts"][0]["counts"][1]["value"]);+ ASSERT_EQ("butter chicken", results["facet_counts"][0]["counts"][2]["value"]);+ ASSERT_EQ("noodles", results["facet_counts"][0]["counts"][3]["value"]);+ ASSERT_EQ("schezwan rice", results["facet_counts"][0]["counts"][4]["value"]);++ //try sort by stirng field+ search_op = coll1->search("*", {}, "", {"receipe.name(sort:desc, sort_field:receipe.origin)"},+ {}, {2});++ ASSERT_EQ(400, search_op.code());+ ASSERT_EQ("Sort field should be non string type to apply sort.", search_op.error());}\ No newline at end of file