Skip to content

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

Multi field matching: ensure best matched field prevails.

kishorencf164de4702b5
diff --git a/include/topster.h b/include/topster.h
index 26b15e7b..33e2bf9b 100644
--- a/include/topster.h
+++ b/include/topster.h
@@ -181,13 +181,19 @@ struct Topster {
KV* existing_kv = found_it->second;
//LOG(INFO) << "existing_kv: " << existing_kv->key << " -> " << existing_kv->match_score;
- if(is_smaller(kv, existing_kv) && kv->field_id == existing_kv->field_id) {
+ bool smaller_than_existing = is_smaller(kv, existing_kv);
+ if(smaller_than_existing && kv->field_id == existing_kv->field_id) {
return false;
}
+ // allows a record to be matched across different fields (aggregated matching)
if(kv->field_id != existing_kv->field_id) {
- // allows a record to be matched across different fields (aggregated matching)
- kv->scores[0] += existing_kv->scores[0];
+ int64_t new_score = kv->scores[0] + existing_kv->scores[0];
+ if(smaller_than_existing) {
+ // ensures that best matched KV is not overwritten (only score needed)
+ kv = existing_kv;
+ }
+ kv->scores[0] = new_score;
}
SIFT_DOWN = true;
diff --git a/test/collection_grouping_test.cpp b/test/collection_grouping_test.cpp
index 223ccab0..7ca2e20d 100644
--- a/test/collection_grouping_test.cpp
+++ b/test/collection_grouping_test.cpp
@@ -299,7 +299,7 @@ TEST_F(CollectionGroupingTest, GroupingWithMultiFieldRelevance) {
ASSERT_STREQ("pop", results["grouped_hits"][0]["group_key"][0].get<std::string>().c_str());
ASSERT_EQ(2, results["grouped_hits"][0]["hits"].size());
ASSERT_STREQ("1", results["grouped_hits"][0]["hits"][0]["document"]["id"].get<std::string>().c_str());
- ASSERT_STREQ("2", results["grouped_hits"][0]["hits"][1]["document"]["id"].get<std::string>().c_str());
+ ASSERT_STREQ("4", results["grouped_hits"][0]["hits"][1]["document"]["id"].get<std::string>().c_str());
ASSERT_STREQ("rock", results["grouped_hits"][1]["group_key"][0].get<std::string>().c_str());
ASSERT_EQ(1, results["grouped_hits"][1]["hits"].size());
diff --git a/test/collection_test.cpp b/test/collection_test.cpp
index 63ce3ac0..76d7ad80 100644
--- a/test/collection_test.cpp
+++ b/test/collection_test.cpp
@@ -2579,6 +2579,15 @@ TEST_F(CollectionTest, MultiFieldRelevance) {
ASSERT_EQ(expected_ids[i], std::stoi(results["hits"][i]["document"]["id"].get<std::string>()));
}
+ ASSERT_STREQ("<mark>Down</mark> <mark>There</mark> <mark>by</mark> <mark>the</mark> <mark>Train</mark>",
+ results["hits"][0]["highlights"][0]["snippet"].get<std::string>().c_str());
+
+ ASSERT_STREQ("<mark>Down</mark> <mark>There</mark> <mark>by</mark> <mark>the</mark> <mark>Train</mark>",
+ results["hits"][1]["highlights"][0]["snippet"].get<std::string>().c_str());
+
+ ASSERT_STREQ("<mark>Dustin</mark> <mark>Kensrue</mark>",
+ results["hits"][2]["highlights"][0]["snippet"].get<std::string>().c_str());
+
// remove documents, reindex in another order and search again
for(size_t i=0; i<expected_ids.size(); i++) {
coll1->remove_if_found(i, true);