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()

Details

Syntax

vector(vectorValue, dimension, coordinateType)

Description

Constructs a VECTOR value.

Arguments

Name

Type

Description

vectorValue

STRING | LIST<INTEGER | FLOAT>

The numeric values to create the vector coordinates from.

dimension

INTEGER

The dimension (number of coordinates) of the vector.

coordinateType

[INTEGER64, INTEGER32, INTEGER16, INTEGER8, FLOAT64, FLOAT32]

The type of each coordinate in the vector.

Returns

VECTOR

Considerations

VECTOR values can be stored as properties.

If a STRING is used in vectorValue, it must start and end with square brackets ([]). The values inside the brackets must be comma-separated numbers, represented in either decimal or scientific notation.

null, NaN, and Infinity values are not allowed as coordinate values.

If vectorValue contains elements that are not of the specified coordinateType, they will be coerced to that coordinate type if possible. This includes the potential of lossy conversion in cases where a larger type, e.g. INTEGER64 does not fit into the specified type, e.g. FLOAT32.

dimension must be greater than 0 and less than or equal to 4096.

A null vectorValue or dimension will return null.

Example 1. Construct a VECTOR value
Query
RETURN vector([1, 2, 3], 3, INTEGER) AS vector
Result
vector

vector([1, 2, 3], 3, INTEGER NOT NULL)

Rows: 1

Example 2. Construct a VECTOR value with a STRING as vectorValue
Query
RETURN vector("[1.05000e+00, 0.123, 5]", 3, FLOAT) AS vector
Result
vector

vector([1.05, 0.123, 5.0], 3, FLOAT NOT NULL)

Rows: 1

Example 3. null values
Query
RETURN vector(null, 3, FLOAT32) AS nullVectorValue,
       vector([1, 2, 3], null, INTEGER8) AS nullDimension
Result
nullVectorValue nullDimension

null

null

Rows: 1

vector.similarity.cosine()

Details

Syntax

vector.similarity.cosine(a, b)

Description

Returns a FLOAT representing the similarity between the argument vectors based on their cosine.

Arguments

Name

Type

Description

a

VECTOR | LIST<INTEGER | FLOAT>

A vector or list value representing the first vector.

b

VECTOR | LIST<INTEGER | FLOAT>

A vector or list value representing the second vector.

Returns

FLOAT

Considerations

vector.similarity.cosine(null, null) returns null.

vector.similarity.cosine(null, b) returns null.

vector.similarity.cosine(a, null) returns null.

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 (vector-2.0).

The similarity score ranges from 0 and 1, with scores closer to 1 indicating a higher degree of similarity.

The input arguments a and b accept VECTOR values as of Neo4j 2025.10.

Floating point operations are performed with float32 arithmetic.

For more details, see the vector index documentation.

vector.similarity.euclidean()

Details

Syntax

vector.similarity.euclidean(a, b)

Description

Returns a FLOAT representing the similarity between the argument vectors based on their Euclidean distance.

Arguments

Name

Type

Description

a

VECTOR | LIST<INTEGER | FLOAT>

A vector or list value representing the first vector.

b

VECTOR | LIST<INTEGER | FLOAT>

A vector or list value representing the second vector.

Returns

FLOAT

Considerations

vector.similarity.euclidean(null, null) returns null.

vector.similarity.euclidean(null, b) returns null.

vector.similarity.euclidean(a, null) returns null.

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 (vector-2.0).

The similarity score ranges from 0 and 1, with scores closer to 1 indicating a higher degree of similarity.

The input arguments a and b accept VECTOR values as of Neo4j 2025.10.

Floating point operations are performed with float32 arithmetic.

For more details, see the vector index documentation.

Example 4. k-Nearest Neighbors

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

(:Node {vector: vector([2.0, 8.0, 3.0], 3, FLOAT32 NOT NULL), id: 3})

0.043478261679410934

(:Node {vector: vector([1.0, 4.0, 2.0], 3, FLOAT32 NOT NULL), id: 1})

0.03703703731298447

Rows: 2

