Implementation:Lance format Lance Java DistanceType
| Knowledge Sources | |
|---|---|
| Domains | Java_SDK, Indexing |
| Last Updated | 2026-02-08 19:33 GMT |
Overview
Description
DistanceType is a Java enum in the org.lance.index package that defines the supported distance metrics used for vector similarity search in Lance indices. It provides four distance metric variants: L2 (Euclidean distance), Cosine (cosine similarity), Dot (dot product), and Hamming (Hamming distance). This enum is used throughout the Java SDK to configure the distance function when creating vector indices via VectorIndexParams.
Usage
DistanceType is passed as a parameter when constructing vector index configurations. It determines how Lance computes the similarity between query vectors and indexed vectors during nearest-neighbor search. The most common usage is with the VectorIndexParams factory methods or builder, where the caller specifies which distance metric the index should use.
Code Reference
Source Location
java/src/main/java/org/lance/index/DistanceType.java
Signature
public enum DistanceType {
L2,
Cosine,
Dot,
Hamming;
}
Import
import org.lance.index.DistanceType;
I/O Contract
| Value | Description |
|---|---|
L2 |
Euclidean (L2) distance. Measures the straight-line distance between two vectors. |
Cosine |
Cosine similarity. Measures the angular distance between two vectors, normalized by their magnitudes. |
Dot |
Dot product distance. Uses the inner product of two vectors as the similarity measure. |
Hamming |
Hamming distance. Counts the number of positions where corresponding elements differ. Typically used with binary vectors. |
Usage Examples
// Use L2 distance for a vector index
VectorIndexParams params = VectorIndexParams.ivfFlat(256, DistanceType.L2);
// Use cosine similarity
VectorIndexParams cosineParams = VectorIndexParams.ivfFlat(128, DistanceType.Cosine);
// Use dot product with IVF-PQ
VectorIndexParams dotParams = VectorIndexParams.ivfPq(
256, 8, 16, DistanceType.Dot, 50
);
Related Pages
- VectorIndexParams - Uses
DistanceTypeto configure vector index distance metrics - IvfBuildParams - IVF build parameters used alongside distance type configuration
- IndexType - Enum of all index types including vector index variants