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

Return rank fusion score in response

ozanarmagan01f6885f55fc
diff --git a/src/collection.cpp b/src/collection.cpp
index 54dc6b87..c95e2969 100644
--- a/src/collection.cpp
+++ b/src/collection.cpp
@@ -1824,11 +1824,15 @@ Option<nlohmann::json> Collection::search(std::string raw_query,
if(field_order_kv->match_score_index == CURATED_RECORD_IDENTIFIER) {
wrapper_doc["curated"] = true;
} else if(field_order_kv->match_score_index >= 0) {
- wrapper_doc["text_match"] = field_order_kv->scores[field_order_kv->match_score_index];
+ if(vector_query.field_name.empty()) {
+ wrapper_doc["text_match"] = field_order_kv->scores[field_order_kv->match_score_index];
- wrapper_doc["text_match_info"] = nlohmann::json::object();
- populate_text_match_info(wrapper_doc["text_match_info"],
- field_order_kv->scores[field_order_kv->match_score_index], match_type);
+ wrapper_doc["text_match_info"] = nlohmann::json::object();
+ populate_text_match_info(wrapper_doc["text_match_info"],
+ field_order_kv->scores[field_order_kv->match_score_index], match_type);
+ } else {
+ wrapper_doc["rank_fusion_score"] = Index::int64_t_to_float(field_order_kv->scores[field_order_kv->match_score_index]);
+ }
}
nlohmann::json geo_distances;
diff --git a/test/collection_test.cpp b/test/collection_test.cpp
index f7743614..a1e1067a 100644
--- a/test/collection_test.cpp
+++ b/test/collection_test.cpp
@@ -4819,12 +4819,16 @@ TEST_F(CollectionTest, HybridSearchRankFusionTest) {
ASSERT_EQ(3, search_res["hits"].size());
// Hybrid search with rank fusion order:
// 1. butter (1/1 * 0.7) + (1/1 * 0.3) = 1
- // 2. butterfly (1/2 * 0.7) + (1/3 * 0.3) = 0.567
- // 3. butterball (1/3 * 0.7) + (1/2 * 0.3) = 0.533
+ // 2. butterfly (1/2 * 0.7) + (1/3 * 0.3) = 0.45
+ // 3. butterball (1/3 * 0.7) + (1/2 * 0.3) = 0.383
ASSERT_EQ("butter", search_res["hits"][0]["document"]["name"].get<std::string>());
ASSERT_EQ("butterfly", search_res["hits"][1]["document"]["name"].get<std::string>());
ASSERT_EQ("butterball", search_res["hits"][2]["document"]["name"].get<std::string>());
+ ASSERT_FLOAT_EQ((1.0/1.0 * 0.7) + (1.0/1.0 * 0.3), search_res["hits"][0]["rank_fusion_score"].get<float>());
+ ASSERT_FLOAT_EQ((1.0/2.0 * 0.7) + (1.0/3.0 * 0.3), search_res["hits"][1]["rank_fusion_score"].get<float>());
+ ASSERT_FLOAT_EQ((1.0/3.0 * 0.7) + (1.0/2.0 * 0.3), search_res["hits"][2]["rank_fusion_score"].get<float>());
+
// delete models folder
system("rm -rf ./models");
}
\ No newline at end of file