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
More fixes for exact match ranking order.
kishorenc535f5f7e1fd9
diff --git a/src/index.cpp b/src/index.cppindex 8500dee5..6cf0ac48 100644--- a/src/index.cpp+++ b/src/index.cpp@@ -1714,6 +1714,7 @@ void Index::search(const std::vector<std::string>& q_include_tokens,uint32_t token_bits = (uint32_t(1) << 31); // top most bit set to guarantee atleast 1 bit setuint64_t total_typos = 0, total_distances = 0;+ uint64_t num_exact_matches = 0;//LOG(INFO) << "Init pop count: " << __builtin_popcount(token_bits);@@ -1730,7 +1731,14 @@ void Index::search(const std::vector<std::string>& q_include_tokens,int64_t match_score = existing_field_kvs[field_id]->scores[existing_field_kvs[field_id]->match_score_index];total_distances += ((100 - (match_score & 0xFF)) + 1) * weight;- total_typos += ((255 - ((match_score >> 8) & 0xFF)) + 1) * weight;++ uint64_t tokens_found = ((match_score >> 16) & 0xFF);+ int64_t field_typos = 255 - ((match_score >> 8) & 0xFF);+ total_typos += (field_typos + 1) * weight;++ if(field_typos == 0 && tokens_found == q_include_tokens.size()) {+ num_exact_matches++;+ }/*LOG(INFO) << "seq_id: " << seq_id << ", total_typos: " << (255 - ((match_score >> 8) & 0xFF))<< ", weighted typos: " << std::max<uint64_t>((255 - ((match_score >> 8) & 0xFF)), 1) * weight@@ -1776,7 +1784,14 @@ void Index::search(const std::vector<std::string>& q_include_tokens,if(words_present != 0) {uint64_t match_score = Match::get_match_score(words_present, 0, 0);total_distances += ((100 - (match_score & 0xFF)) + 1) * weight;- total_typos += ((255 - ((match_score >> 8) & 0xFF)) + 1) * weight;++ uint64_t tokens_found = ((match_score >> 16) & 0xFF);+ uint64_t field_typos = 255 - ((match_score >> 8) & 0xFF);+ total_typos += (field_typos + 1) * weight;++ if(field_typos == 0 && tokens_found == q_include_tokens.size()) {+ num_exact_matches++;+ }//LOG(INFO) << "seq_id: " << seq_id << ", total_typos: " << ((match_score >> 8) & 0xFF);}}@@ -1786,6 +1801,7 @@ void Index::search(const std::vector<std::string>& q_include_tokens,total_distances = std::min<uint64_t>(100, total_distances);uint64_t aggregated_score = (+ (num_exact_matches << 24) |(tokens_present << 16) |((255 - total_typos) << 8) |(100 - total_distances)diff --git a/test/collection_test.cpp b/test/collection_test.cppindex 65879b1d..72a48a1f 100644--- a/test/collection_test.cpp+++ b/test/collection_test.cpp@@ -2908,8 +2908,8 @@ TEST_F(CollectionTest, MultiFieldRelevance3) {ASSERT_EQ(2, results["found"].get<size_t>());ASSERT_EQ(2, results["hits"].size());- ASSERT_STREQ("1", results["hits"][0]["document"]["id"].get<std::string>().c_str());- ASSERT_STREQ("0", results["hits"][1]["document"]["id"].get<std::string>().c_str());+ ASSERT_STREQ("0", results["hits"][0]["document"]["id"].get<std::string>().c_str());+ ASSERT_STREQ("1", results["hits"][1]["document"]["id"].get<std::string>().c_str());collectionManager.drop_collection("coll1");}@@ -2957,6 +2957,54 @@ TEST_F(CollectionTest, MultiFieldRelevance4) {collectionManager.drop_collection("coll1");}+TEST_F(CollectionTest, MultiFieldRelevance5) {+ Collection *coll1;++ std::vector<field> fields = {field("company_name", field_types::STRING, false),+ field("country", field_types::STRING, false),+ field("field_a", field_types::STRING, false),+ field("num_employees", field_types::INT32, false),};++ coll1 = collectionManager.get_collection("coll1").get();+ if(coll1 == nullptr) {+ coll1 = collectionManager.create_collection("coll1", 1, fields, "num_employees").get();+ }++ std::vector<std::vector<std::string>> records = {+ {"Stark Industries ™", "Canada", "Canadia", "5215"},+ {"Canaida Corp", "United States", "Canadoo", "200"},+ {"Acme Corp", "Mexico", "Canadoo", "300"}+ };++ for(size_t i=0; i<records.size(); i++) {+ nlohmann::json doc;++ doc["id"] = std::to_string(i);+ doc["company_name"] = records[i][0];+ doc["country"] = records[i][1];+ doc["field_a"] = records[i][2];+ doc["num_employees"] = std::stoi(records[i][3]);++ ASSERT_TRUE(coll1->add(doc.dump()).ok());+ }++ auto results = coll1->search("Canada",+ {"company_name","country","field_a"}, "", {}, {}, 2, 10, 1, FREQUENCY,+ true, 10, spp::sparse_hash_set<std::string>(),+ spp::sparse_hash_set<std::string>(), 10, "", 30, 4, "", 40, {}, {}, {}, 0,+ "<mark>", "</mark>", {1, 1, 1}).get();++ LOG(INFO) << results;++ ASSERT_EQ(3, results["found"].get<size_t>());+ ASSERT_EQ(3, results["hits"].size());++ ASSERT_STREQ("0", results["hits"][0]["document"]["id"].get<std::string>().c_str());+ //ASSERT_STREQ("1", results["hits"][1]["document"]["id"].get<std::string>().c_str());++ collectionManager.drop_collection("coll1");+}+TEST_F(CollectionTest, MultiFieldMatchRanking) {Collection *coll1;