Vector / Similarity Functions
Pinot provides built-in vector similarity and utility functions for working with float array columns. These functions support nearest-neighbor search, recommendation systems, retrieval-augmented generation (RAG), and any use case involving embedding vectors.
Both input vectors must have the same number of dimensions. Passing null or mismatched-length vectors results in an error.
VECTOR_SIMILARITY
VECTOR_SIMILARITY is a WHERE-clause predicate that performs approximate nearest-neighbor (ANN) search using a vector index. It is not a standalone function—it acts as a filter that returns the top-K documents whose vectors are closest to the given query vector.
Syntax
WHERE VECTOR_SIMILARITY(vectorColumn, queryVector, topK)queryVector
ARRAY[...]
A float array literal representing the query embedding.
topK
integer literal
Number of nearest neighbors to retrieve. Defaults to 10 if omitted.
Prerequisites
VECTOR_SIMILARITY uses a vector index when one is available on the target column. Pinot supports four backends: HNSW, IVF_FLAT, IVF_PQ, and IVF_ON_DISK. If a segment does not have a vector index (e.g., realtime segments with IVF_FLAT, IVF_PQ, or IVF_ON_DISK), Pinot falls back to an exact scan over the forward index for that segment. Exact scans are much slower than ANN lookups, so configure a vector index for production workloads. See the vector index documentation for setup instructions.
Minimal field config:
{
"fieldConfigList": [
{
"name": "embedding",
"encodingType": "RAW",
"indexes": {
"vector": {
"vectorIndexType": "HNSW",
"vectorDimension": 512,
"vectorDistanceFunction": "COSINE",
"version": 1
}
}
}
]
}Examples
Find the 5 nearest products to a query embedding and rank by cosine distance:
Combine with metadata filters — retrieve 20 ANN candidates and then filter by category:
When Pinot uses a vector index, VECTOR_SIMILARITY is an approximate nearest-neighbor predicate. vectorExactRerank=true re-scores the ANN candidates returned by the index, but it does not turn ANN search into a full exact scan. IVF_PQ defaults to vectorExactRerank=true because PQ distances are approximate by construction. To get better recall, request a larger topK than the final LIMIT and combine with an ORDER BY on a distance function.
VECTOR_SIMILARITY_RADIUS
VECTOR_SIMILARITY_RADIUS is a WHERE-clause predicate that returns all vectors within a distance threshold, without requiring a fixed top-K. It is useful when you want every result meeting a quality threshold rather than an arbitrary number of nearest neighbors.
Syntax
queryVector
ARRAY[...]
A float array literal representing the query embedding.
distanceThreshold
float literal
Maximum distance for a vector to be included in results.
Behavior
On segments with a vector index, Pinot uses the ANN index to generate candidates and then filters by exact distance. On segments without a vector index, Pinot falls back to a brute-force scan, returning all vectors within the threshold exactly.
Examples
Find all products within cosine distance 0.3 of a query embedding:
Combine radius search with metadata filters:
Distance Functions
cosineDistance
Returns the cosine distance between two vectors, defined as 1 - cosine_similarity. The result ranges from 0 (identical direction) to 2 (opposite direction).
If either vector has a norm of zero, the two-argument form returns NaN while the three-argument form returns the specified defaultValue.
innerProduct
Returns the inner product (sum of element-wise products) of two vectors. Useful when embeddings are pre-normalized and higher scores indicate greater similarity.
l1Distance
Returns the L1 distance (Manhattan distance) between two vectors, computed as the sum of absolute differences of their components.
l2Distance
Returns the L2 distance (Euclidean distance) between two vectors, computed as the square root of the sum of squared differences.
euclideanDistance
Returns the squared Euclidean distance between two vectors (the sum of squared differences without the square root). This is computationally cheaper than l2Distance when you only need to compare relative distances, since omitting the square root preserves the ordering.
dotProduct
Returns the dot product of two vectors. Functionally equivalent to innerProduct.
Utility Functions
vectorDims
Returns the number of dimensions (length) of a vector.
vectorNorm
Returns the L2 norm (Euclidean length) of a vector, computed as the square root of the sum of squared components.
End-to-End Example: Semantic Search
This example walks through setting up a table for semantic search over product reviews, from schema definition to querying.
1. Define the schema
The embedding column must be a multi-valued FLOAT field:
2. Configure the table with a vector index
Enable the HNSW vector index on the embedding column. Choose a distance function that matches how your embeddings were produced—COSINE is the most common choice for normalized text embeddings.
3. Query with vector similarity
Use VECTOR_SIMILARITY to retrieve nearest neighbors and a distance function to rank results:
This query first uses the HNSW index to retrieve the 10 approximate nearest neighbors, then orders those candidates by exact cosine distance and returns the top 5.
Function Summary
VECTOR_SIMILARITY(col, query, topK)
predicate
ANN filter — returns top-K nearest neighbors.
VECTOR_SIMILARITY_RADIUS(col, query, threshold)
predicate
Distance-based filter — returns all vectors within the threshold.
cosineDistance(v1, v2 [, default])
DOUBLE
Cosine distance (1 - cosine_similarity).
innerProduct(v1, v2)
DOUBLE
Inner product (sum of element-wise products).
l1Distance(v1, v2)
DOUBLE
Manhattan distance.
l2Distance(v1, v2)
DOUBLE
Euclidean distance (with square root).
euclideanDistance(v1, v2)
DOUBLE
Squared Euclidean distance (no square root).
dotProduct(v1, v2)
DOUBLE
Dot product (equivalent to innerProduct).
vectorDims(v)
INT
Number of vector dimensions.
vectorNorm(v)
DOUBLE
L2 norm (magnitude) of a vector.
Last updated
Was this helpful?

