Home
• User Guide

# 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.
``````
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.

## 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.
``````
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

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.
``````
``````await milvusClient.collectionManager.loadCollection({
collection_name: "book",
});
``````
``````err := milvusClient.LoadCollection(
context.Background(),   // ctx
"book",                 // CollectionName
false                   // async
)
if err != nil {
}
``````
``````milvusClient.loadCollection(
.withCollectionName("book")
.build()
);
``````
``````load -c 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.
``````