Enum Class WeaviateDistance
- All Implemented Interfaces:
Serializable,Comparable<WeaviateDistance>,Constable
The distance determines how Weaviate compares stored vectors with a query vector.
It belongs to the collection vector index configuration, so it is chosen when the collection is created
(WeaviateConfigurations.DISTANCE) and it is not selected per search operation.
query vector
|
v
configured distance
|
v
stored vectors
|
v
nearest matches
The search threshold of the JNoSQL vector API keeps the same meaning for every distance:
with COSINE and DOT it is the minimum similarity a result must have, and with
EUCLIDEAN, MANHATTAN and HAMMING it is the maximum distance a result may have.
-
Nested Class Summary
Nested classes/interfaces inherited from class java.lang.Enum
Enum.EnumDesc<E extends Enum<E>> -
Enum Constant Summary
Enum ConstantsEnum ConstantDescriptionCosine similarity, from-1(opposite) to1(same direction).Dot (inner) product, which grows with both the alignment and the magnitude of the vectors.Euclidean (L2) distance, the straight-line distance between two points.Hamming distance, the number of dimensions whose values differ.Manhattan (L1) distance, the sum of the absolute differences of each dimension. -
Method Summary
Modifier and TypeMethodDescriptionstatic WeaviateDistanceParses a distance by its name or its Weaviate name (L2_SQUARED), ignoring the case.static WeaviateDistanceReturns the enum constant of this class with the specified name.static WeaviateDistance[]values()Returns an array containing the constants of this enum class, in the order they are declared.
-
Enum Constant Details
-
COSINE
Cosine similarity, from-1(opposite) to1(same direction).It compares the direction of the vectors and ignores their magnitude. It suits text embeddings, where two sentences about the same topic point in the same direction even if one of them is much longer.
cos(a, b) = (a ยท b) / (|a| * |b|)Higher similarities are more similar.
-
DOT
Dot (inner) product, which grows with both the alignment and the magnitude of the vectors.It suits embeddings that are already normalized by the model, or recommendation scores where a longer vector means a stronger match.
dot(a, b) = sum(a[i] * b[i])Higher products are more similar.
-
EUCLIDEAN
Euclidean (L2) distance, the straight-line distance between two points.It compares the absolute position of the vectors, so magnitude matters: two products with the same attributes but very different prices are far from each other.
l2(a, b) = sqrt(sum((a[i] - b[i])^2))Weaviate stores the squared distance (
l2-squared), which ranks results the same way and avoids the square root. Lower distances are more similar. It is also accepted asL2_SQUARED. -
MANHATTAN
Manhattan (L1) distance, the sum of the absolute differences of each dimension.It is the distance of walking a city grid, and it is less sensitive than
EUCLIDEANto a single dimension that differs a lot.l1(a, b) = sum(|a[i] - b[i]|)Lower distances are more similar.
-
HAMMING
Hamming distance, the number of dimensions whose values differ.It suits vectors of flags or hashes, such as the set of features an item has or lacks.
hamming(a, b) = count(a[i] != b[i])Lower distances are more similar.
-
-
Method Details
-
values
Returns an array containing the constants of this enum class, in the order they are declared.- Returns:
- an array containing the constants of this enum class, in the order they are declared
-
valueOf
Returns the enum constant of this class with the specified name. The string must match exactly an identifier used to declare an enum constant in this class. (Extraneous whitespace characters are not permitted.)- Parameters:
name- the name of the enum constant to be returned.- Returns:
- the enum constant with the specified name
- Throws:
IllegalArgumentException- if this enum class has no constant with the specified nameNullPointerException- if the argument is null
-
parse
Parses a distance by its name or its Weaviate name (L2_SQUARED), ignoring the case.- Parameters:
value- the distance name- Returns:
- the distance
- Throws:
NullPointerException- when the value is nullIllegalArgumentException- when the value is not a supported distance
-