Skip to content

Apache Lucene open-source search software

LUCENE-10146: Add note that dot product is preferred over cosine (#400)

Julie Tibshirani6bb2bbcd6ab2
diff --git a/lucene/core/src/java/org/apache/lucene/index/VectorSimilarityFunction.java b/lucene/core/src/java/org/apache/lucene/index/VectorSimilarityFunction.java
index a133bf2c3b8..a237e3d8609 100644
--- a/lucene/core/src/java/org/apache/lucene/index/VectorSimilarityFunction.java
+++ b/lucene/core/src/java/org/apache/lucene/index/VectorSimilarityFunction.java
@@ -56,7 +56,12 @@ public enum VectorSimilarityFunction {
}
},
- /** Cosine similarity */
+ /**
+ * Cosine similarity. NOTE: the preferred way to perform cosine similarity is to normalize all
+ * vectors to unit length, and instead use {@link VectorSimilarityFunction#DOT_PRODUCT}. You
+ * should only use this function if you need to preserve the original vectors and cannot normalize
+ * them in advance.
+ */
COSINE {
@Override
public float compare(float[] v1, float[] v2) {