Vector functions
Vector functions allow you to construct VECTOR
values, compute the similarity and distance of vector pairs, and calculate the size of a vector.
vector()Cypher 25 onlyIntroduced in Neo4j 2025.10
Syntax |
|
||
Description |
Constructs a |
||
Arguments |
Name |
Type |
Description |
|
|
The numeric values to create the vector coordinates from. |
|
|
|
The dimension (number of coordinates) of the vector. |
|
|
|
The type of each coordinate in the vector. |
|
Returns |
|
If a |
|
If |
|
A |
VECTOR
valueRETURN vector([1, 2, 3], 3, INTEGER) AS vector
vector |
---|
|
Rows: 1 |
VECTOR
value with a STRING
as vectorValue
RETURN vector("[1.05000e+00, 0.123, 5]", 3, FLOAT) AS vector
vector |
---|
|
Rows: 1 |
null
valuesRETURN vector(null, 3, FLOAT32) AS nullVectorValue,
vector([1, 2, 3], null, INTEGER8) AS nullDimension
nullVectorValue | nullDimension |
---|---|
|
|
Rows: 1 |
vector.similarity.cosine()
Syntax |
|
||
Description |
Returns a |
||
Arguments |
Name |
Type |
Description |
|
|
A vector or list value representing the first vector. |
|
|
|
A vector or list value representing the second vector. |
|
Returns |
|
|
|
|
Both vectors must be of the same dimension. |
Both vectors must be valid with respect to cosine similarity. |
The implementation is the same of the latest vector index provider ( |
The similarity score ranges from |
The input arguments |
Floating point operations are performed with |
For more details, see the vector index documentation.
vector.similarity.euclidean()
Syntax |
|
||
Description |
Returns a |
||
Arguments |
Name |
Type |
Description |
|
|
A vector or list value representing the first vector. |
|
|
|
A vector or list value representing the second vector. |
|
Returns |
|
|
|
|
Both vectors must be of the same dimension. |
Both vectors must be valid with respect to Euclidean similarity. |
The implementation is the same of the latest available vector index provider ( |
The similarity score ranges from |
The input arguments |
Floating point operations are performed with |
For more details, see the vector index documentation.
k-nearest neighbor queries return the k entities with the highest similarity scores based on comparing their associated vectors with a query vector. Such queries can be run against vector indexes in the form of approximate k-nearest neighbor (k-ANN) queries, whose returned entities have a high probability of being among the true k nearest neighbors. However, they can also be expressed as an exhaustive search using vector similarity functions directly. While this is typically significantly slower than using an index, it is exact rather than approximate and does not require an existing index. This can be useful for one-off queries on small datasets.
To create the graph used in this example, run the following query on an empty Neo4j database:
CREATE
(:Node { id: 1, vector: vector([1.0, 4.0, 2.0], 3, FLOAT32) }),
(:Node { id: 2, vector: vector([3.0, -2.0, 1.0], 3, FLOAT32) }),
(:Node { id: 3, vector: vector([2.0, 8.0, 3.0], 3, FLOAT32) });
Given a parameter query
(here set to [4.0, 5.0, 6.0]
), you can query for the two nearest neighbors by Euclidean distance.
This is achieved by matching on all candidate vectors and ordering by the similarity score:
MATCH (node:Node)
WITH node, vector.similarity.euclidean($query, node.vector) AS score
RETURN node, score
ORDER BY score DESCENDING
LIMIT 2
This returns the two nearest neighbors.
node | score |
---|---|
|
|
|
|
Rows: 2 |
vector_dimension_count()Cypher 25 onlyIntroduced in Neo4j 2025.10
Syntax |
|
||
Description |
Returns the dimension of a |
||
Arguments |
Name |
Type |
Description |
|
|
The vector to calculate the dimension of. |
|
Returns |
|
You can also use the size() function to return the dimension of a VECTOR value.
|
VECTOR
RETURN vector_dimension_count(vector([1, 2, 3], 3, INTEGER8)) AS size
size |
---|
|
Rows: 1 |
vector_distance()Cypher 25 onlyIntroduced in Neo4j 2025.10
Syntax |
|
||
Description |
Returns a |
||
Arguments |
Name |
Type |
Description |
|
|
The first vector. |
|
|
|
The second vector. |
|
|
|
The vector distance algorithm to calculate the distance by. |
|
Returns |
|
Distance Type | Formula |
---|---|
|
√( (A₁ - B₁)² + (A₂ - B₂)² + … + (Aᴰ - Bᴰ)² ) |
|
(A₁ - B₁)² + (A₂ - B₂)² + … + (Aᴰ - Bᴰ)² |
|
|A₁ - B₁| + |A₂ - B₂| + … + |Aᴰ - Bᴰ| |
|
1 - ( (A₁×B₁ + A₂×B₂ + … + Aᴰ×Bᴰ) / ( √(A₁² + A₂² + … + Aᴰ²) × √(B₁² + B₂² + … + Bᴰ²) ) ) |
|
- (A₁×B₁ + A₂×B₂ + … + Aᴰ×Bᴰ) |
|
Number of dimensions in which |
The smaller the returned number, the more similar the vectors; the larger the number, the more distant the vectors.
This is in contrast to the similarity functions where the closer to |
Floating point operations are performed with |
COSINE
distanceRETURN vector_distance(vector([1, 2, 3], 3, INTEGER8), vector([1, 2, 4], 3, INTEGER8), COSINE) AS distance
distance |
---|
|
Rows: 1 |
EUCLIDEAN
distanceRETURN vector_distance(vector([1.0, 5.0, 3.0, 6.7], 4, FLOAT32), vector([5.0, 2.5, 3.1, 9.0], 4, FLOAT32), EUCLIDEAN)
distance |
---|
|
Rows: 1 |
vector_norm()Cypher 25 onlyIntroduced in Neo4j 2025.20
Syntax |
|
||
Description |
Returns a |
||
Arguments |
Name |
Type |
Description |
|
|
A vector for which the norm to the origin vector will be computed. |
|
|
|
The vector distance algorithm to calculate the distance by. |
|
Returns |
|
Distance Type | Formula |
---|---|
|
√( (A₁ - B₁)² + (A₂ - B₂)² + … + (Aᴰ - Bᴰ)² ) |
|
|A₁ - B₁| + |A₂ - B₂| + … + |Aᴰ - Bᴰ| |
EUCLIDEAN
distanceFloating point operations are performed with |
RETURN vector_norm(vector([1.0, 5.0, 3.0, 6.7], 4, FLOAT32), EUCLIDEAN) AS norm
norm |
---|
|
Rows: 1 |
EUCLIDEAN
distanceRETURN vector_norm(vector([1.0, 5.0, 3.0, 6.7], 4, FLOAT32), MANHATTAN) AS norm
norm |
---|
|
Rows: 1 |