KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Cosine, Dot or Euclidean? Choosing a 4D.Vector Metric
PRODUCT: 4D | VERSION: 21 | PLATFORM: Mac & Win
Published On: December 2, 2026
4D.Vector compares vectors with cosineSimilarity (direction only), dotSimilarity (direction and length) or euclideanDistance (how far apart). On the hand-made vectors of the example, each picks a different winner: C points the same way as the query (cosine 1), A is the longest (dot 18), B is the nearest (distance 0.45).

Real embeddings behave differently. For instance, all-minilm and nomic-embed-text, run by Ollama through its /v1/embeddings endpoint, returned vectors of length 1 ($v.dotSimilarity($v) ≈ 1): cosine and dot are then equal, the distance is the square root of 2 − 2 × cosine, and all three rank texts identically. Only the threshold changes.

In an ORDA query, metric takes mk cosine, mk dot or mk euclidean. For a distance, write embedding < :1 order by embedding; in our tests, the > form raised no error but kept only vectors farther than the threshold. That threshold defaults to 0.5 for a distance too, which for vectors of length 1 is a cosine of 0.875: with all-minilm, a Euclidean query without a threshold returned nothing, while the cosine query returned the best match (distance 0.92, cosine 0.57).

Example_VectorMetrics:
#DECLARE() : Object

var $query : 4D.Vector:=4D.Vector.new([1; 1])
var $candidates : Object:={A: 4D.Vector.new([10; 8]); \
    B: 4D.Vector.new([1.2; 0.6]); \
    C: 4D.Vector.new([3; 3])}
var $rows : Collection:=[]
var $name : Text

For each ($name; $candidates)
    var $vector : 4D.Vector:=$candidates[$name]
    $rows.push({name: $name; \
        cosine: $query.cosineSimilarity($vector); \
        dot: $query.dotSimilarity($vector); \
        euclidean: $query.euclideanDistance($vector)})
End for each

return {cosine: $rows.orderBy("cosine desc").extract("name").join(" "); \
    dot: $rows.orderBy("dot desc").extract("name").join(" "); \
    euclidean: $rows.orderBy("euclidean").extract("name").join(" ")}


Result:

{
    "cosine": "C A B",
    "dot": "A C B",
    "euclidean": "B C A"
}