From fa7cf587b0983b3f323873742923d316933ff655 Mon Sep 17 00:00:00 2001
From: xige-16 <xi.ge@zilliz.com>
Date: Wed, 17 Jan 2024 20:25:03 +0800
Subject: [PATCH] enhance: Opt metric type does not match error message
 (#29927)

issue: #29791
/kind improvement
Signed-off-by: xige-16 <xi.ge@zilliz.com>

Signed-off-by: xige-16 <xi.ge@zilliz.com>
---
 internal/core/src/common/EasyAssert.h         |  2 ++
 .../core/src/segcore/SegmentGrowingImpl.h     | 11 +----------
 .../core/src/segcore/SegmentInterface.cpp     | 19 +++++++++++++++++++
 internal/core/src/segcore/SegmentInterface.h  |  4 ++++
 .../core/src/segcore/SegmentSealedImpl.cpp    | 11 +----------
 5 files changed, 27 insertions(+), 20 deletions(-)

diff --git a/internal/core/src/common/EasyAssert.h b/internal/core/src/common/EasyAssert.h
index 98b9eb7218..4d84b45aea 100644
--- a/internal/core/src/common/EasyAssert.h
+++ b/internal/core/src/common/EasyAssert.h
@@ -58,7 +58,9 @@ enum ErrorCode {
     FieldNotLoaded = 2027,
     ExprInvalid = 2028,
     UnistdError = 2030,
+    MetricTypeNotMatch = 2031,
     KnowhereError = 2100,
+
 };
 namespace impl {
 void
diff --git a/internal/core/src/segcore/SegmentGrowingImpl.h b/internal/core/src/segcore/SegmentGrowingImpl.h
index e23921801e..6293593895 100644
--- a/internal/core/src/segcore/SegmentGrowingImpl.h
+++ b/internal/core/src/segcore/SegmentGrowingImpl.h
@@ -281,16 +281,7 @@ class SegmentGrowingImpl : public SegmentGrowing {
     void
     check_search(const query::Plan* plan) const override {
         Assert(plan);
-        auto& metric_str = plan->plan_node_->search_info_.metric_type_;
-        auto searched_field_id = plan->plan_node_->search_info_.field_id_;
-        auto index_meta =
-            index_meta_->GetFieldIndexMeta(FieldId(searched_field_id));
-        if (metric_str.empty()) {
-            metric_str = index_meta.GeMetricType();
-        } else {
-            AssertInfo(metric_str == index_meta.GeMetricType(),
-                       "metric type not match");
-        }
+        check_metric_type(plan, index_meta_);
     }
 
     const ConcurrentVector<Timestamp>&
diff --git a/internal/core/src/segcore/SegmentInterface.cpp b/internal/core/src/segcore/SegmentInterface.cpp
index 6779b56654..ca8e546a80 100644
--- a/internal/core/src/segcore/SegmentInterface.cpp
+++ b/internal/core/src/segcore/SegmentInterface.cpp
@@ -316,4 +316,23 @@ SegmentInternalInterface::LoadStringSkipIndex(
     skipIndex_.LoadString(field_id, chunk_id, var_column);
 }
 
+void
+SegmentInternalInterface::check_metric_type(
+    const query::Plan* plan, const IndexMetaPtr index_meta) const {
+    auto& metric_str = plan->plan_node_->search_info_.metric_type_;
+    auto searched_field_id = plan->plan_node_->search_info_.field_id_;
+    auto field_index_meta =
+        index_meta->GetFieldIndexMeta(FieldId(searched_field_id));
+    if (metric_str.empty()) {
+        metric_str = field_index_meta.GeMetricType();
+    }
+    if (metric_str != field_index_meta.GeMetricType()) {
+        throw SegcoreError(
+            MetricTypeNotMatch,
+            fmt::format("metric type not match, expected {}, actual {}.",
+                        field_index_meta.GeMetricType(),
+                        metric_str));
+    }
+}
+
 }  // namespace milvus::segcore
diff --git a/internal/core/src/segcore/SegmentInterface.h b/internal/core/src/segcore/SegmentInterface.h
index a5fbc8014d..d8dd42e7ac 100644
--- a/internal/core/src/segcore/SegmentInterface.h
+++ b/internal/core/src/segcore/SegmentInterface.h
@@ -231,6 +231,10 @@ class SegmentInternalInterface : public SegmentInterface {
     virtual std::pair<std::unique_ptr<IdArray>, std::vector<SegOffset>>
     search_ids(const IdArray& id_array, Timestamp timestamp) const = 0;
 
+    void
+    check_metric_type(const query::Plan* plan,
+                      const IndexMetaPtr index_meta) const;
+
     /**
      * Apply timestamp filtering on bitset, the query can't see an entity whose
      * timestamp is bigger than the timestamp of query.
diff --git a/internal/core/src/segcore/SegmentSealedImpl.cpp b/internal/core/src/segcore/SegmentSealedImpl.cpp
index 8e6f9f0163..c8529f816a 100644
--- a/internal/core/src/segcore/SegmentSealedImpl.cpp
+++ b/internal/core/src/segcore/SegmentSealedImpl.cpp
@@ -921,16 +921,7 @@ SegmentSealedImpl::check_search(const query::Plan* plan) const {
     AssertInfo(plan->extra_info_opt_.has_value(),
                "Extra info of search plan doesn't have value");
 
-    auto& metric_str = plan->plan_node_->search_info_.metric_type_;
-    auto searched_field_id = plan->plan_node_->search_info_.field_id_;
-    auto index_meta =
-        col_index_meta_->GetFieldIndexMeta(FieldId(searched_field_id));
-    if (metric_str.empty()) {
-        metric_str = index_meta.GeMetricType();
-    } else {
-        AssertInfo(metric_str == index_meta.GeMetricType(),
-                   "metric type not match");
-    }
+    check_metric_type(plan, col_index_meta_);
 
     if (!is_system_field_ready()) {
         PanicInfo(