milvus-logo

Calculate Distance Between Vectors

This topic describes how to calculate distance between vectors with Milvus.

Milvus searches most similar vectors based on the distance calculation of vectors. Vice versa, you can use Milvus to calculate the distance between vectors using distance metrics that suit specific scenario. See Similarity Metrics for more information.

The following example simulates the scenarios when you want to calculate the distance between vectors in the collection and some other vectors.

Prepare vectors

Prepare the vectors used for calculation.

Vectors to be calculated must agree in vector type and dimension.
vectors_left = {
    "ids": [0, 1], 
    "collection": "book", 
    "partition": "_default", 
    "field": "book_intro"
}
import random
external_vectors = [[random.random() for _ in range(2)] for _ in range(4)]
vectors_right = {"float_vectors": external_vectors}
// Node User Guide will be ready soon.
// GO User Guide will be ready soon.
// Java User Guide will be ready soon.
// CLI User Guide will be ready soon.
vectors_left='{
  "dim": 2,
  "ids": {
    "id_array": [1,2],
    "collection_name": "book",
    "partition_names": ["_default"],
    "field_name": "book_intro"
  }
}'
vectors_right='{
  "dim": 2,
  "vectors": [1,2,3,4,5,6,7,8] # The numbers in the list will be automatically split into four vectors. 
}'
Parameter Description
vectors_left and vectors_right Vectors on the left and right side of the operator. Dict type that can be represented as {"ids": [primary_key_1, primary_key_2, ... primary_key_n], "collection": "collection_name", "partition": "partition_name", "field": "vector_field_name"}, {"float_vectors": [[1.0, 2.0], [3.0, 4.0], ... [9.0, 10.0]]}, or {"bin_vectors": [b'”', b'N', ... b'Ê']}.
ids List of primary key of entities that in the collection.
collection Name of the collection that holds the entities.
partition Name of the partition that holds the entities.
field Name of the vector field in the collection.
float_vectors or bin_vectors Type of the vectors.
Parameter Description Option
dim Dimension of the vector. N/A
id_array List of the primary keys of entities in the collection. N/A
collection_name Name of the collection that holds the entities. N/A
partition_names Names of the partitions that hold the entities. N/A
field_name Name of the vector field in the collection. N/A
vectors Temporarily only floating-point vectors are supported. N/A

Prepare calculation parameters

Specify the parameters used for the calculation.

params = {
    "metric": "IP", 
    "dim": 2
}
// Node User Guide will be ready soon.
// GO User Guide will be ready soon.
// Java User Guide will be ready soon.
// CLI User Guide will be ready soon.
params='[
  {"key": "metric", "value": "IP"}
]'
Parameter Description Option
params Calculation parameters. N/A
metric Metric types used for calculation. For floating-point vectors:
  • L2 (Euclidean distance)
  • IP (Inner product)
For binary vectors:
  • JACCARD (Jaccard distance)
  • TANIMOTO (Tanimoto distance)
  • HAMMING (Hamming distance)
  • SUPERSTRUCTURE (Superstructure)
  • SUBSTRUCTURE (Substructure)
dim Dimension of the vector. N/A
Parameter Description Option
metric Metric types used for calculation. For floating-point vectors:
  • L2 (Euclidean distance)
  • IP (Inner product)

(Optional) Load collection

If you calculate with the vectors in a collection in Milvus, you must load the collection to memory first.

from pymilvus import Collection
collection = Collection("book")      # Get an existing collection.
collection.load()
await milvusClient.collectionManager.loadCollection({
  collection_name: "book",
});
err := milvusClient.LoadCollection(
  context.Background(),   // ctx
  "book",                 // CollectionName
  false                   // async
)
if err != nil {
  log.Fatal("failed to load collection:", err.Error())
}
milvusClient.loadCollection(
  LoadCollectionParam.newBuilder()
    .withCollectionName("book")
    .build()
);
load -c book
curl -X 'POST' \
  'http://localhost:9091/api/v1/collection/load' \
  -H 'accept: application/json' \
  -H 'Content-Type: application/json' \
  -d '{
    "collection_name": "book"
  }'

Calculate vector distance

Calculate the distance between vectors based on the vectors and parameters provided.

from pymilvus import utility
results = utility.calc_distance(
    vectors_left=vectors_left, 
    vectors_right=vectors_right, 
    params=params
)
print(results)
// Node User Guide will be ready soon.
// GO User Guide will be ready soon.
// Java User Guide will be ready soon.
// CLI User Guide will be ready soon.
curl -X 'GET' \
  'http://localhost:9091/api/v1/distance' \
  -H 'accept: application/json' \
  -H 'Content-Type: application/json' \
  -d "{
    \"op_left\": $vectors_left,
    \"op_right\": $vectors_right,
    \"params\": $params
  }"
Output:
{"status":{},"Array":{"FloatDist":{"data":[3,7,11,15,4,10,16,22]}}}

What's next

On this page