Vector Functions
- Last Updated: April 16, 2026
- 4 minute read
- MarkLogic Server
- Version 12.0
- Documentation
This chapter describes the supported vector functions:
Overview of Vectors
In simple terms, vectors are arrays of numbers representing a specific point in an N-dimensional space. These points represent a given data for a specific model. In large language models (LLM), these vectors encode various features of the data, such as the semantic meaning of words, sentences, or entire documents. Vectors can undergo various mathematical operations, such as addition, scalar multiplication, and dot products, which are essential for tasks like similarity searches.
Vectors and corresponding operations also apply to systems related to image and video recognition, genomics, financial services like risk assessment, fraud detection, and algorithmic trading.
Vector constructors
Vectors are essentially an array of numbers. To differentiate vectors from your native array of numbers, a special constructor is available, vec.vector.
vec.vector([3.14, 1.59, 2.65])
The construction of this object is required when using the various operators for vectors.
Vector arithmetic
Vector operations may be performed between vectors of equal dimensions. This table describes the supported vector functions.
| Function | Description | Example |
vec.add |
If a = [ a1, a2, a3 ] and b = [ b1, b2, b3 ], then:
|
vec.add( vec.vector([3.14, 1.59, 2.65]), vec.vector([3.58, 9.79, 3.23]) ) |
vec.subtract |
If a = [ a1, a2, a3 ] and b = [ b1, b2, b3 ], then:
|
vec.subtract( vec.vector([3.14, 1.59, 2.65]), vec.vector([3.58, 9.79, 3.23]) ) |
vec.dotProduct |
If a = [ a1, a2, a3 ] and b = [ b1, b2, b3 ], then:
The dot product is useful for finding the angle between two vectors and determining if they are orthogonal (perpendicular). |
vec.dotProduct( vec.vector([3.14, 1.59, 2.65]), vec.vector([3.58, 9.79, 3.23]) ) |
vec.magnitude |
If a = [ a1, a2, a3 ], then:
This represents the length of the vector. |
vec.magnitude( vec.vector([3.14, 1.59, 2.65]) ) |
vec.normalize |
If a = [ a1, a2, a3 ], then:
Also known as a unit vector. This is a vector with a magnitude of 1. It is obtained by dividing a vector by its magnitude. |
vec.normalize( vec.vector([3.14, 1.59, 2.65]) ) |
vec.euclideanDistance |
If a = [ a1, a2, ... an ] and b = [ b1, b2, ... bn ], then:
Euclidean Distance calculates the straight-line distance between two points in Euclidean space, considering both magnitude and direction. This metric is often used in clustering algorithms to determine how far apart data points are. |
vec.euclideanDistance( vec.vector([3.14, 1.59, 2.65]), vec.vector([3.58, 9.79, 3.23]) ) |
vec.cosine |
If a = [ a1, a2, ... an ] and b = [ b1, b2, ... bn ], then:
Cosine Similarity measures the cosine of the angle between two vectors, focusing on the direction rather than the magnitude. It's particularly useful in text analysis and recommendation systems where the orientation of vectors matters more than their length. |
vec.cosine( vec.vector([3.14, 1.59, 2.65]), vec.vector([3.58, 9.79, 3.23]) ) |
More information about various vector functions is available at docs.marklogic.com
Vector score
In a Retrieval Augmentation Generation (RAG) application, it is advantageous to combine ranking from different retrieval techniques to produce a "hybrid" or "fused" relevance rank. This approach is also know as Reciprocal Rank Fusion. The function vec.vectorScore is a helper function that combines a CTS score with the evaluated cosine distance to calculate a hybrid score. Assuming a document with an vector property of textEmbedding:
...
let score = cts.score(resultItem)
let distance = vec.cosineDistance(queryVector, vec.vector(resultItem.envelope.instance.emb))
let vectorScore = vec.vectorScore(score, distance)
vectorScore
- Refer to Use of an Embedding Model regarding definition of the function
getVectorEquivalent(). It is important that you use the same LLM model for your embeddings and your query text.
This vectorScore, also known as Hybrid Score, can be used to rerank your search results. The impact of CTS score and vector distance can be tweaked using the weight parameters that can be supplied.
let distanceWeight = 1
let weight = .5
let vectorScore = vec.vectorScore(score, distance, distanceWeight, weight)
vectorScore
The vectorScore is computed using the formula weight * annScore + (1 - weight) * ctsScore.
distanceWeight is a value that scales the value of distance. A higher value results in a lower annScore.
weight is a floating-point value between 0 and 1. A higher value emphasizes the influence of vector distance, while a lower value emphasizes the influence of CTS score.
For indexed search of vectors using the Optic API, refer to Building Vector Queries.



