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
merge v0.26-facets
krunala282fbd1a8a3
diff --cc include/field.hindex 46a6c237,a051c7ee..23341921--- a/include/field.h+++ b/include/field.h@@@ -604,20 -611,14 +611,18 @@@ struct facetbool is_intersected = false;+ bool is_sort_by_alpha = false;++ std::string sort_order="";+- bool get_range(std::string key, std::pair<std::string, std::string>& range_pair)- {- if(facet_range_map.empty())- {+ 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!!!";}+auto it = facet_range_map.lower_bound(key);- if(it != facet_range_map.end())- {+ if(it != facet_range_map.end()) {range_pair.first = it->first;range_pair.second = it->second;return true;@@@ -626,14 -627,9 +631,10 @@@return false;}- explicit facet(const std::string& field_name,- std::map<std::string, 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;+ explicit facet(const std::string& field_name, std::map<int64_t, std::string> facet_range = {},- bool is_range_q = false) :field_name(field_name), facet_range_map(facet_range),- is_range_query(is_range_q) {++ 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) {}};diff --cc src/collection_manager.cppindex 9bd3c09a,7791b2a9..8c0afc8b--- a/src/collection_manager.cpp+++ b/src/collection_manager.cpp@@@ -1222,9 -1223,10 +1223,9 @@@ Option<bool> CollectionManager::do_searoffset,HASH,remote_embedding_timeout_ms,- remote_embedding_num_try,+ remote_embedding_num_tries,stopwords_set,- facet_return_parent- );+ facet_return_parent);uint64_t timeMillis = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::high_resolution_clock::now() - begin).count();diff --cc src/index.cppindex b9c2aa93,db429001..567c4bba--- a/src/index.cpp+++ b/src/index.cpp@@@ -1275,33 -1278,10 +1278,24 @@@ void Index::do_facets(std::vector<facetbool 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);- // We have to choose between hash and value index:- // 1. Group queries -> requires hash index- // 2. Wildcard + no filters -> use value index- // 3. Very few unique facet values (< 250) -> use value index- // 4. Result match > 50%- bool use_value_index = (group_limit == 0) && ( is_wildcard_no_filter_query ||- (results_size > 1000 && num_facet_values < 250) ||- (results_size > 1000 && results_size * 2 > total_docs));-+ 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.result_map[kv.first];++ facet_count_t &facet_count = a_facet.value_result_map[kv.first];+ facet_count.count = kv.second;+ }+#ifdef TEST_BUILD- if(facet_index_type == VALUE) {+ } else if(facet_index_type == VALUE) {#else- if(facet_value_index_exists && use_value_index) {+ } else if(facet_value_index_exists && use_value_index) {#endif// LOG(INFO) << "Using intersection to find facets";a_facet.is_intersected = true;@@@ -2756,20 -2713,19 +2727,19 @@@ Option<bool> Index::search(std::vector<auto result = result_it->second;// old_score + (1 / rank_of_document) * WEIGHT)result->vector_distance = vec_result.second;- result->scores[result->match_score_index] = float_to_int64_t(+ result->text_match_score = result->scores[result->match_score_index];+ int64_t match_score = float_to_int64_t((int64_t_to_float(result->scores[result->match_score_index])) +((1.0 / (res_index + 1)) * VECTOR_SEARCH_WEIGHT));+ int64_t match_score_index = -1;+ int64_t scores[3] = {0};-+- for(size_t i = 0;i < 3; i++) {- if(field_values[i] == &vector_distance_sentinel_value) {- result->scores[i] = float_to_int64_t(vec_result.second);- }+ compute_sort_scores(sort_fields_std, sort_order, field_values, geopoint_indices, doc_id, 0, match_score, scores, match_score_index, vec_result.second);- if(sort_order[i] == -1) {- result->scores[i] = -result->scores[i];- }+ for(int i = 0; i < 3; i++) {+ result->scores[i] = scores[i];}-+ result->match_score_index = match_score_index;} else {// Result has been found only in vector search: we have to add it to both KV and result_ids// (1 / rank_of_document) * WEIGHT)diff --cc test/collection_faceting_test.cppindex dd36a0fa,a797532e..426f1d73--- a/test/collection_faceting_test.cpp+++ b/test/collection_faceting_test.cpp@@@ -1876,139 -1962,3 +1962,140 @@@ TEST_F(CollectionFacetingTest, FacetingASSERT_EQ("{\"b\":0,\"color\":\"red\",\"g\":0,\"r\":255}", results["facet_counts"][0]["counts"][0]["value"]);ASSERT_EQ("{\"b\":255,\"color\":\"blue\",\"g\":0,\"r\":0}", results["facet_counts"][0]["counts"][1]["value"]);}++++TEST_F(CollectionFacetingTest, FacetSortByAlpha) {+ nlohmann::json schema = R"({+ "name": "coll1",+ "fields": [+ {"name": "phone", "type": "string", "optional": false, "facet": true },+ {"name": "brand", "type": "string", "optional": false, "facet": true },+ {"name": "rating", "type": "float", "optional": false, "facet": true }+ ]+ })"_json;++ auto op = collectionManager.create_collection(schema);+ ASSERT_TRUE(op.ok());+ Collection *coll1 = op.get();++ nlohmann::json doc;++ doc["phone"] = "Oneplus 11R";+ doc["brand"] = "Oneplus";+ doc["rating"] = 4.6;+ auto add_op = coll1->add(doc.dump(), CREATE);+ ASSERT_TRUE(add_op.ok());++ doc["phone"] = "Fusion Plus";+ doc["brand"] = "Moto";+ doc["rating"] = 4.2;+ add_op = coll1->add(doc.dump(), CREATE);+ ASSERT_TRUE(add_op.ok());++ doc["phone"] = "S22 Ultra";+ doc["brand"] = "Samsung";+ doc["rating"] = 4.1;+ add_op = coll1->add(doc.dump(), CREATE);+ ASSERT_TRUE(add_op.ok());++ doc["phone"] = "GT Master";+ doc["brand"] = "Realme";+ doc["rating"] = 4.4;+ add_op = coll1->add(doc.dump(), CREATE);+ ASSERT_TRUE(add_op.ok());++ doc["phone"] = "T2";+ doc["brand"] = "Vivo";+ doc["rating"] = 4.0;+ add_op = coll1->add(doc.dump(), CREATE);+ ASSERT_TRUE(add_op.ok());++ doc["phone"] = "Mi 6";+ doc["brand"] = "Xiaomi";+ doc["rating"] = 3.9;+ add_op = coll1->add(doc.dump(), CREATE);+ ASSERT_TRUE(add_op.ok());++ doc["phone"] = "Z6 Lite";+ doc["brand"] = "Iqoo";+ doc["rating"] = 4.3;+ add_op = coll1->add(doc.dump(), CREATE);+ ASSERT_TRUE(add_op.ok());++ //sort facets by phone in asc order+ auto search_op = coll1->search("*", {}, "", {"phone(sort:asc)"},+ {}, {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(7, results["facet_counts"][0]["counts"].size());+ ASSERT_EQ("Fusion Plus", results["facet_counts"][0]["counts"][0]["value"]);+ ASSERT_EQ("GT Master", results["facet_counts"][0]["counts"][1]["value"]);+ ASSERT_EQ("Mi 6", results["facet_counts"][0]["counts"][2]["value"]);+ ASSERT_EQ("Oneplus 11R", results["facet_counts"][0]["counts"][3]["value"]);+ ASSERT_EQ("S22 Ultra", results["facet_counts"][0]["counts"][4]["value"]);+ ASSERT_EQ("T2", results["facet_counts"][0]["counts"][5]["value"]);+ ASSERT_EQ("Z6 Lite", results["facet_counts"][0]["counts"][6]["value"]);++ //sort facets by brand in desc order+ search_op = coll1->search("*", {}, "", {"brand(sort:desc)"},- {}, {2});++ {}, {2});++ if (!search_op.ok()) {+ LOG(ERROR) << search_op.error();+ FAIL();+ }++ results = search_op.get();+ ASSERT_EQ(1, results["facet_counts"].size());+ ASSERT_EQ(7, results["facet_counts"][0]["counts"].size());+ ASSERT_EQ("Xiaomi", results["facet_counts"][0]["counts"][0]["value"]);+ ASSERT_EQ("Vivo", results["facet_counts"][0]["counts"][1]["value"]);+ ASSERT_EQ("Samsung", results["facet_counts"][0]["counts"][2]["value"]);+ ASSERT_EQ("Realme", results["facet_counts"][0]["counts"][3]["value"]);+ ASSERT_EQ("Oneplus", results["facet_counts"][0]["counts"][4]["value"]);+ ASSERT_EQ("Moto", results["facet_counts"][0]["counts"][5]["value"]);+ ASSERT_EQ("Iqoo", results["facet_counts"][0]["counts"][6]["value"]);++ //sort facets by brand in desc order and phone by asc order+ search_op = coll1->search("*", {}, "", {"brand(sort:desc)", "phone(sort:asc)"},+ {}, {2});++ if (!search_op.ok()) {+ LOG(ERROR) << search_op.error();+ FAIL();+ }++ results = search_op.get();+ ASSERT_EQ(2, results["facet_counts"].size());++ ASSERT_EQ(7, results["facet_counts"][0]["counts"].size());+ ASSERT_EQ("Xiaomi", results["facet_counts"][0]["counts"][0]["value"]);+ ASSERT_EQ("Vivo", results["facet_counts"][0]["counts"][1]["value"]);+ ASSERT_EQ("Samsung", results["facet_counts"][0]["counts"][2]["value"]);+ ASSERT_EQ("Realme", results["facet_counts"][0]["counts"][3]["value"]);+ ASSERT_EQ("Oneplus", results["facet_counts"][0]["counts"][4]["value"]);+ ASSERT_EQ("Moto", results["facet_counts"][0]["counts"][5]["value"]);+ ASSERT_EQ("Iqoo", results["facet_counts"][0]["counts"][6]["value"]);++ ASSERT_EQ(7, results["facet_counts"][1]["counts"].size());+ ASSERT_EQ("Fusion Plus", results["facet_counts"][1]["counts"][0]["value"]);+ ASSERT_EQ("GT Master", results["facet_counts"][1]["counts"][1]["value"]);+ ASSERT_EQ("Mi 6", results["facet_counts"][1]["counts"][2]["value"]);+ ASSERT_EQ("Oneplus 11R", results["facet_counts"][1]["counts"][3]["value"]);+ ASSERT_EQ("S22 Ultra", results["facet_counts"][1]["counts"][4]["value"]);+ ASSERT_EQ("T2", results["facet_counts"][1]["counts"][5]["value"]);+ ASSERT_EQ("Z6 Lite", results["facet_counts"][1]["counts"][6]["value"]);++ //try sort on non stirng field+ search_op = coll1->search("*", {}, "", {"rating(sort:desc)"},+ {}, {2});++ ASSERT_EQ(400, search_op.code());+ ASSERT_EQ("Facet field should be string type to apply alpha sort.", search_op.error());+}