MetricType

This enum specifies the distance metric used to compare vectors. Pass a MetricType value to IndexDesc when creating an index, and to search request arguments when running a search. The valid choices depend on the vector field’s data type.

enum class MetricType {
    INVALID = 0,   // synonym: DEFAULT
    DEFAULT = 0,
    L2 = 1,
    IP = 2,
    COSINE = 3,
    HAMMING = 101,
    JACCARD = 102,
    MHJACCARD = 103,
    BM25 = 201,
    MAX_SIM_COSINE = 301,
    MAX_SIM_IP = 302,
    MAX_SIM_L2 = 303,
    MAX_SIM_JACCARD = 401,
    MAX_SIM_HAMMING = 402,
};

VALUES:

Dense float vectors (FLOAT_VECTOR, FLOAT16_VECTOR, BFLOAT16_VECTOR, INT8_VECTOR):

  • L2 (1) - Euclidean distance. Smaller value means more similar. Use when vectors are not normalized.

  • IP (2) - Inner product (dot product). Larger value means more similar. Use with pre-normalized (unit-length) vectors; numerically equivalent to cosine similarity in that case.

  • COSINE (3) - Cosine similarity (range −1 to 1). Larger value means more similar. Recommended over IP when vectors may not be normalized, as Milvus normalizes them internally.

Binary vectors (BINARY_VECTOR):

  • HAMMING (101) - Number of bit positions that differ between two vectors (popcount of XOR). Lower is more similar.

  • JACCARD (102) - Jaccard distance: ratio of bit positions where exactly one of the two vectors has a set bit. Lower is more similar. Preferred for sparse set-membership vectors.

  • MHJACCARD (103) - Modified Hamming–Jaccard hybrid distance for binary vectors.

Sparse vectors (SPARSE_FLOAT_VECTOR):

  • BM25 (201) - BM25 relevance score for full-text search. Only valid for sparse vectors generated by a BM25 built-in function. Larger score means more relevant.

Struct fields (multi-vector — STRUCT):

  • MAX_SIM_COSINE (301) - Maximum cosine similarity across all sub-vectors in a struct field.

  • MAX_SIM_IP (302) - Maximum inner product across all sub-vectors in a struct field.

  • MAX_SIM_L2 (303) - Minimum L2 distance (maximum L2 similarity) across all sub-vectors in a struct field.

  • MAX_SIM_JACCARD (401) - Maximum Jaccard similarity across binary sub-vectors in a struct field.

  • MAX_SIM_HAMMING (402) - Maximum Hamming similarity across binary sub-vectors in a struct field.

Special:

  • INVALID / DEFAULT (0) - Not explicitly set; the server auto-determines the metric type based on the field data type. Not needed for scalar field indexes.

Example

#include "milvus/MilvusClientV2.h"
#include <milvus/MilvusClientV2.h>
#include <milvus/types/MetricType.h>
using namespace milvus;

auto client = MilvusClientV2::Create();
client->Connect(ConnectParam("http://localhost:19530").WithToken("root:Milvus"));

// Float vector: cosine similarity (recommended for unnormalized embeddings)
IndexDesc idx_float("vec", "vec_idx", IndexType::HNSW, MetricType::COSINE);
idx_float.AddExtraParam("M", "16");
idx_float.AddExtraParam("efConstruction", "200");

// Binary vector: Hamming distance
IndexDesc idx_bin("bin_vec", "bin_idx", IndexType::BIN_FLAT, MetricType::HAMMING);

// Sparse vector: BM25 for full-text search
IndexDesc idx_sparse("sparse_vec", "sparse_idx",
                     IndexType::SPARSE_INVERTED_INDEX, MetricType::BM25);

client->CreateIndex(
    CreateIndexRequest()
        .WithCollectionName("my_collection")
        .WithSync(true)
        .AddIndex(std::move(idx_float))
        .AddIndex(std::move(idx_bin))
        .AddIndex(std::move(idx_sparse)));

Try Managed Milvus for Free

Zilliz Cloud is hassle-free, powered by Milvus and 10x faster.

Get Started
Feedback

Was this page helpful?