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
making sorting more generic
krunal1313de5a7d08a279
diff --git a/src/collection.cpp b/src/collection.cppindex a6ab6caf..7f4aed73 100644--- a/src/collection.cpp+++ b/src/collection.cpp@@ -2333,10 +2333,32 @@ void Collection::populate_result_kvs(Topster *topster, std::vector<std::vector<K// we have to pick top-K groupsTopster gtopster(topster->MAX_SIZE);+ int group_count_index = -1;+ int group_sort_order = 1;++ for(int i = 0; i < sort_by_fields.size(); ++i) {+ if(sort_by_fields[i].name == sort_field_const::group_count) {+ group_count_index = i;++ if(sort_by_fields[i].order == sort_field_const::asc) {+ group_sort_order *= -1;+ }++ break;+ }+ }+for(auto& group_topster: topster->group_kv_map) {group_topster.second->sort();if(group_topster.second->size != 0) {KV* kv_head = group_topster.second->getKV(0);++ if(group_count_index >= 0) {+ const auto& itr = groups_processed.find(kv_head->distinct_key);+ if(itr != groups_processed.end()) {+ kv_head->scores[0] = itr->second * group_sort_order;+ }+ }gtopster.add(kv_head);}}@@ -2351,25 +2373,6 @@ void Collection::populate_result_kvs(Topster *topster, std::vector<std::vector<K);result_kvs.emplace_back(group_kvs);}-- if(!sort_by_fields.empty() && sort_by_fields[0].name == sort_field_const::group_count) {- std::sort(result_kvs.begin(), result_kvs.end(),- [&](const std::vector<KV*>& g1, const std::vector<KV*>& g2) {- const auto& it1 = groups_processed.find(g1[0]->distinct_key);- const auto& it2 = groups_processed.find(g2[0]->distinct_key);-- if(it1 != groups_processed.end() && it2 != groups_processed.end()) {- if(sort_by_fields[0].order == sort_field_const::asc) {- return it1->second < it2->second;- }- else {- return it1->second > it2->second;- }- }- return false;- });- }-} else {for(uint32_t t = 0; t < topster->size; t++) {KV* kv = topster->getKV(t);diff --git a/src/index.cpp b/src/index.cppindex fd24b481..e4e09ed3 100644--- a/src/index.cpp+++ b/src/index.cpp@@ -3799,7 +3799,7 @@ void Index::compute_sort_scores(const std::vector<sort_by>& sort_fields, const iconst int64_t default_score = INT64_MIN; // to handle field that doesn't exist in document (e.g. optional)// avoiding loop- if (sort_fields.size() > 0 && sort_fields[0].name != sort_field_const::group_count) {+ if (sort_fields.size() > 0) {if (field_values[0] == &text_match_sentinel_value) {scores[0] = int64_t(max_field_match_score);match_score_index = 0;@@ -3855,7 +3855,7 @@ void Index::compute_sort_scores(const std::vector<sort_by>& sort_fields, const i}}- if(sort_fields.size() > 1 && sort_fields[1].name != sort_field_const::group_count) {+ if(sort_fields.size() > 1) {if (field_values[1] == &text_match_sentinel_value) {scores[1] = int64_t(max_field_match_score);match_score_index = 1;@@ -3907,7 +3907,7 @@ void Index::compute_sort_scores(const std::vector<sort_by>& sort_fields, const i}}- if(sort_fields.size() > 2 && sort_fields[2].name != sort_field_const::group_count) {+ if(sort_fields.size() > 2) {if (field_values[2] == &text_match_sentinel_value) {scores[2] = int64_t(max_field_match_score);match_score_index = 2;@@ -4569,7 +4569,8 @@ void Index::populate_sort_mapping(int* sort_order, std::vector<size_t>& geopointif (sort_fields_std[i].name == sort_field_const::text_match) {field_values[i] = &text_match_sentinel_value;- } else if (sort_fields_std[i].name == sort_field_const::seq_id) {+ } else if (sort_fields_std[i].name == sort_field_const::seq_id ||+ sort_fields_std[i].name == sort_field_const::group_count) {field_values[i] = &seq_id_sentinel_value;} else if (sort_fields_std[i].name == sort_field_const::eval) {field_values[i] = &eval_sentinel_value;@@ -5038,8 +5039,7 @@ void Index::score_results(const std::vector<sort_by> & sort_fields, const uint16// avoiding loopif (sort_fields.size() > 0) {- if (field_values[0] == &text_match_sentinel_value- && sort_fields[0].name != sort_field_const::group_count) {+ if (field_values[0] == &text_match_sentinel_value) {scores[0] = int64_t(match_score);match_score_index = 0;} else if (field_values[0] == &seq_id_sentinel_value) {@@ -5058,7 +5058,7 @@ void Index::score_results(const std::vector<sort_by> & sort_fields, const uint16}}- if(sort_fields.size() > 1 && sort_fields[1].name != sort_field_const::group_count) {+ if(sort_fields.size() > 1) {if (field_values[1] == &text_match_sentinel_value) {scores[1] = int64_t(match_score);match_score_index = 1;@@ -5078,7 +5078,7 @@ void Index::score_results(const std::vector<sort_by> & sort_fields, const uint16}}- if(sort_fields.size() > 2 && sort_fields[2].name != sort_field_const::group_count) {+ if(sort_fields.size() > 2) {if (field_values[2] == &text_match_sentinel_value) {scores[2] = int64_t(match_score);match_score_index = 2;