Fix similarity correlation (#12511)

Signed-off-by: dragondriver <jiquan.long@zilliz.com>
This commit is contained in:
dragondriver 2021-12-01 15:26:02 +08:00 committed by GitHub
parent b08e28abc1
commit 1db1cf2104
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 11 additions and 17 deletions

View File

@ -13,12 +13,9 @@
#include "index/thirdparty/faiss/MetricType.h"
namespace milvus {
namespace segcore {
namespace milvus::segcore {
static inline bool
PositivelyRelated(faiss::MetricType metric_type) {
return metric_type == faiss::MetricType::METRIC_INNER_PRODUCT || metric_type == faiss::MetricType::METRIC_Jaccard ||
metric_type == faiss::MetricType::METRIC_Tanimoto;
return metric_type == faiss::MetricType::METRIC_INNER_PRODUCT;
}
} // namespace segcore
} // namespace milvus
} // namespace milvus::segcore

View File

@ -68,8 +68,7 @@ Search(CSegmentInterface c_segment,
auto plan = (milvus::query::Plan*)c_plan;
auto phg_ptr = reinterpret_cast<const milvus::query::PlaceholderGroup*>(c_placeholder_group);
*search_result = segment->Search(plan, *phg_ptr, timestamp);
if (plan->plan_node_->search_info_.metric_type_ != milvus::MetricType::METRIC_INNER_PRODUCT) {
// if (!milvus::segcore::PositivelyRelated(plan->plan_node_->search_info_.metric_type_)) {
if (!milvus::segcore::PositivelyRelated(plan->plan_node_->search_info_.metric_type_)) {
for (auto& dis : search_result->distances_) {
dis *= -1;
}

View File

@ -15,9 +15,9 @@
TEST(SimilarityCorelation, Naive) {
ASSERT_TRUE(milvus::segcore::PositivelyRelated(faiss::METRIC_INNER_PRODUCT));
ASSERT_TRUE(milvus::segcore::PositivelyRelated(faiss::METRIC_Jaccard));
ASSERT_TRUE(milvus::segcore::PositivelyRelated(faiss::METRIC_Tanimoto));
ASSERT_FALSE(milvus::segcore::PositivelyRelated(faiss::METRIC_Jaccard));
ASSERT_FALSE(milvus::segcore::PositivelyRelated(faiss::METRIC_Tanimoto));
ASSERT_FALSE(milvus::segcore::PositivelyRelated(faiss::METRIC_L2));
ASSERT_FALSE(milvus::segcore::PositivelyRelated(faiss::METRIC_Hamming));
ASSERT_FALSE(milvus::segcore::PositivelyRelated(faiss::METRIC_Substructure));

View File

@ -46,6 +46,7 @@ import (
"github.com/milvus-io/milvus/internal/proto/querypb"
"github.com/milvus-io/milvus/internal/proto/schemapb"
"github.com/milvus-io/milvus/internal/types"
"github.com/milvus-io/milvus/internal/util/distance"
"github.com/milvus-io/milvus/internal/util/funcutil"
"github.com/milvus-io/milvus/internal/util/indexparamcheck"
"github.com/milvus-io/milvus/internal/util/timerecord"
@ -1831,8 +1832,7 @@ func reduceSearchResultData(searchResultData []*schemapb.SearchResultData, nq in
log.Debug("skip duplicated search result", zap.Int64("count", skipDupCnt))
ret.Results.TopK = realTopK
if metricType != "IP" {
// if !distance.PositivelyRelated(metricType) {
if !distance.PositivelyRelated(metricType) {
for k := range ret.Results.Scores {
ret.Results.Scores[k] *= -1
}

View File

@ -15,7 +15,5 @@ import "strings"
func PositivelyRelated(metricType string) bool {
mUpper := strings.ToUpper(metricType)
return mUpper == strings.ToUpper(IP) ||
mUpper == strings.ToUpper(JACCARD) ||
mUpper == strings.ToUpper(TANIMOTO)
return mUpper == strings.ToUpper(IP)
}

View File

@ -24,11 +24,11 @@ func TestPositivelyRelated(t *testing.T) {
},
{
JACCARD,
true,
false,
},
{
TANIMOTO,
true,
false,
},
{
L2,