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
Support zero weighting for multi-field match scoring.
Kishore Nallanba67efb7da06
diff --git a/src/collection.cpp b/src/collection.cppindex 1fc39fdd..1e8268cf 100644--- a/src/collection.cpp+++ b/src/collection.cpp@@ -555,7 +555,7 @@ Option<nlohmann::json> Collection::search(const std::string & query, const std::// process weights for search fieldsstd::vector<search_field_t> weighted_search_fields;- size_t max_weight = 100;+ size_t max_weight = 20;if(query_by_weights.empty()) {max_weight = search_fields.size();@@ -568,8 +568,10 @@ Option<nlohmann::json> Collection::search(const std::string & query, const std::for(size_t i=0; i < search_fields.size(); i++) {const auto& search_field = search_fields[i];+ // NOTE: we support zero-weight only for weighting and not priority since priority is used for typos, where+ // relative ordering is still useful.+ const auto weight = query_by_weights[i];const auto priority = (max_weight - query_by_weights[i]) + 1;- const auto weight = query_by_weights[i] + 1;weighted_search_fields.push_back({search_field, priority, weight});}diff --git a/test/collection_specific_test.cpp b/test/collection_specific_test.cppindex 7a98888c..b504bb0f 100644--- a/test/collection_specific_test.cpp+++ b/test/collection_specific_test.cpp@@ -1315,12 +1315,83 @@ TEST_F(CollectionSpecificTest, MultiFieldMatchesShouldBeWeighted) {spp::sparse_hash_set<std::string>(), 10, "", 30, 4, "", 10, {}, {}, {}, 0,"<mark>", "</mark>", {6, 1, 1}).get();- LOG(INFO) << results;+ ASSERT_EQ(2, results["hits"].size());+ ASSERT_EQ("1", results["hits"][0]["document"]["id"].get<std::string>());+ ASSERT_EQ("0", results["hits"][1]["document"]["id"].get<std::string>());++ collectionManager.drop_collection("coll1");+}++TEST_F(CollectionSpecificTest, ZeroWeightedField) {+ // 2 matches on low weighted fields should not overpower a single match on high weighted field+ std::vector<field> fields = {field("name", field_types::STRING, false),+ field("category", field_types::STRING, false),+ field("points", field_types::INT32, false),};++ Collection* coll1 = collectionManager.create_collection("coll1", 1, fields, "points").get();++ nlohmann::json doc1;+ doc1["id"] = "0";+ doc1["name"] = "Energy Kids";+ doc1["category"] = "kids";+ doc1["points"] = 3;++ nlohmann::json doc2;+ doc2["id"] = "1";+ doc2["name"] = "Amazing Twin";+ doc2["category"] = "kids";+ doc2["points"] = 5;++ ASSERT_TRUE(coll1->add(doc1.dump()).ok());+ ASSERT_TRUE(coll1->add(doc2.dump()).ok());++ auto results = coll1->search("kids", {"name", "category"},+ "", {}, {}, {0, 0}, 10,+ 1, FREQUENCY, {false, false},+ 2, spp::sparse_hash_set<std::string>(),+ spp::sparse_hash_set<std::string>(), 10, "", 30, 4, "", 10, {}, {}, {}, 0,+ "<mark>", "</mark>", {0, 1}).get();ASSERT_EQ(2, results["hits"].size());ASSERT_EQ("1", results["hits"][0]["document"]["id"].get<std::string>());ASSERT_EQ("0", results["hits"][1]["document"]["id"].get<std::string>());+ collectionManager.drop_collection("coll1");+}++TEST_F(CollectionSpecificTest, ZeroWeightedFieldCannotPrioritizeExactMatch) {+ std::vector<field> fields = {field("name", field_types::STRING, false),+ field("category", field_types::STRING, false),+ field("points", field_types::INT32, false),};++ Collection* coll1 = collectionManager.create_collection("coll1", 1, fields, "points").get();++ nlohmann::json doc1;+ doc1["id"] = "0";+ doc1["name"] = "Levis";+ doc1["category"] = "mens";+ doc1["points"] = 3;++ nlohmann::json doc2;+ doc2["id"] = "1";+ doc2["name"] = "Amazing from Levis";+ doc2["category"] = "mens";+ doc2["points"] = 5;++ ASSERT_TRUE(coll1->add(doc1.dump()).ok());+ ASSERT_TRUE(coll1->add(doc2.dump()).ok());++ auto results = coll1->search("levis", {"name", "category"},+ "", {}, {}, {0, 0}, 10,+ 1, FREQUENCY, {false, false},+ 2, spp::sparse_hash_set<std::string>(),+ spp::sparse_hash_set<std::string>(), 10, "", 30, 4, "", 10, {}, {}, {}, 0,+ "<mark>", "</mark>", {0, 1},+ 1000, true).get();++ ASSERT_EQ(2, results["hits"].size());+ ASSERT_EQ("1", results["hits"][0]["document"]["id"].get<std::string>());+ ASSERT_EQ("0", results["hits"][1]["document"]["id"].get<std::string>());collectionManager.drop_collection("coll1");}