vector_dimension_count()

Details

Syntax

vector_dimension_count(vector)

Description

Returns the dimension of a VECTOR.

Arguments

Name

Type

Description

vector

VECTOR

The vector to calculate the dimension of.

Returns

INTEGER

You can also use the size() function to return the dimension of a VECTOR value.
Example 5. Calculate the size of a VECTOR
Query
RETURN vector_dimension_count(vector([1, 2, 3], 3, INTEGER8)) AS size
Result
size

3

Rows: 1

vector_distance()

Details

Syntax

vector_distance(vector1, vector2, vectorDistanceMetric)

Description

Returns a FLOAT representing the distance between the two vector values based on the selected vectorDistanceMetric algorithm.

Arguments

Name

Type

Description

vector1

VECTOR

The first vector.

vector2

VECTOR

The second vector.

vectorDistanceMetric

[EUCLIDEAN, EUCLIDEAN_SQUARED, MANHATTAN, COSINE, DOT, HAMMING]

The vector distance algorithm to calculate the distance by.

Returns

FLOAT

Supported vectorDistanceMetric algorithms
Distance Type Formula

EUCLIDEAN

√( (A₁ - B₁)² + (A₂ - B₂)² + …​ + (Aᴰ - Bᴰ)² )

EUCLIDEAN_SQUARED

(A₁ - B₁)² + (A₂ - B₂)² + …​ + (Aᴰ - Bᴰ)²

MANHATTAN

|A₁ - B₁| + |A₂ - B₂| + …​ + |Aᴰ - Bᴰ|

COSINE

1 - ( (A₁×B₁ + A₂×B₂ + …​ + Aᴰ×Bᴰ) / ( √(A₁² + A₂² + …​ + Aᴰ²) × √(B₁² + B₂² + …​ + Bᴰ²) ) )

DOT

- (A₁×B₁ + A₂×B₂ + …​ + Aᴰ×Bᴰ)

HAMMING

Number of dimensions in which vector1 and vector2 differ.

Considerations

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 1 the result is the higher the degree of similarity.

Floating point operations are performed with float32 arithmetic.

Example 6. Calculate the distance between two vectors using the COSINE distance
Query
RETURN vector_distance(vector([1, 2, 3], 3, INTEGER8), vector([1, 2, 4], 3, INTEGER8), COSINE) AS distance
Result
distance

0.008539855480194092

Rows: 1

Example 7. Calculate the distance between two vectors using the EUCLIDEAN distance
Query
RETURN 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)
Result
distance

5.248809337615967

Rows: 1

vector_norm()

Details

Syntax

vector_norm(vector, vectorDistanceMetric)

Description

Returns a FLOAT representing the distance between the given vector and an origin vector, which is a vector with the same dimension with all coordinates set to zero, calculated using the specified vectorDistanceMetric.

Arguments

Name

Type

Description

vector

VECTOR

A vector for which the norm to the origin vector will be computed.

vectorDistanceMetric

[EUCLIDEAN, MANHATTAN]

The vector distance algorithm to calculate the distance by.

Returns

FLOAT

Supported vectorDistanceMetric algorithms
Distance Type Formula

EUCLIDEAN

√( (A₁ - B₁)² + (A₂ - B₂)² + …​ + (Aᴰ - Bᴰ)² )

MANHATTAN

|A₁ - B₁| + |A₂ - B₂| + …​ + |Aᴰ - Bᴰ|

Example 8. Measure the norm between a vector and an origin vector using the EUCLIDEAN distance
Considerations

Floating point operations are performed with float32 arithmetic.

Query
RETURN vector_norm(vector([1.0, 5.0, 3.0, 6.7], 4, FLOAT32), EUCLIDEAN) AS norm
Result
norm

8.93812084197998

Rows: 1

Example 9. Measure the norm between a vector and an origin vector using the EUCLIDEAN distance
Query
RETURN vector_norm(vector([1.0, 5.0, 3.0, 6.7], 4, FLOAT32), MANHATTAN) AS norm
Result
norm

15.699999809265137

Rows: 1