Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Important
The AI function guidance in this article uses the ai schema in the azure_ai extension version 2.5.6 or later. Pipeline builders use the ai.*_step() names. For earlier versions or existing azure_ai.* and azure_openai.create_embeddings() calls, see Migrate AI functions to the ai schema for version checks, API differences, upgrade steps, and the HorizonDB legacy reference.
Vector search retrieves results that are semantically close to a query, but "close" in embedding space doesn't always mean "relevant" to the user's intent. Synonyms, intent shifts, long-tail phrasing, and domain-specific nuance can cause the most relevant document to rank third or tenth instead of first. Users judge results by how well they match their intent, not by raw distance scores.
Semantic reranking addresses this problem. After an initial retrieval stage (vector search, full-text search, or hybrid search) returns a broad set of candidates, a cross-encoder reranker model rescors each candidate against the original query. Unlike embedding models that encode the query and document separately, a cross-encoder processes the query and document together, capturing fine-grained interactions between them and producing more accurate relevance scores.
The ai.rank() function in the azure_ai extension brings this capability directly into SQL, so you can add a reranking stage without leaving the database.
Why reranking matters
The gap between distance and relevance
Vector embeddings are optimized for fast, approximate similarity at scale. They encode meaning into fixed-size vectors independently, one for the query, one for each document. This bi-encoder approach is efficient (you can index billions of vectors with DiskANN), but it compresses away fine-grained interactions between query and document.
A cross-encoder, by contrast, processes the query and document as a single input pair. It attends to every word in both, capturing token-level interactions that bi-encoders miss. The result is more accurate relevance scores, but at higher computational cost, because every candidate requires a separate inference call.
Why not use a cross-encoder for everything?
Cross-encoders are accurate but expensive. Scoring 1 million documents with a cross-encoder at query time is impractical as it would take seconds to minutes per query. That's why production retrieval uses a two-stage pipeline:
- Stage 1: Retrieve a broad set of candidates cheaply (vector search, BM25, or hybrid search). This step narrows millions of documents down to the top 50-100.
- Stage 2: Rerank only those 50-100 candidates with a cross-encoder for precision on the results that matter most.
This pattern gives you the speed of embedding-based retrieval with the accuracy of cross-encoder scoring, at a fraction of the cost of running the cross-encoder over the entire corpus.
When to use semantic reranking
| Use reranking when | Skip reranking when |
|---|---|
| Search quality directly affects user experience (product search, support search, knowledge base) | Simple exact-match lookups (product code, ID search) |
| Queries are natural language with nuance, synonyms, or intent variation | The corpus is small and homogeneous enough that vector search alone achieves high precision |
| You're building RAG or Implement durable AI pipelines in Azure HorizonDB and need the best possible context for large language models (LLM) generation | Latency budget can't accommodate the additional model call |
| Hybrid search returns a fused list and you want a final accuracy bump | Results are already filtered to a small set (fewer than 5) |
Prerequisites
An Azure HorizonDB instance with either:
- AI Model Management (Preview) enabled:
- It automatically provisions a
default-rerankermodel (Cohere-rerank-v4.0-fast) ready to use.
- It automatically provisions a
- The
azure_aiextension installed with a reranker model registered via the model registry. See Manual setup with model registry.
Use azure_ai 2.5.6 or later. For earlier versions and existing callers, follow Migrate AI functions to the ai schema. The vector examples also require an embedding model and stored vectors generated with the same model and dimensions as the query embeddings. See Generate vector embeddings.
The rank() function
The ai.rank() function reranks a set of documents based on their relevance to a query using a cross-encoder model.
Syntax
ai.rank(
query text,
data text[],
top_n integer DEFAULT NULL,
model text DEFAULT 'default-reranker'
)
RETURNS TABLE (id bigint, rank bigint, score double precision)
ai.rank(
query text,
data text[],
ids anyarray,
top_n integer DEFAULT NULL,
model text DEFAULT 'default-reranker'
)
RETURNS TABLE (id anyelement, rank bigint, score double precision)
Arguments
| Argument | Type | Description |
|---|---|---|
query |
text |
The search query to evaluate relevance against. |
data |
text[] |
Array of document texts to rerank. |
ids |
anyarray |
Optional through the overload with IDs. Supply one non-NULL identifier per document in the same order as data. |
top_n (optional) |
integer |
Maximum results to return. Defaults to NULL (all results); an explicit value must be greater than zero. |
model (optional) |
text |
Model alias registered in the model registry. When omitted, uses the default-reranker Managed Model (Cohere-rerank-v4.0-fast). |
Return type
Returns a table with columns: id, rank, and score. Without ids, id is the zero-based input-document index. With ids, id has the element type of the supplied array.
Note
BYOM users: Pass your registered reranker model alias by name: ai.rank(query => 'query', data => documents, ids => document_ids, model => 'my-reranker'). See AI functions in the azure_ai extension for Azure HorizonDB for details on registering models.
Basic reranking example
Rerank a set of product descriptions against a search query:
SELECT * FROM ai.rank(
query => 'wireless noise cancelling headphones',
data => ARRAY[
'Over-ear wireless headphones with active noise cancellation and 30-hour battery life.',
'Wired earbuds with inline microphone, no noise cancellation.',
'Bluetooth speaker with 360-degree sound and waterproof design.',
'Compact noise cancelling earbuds with transparency mode and wireless charging case.'
]
);
Note
BYOM users: Add model => 'my-reranker' to the ai.rank() call.
Two-stage retrieval: vector search + reranking
The most common pattern is to retrieve candidates with vector search, then rerank the top results for precision:
WITH candidates AS (
SELECT id, title, description
FROM products
ORDER BY embedding <=> ai.embed(
text => 'wireless noise cancelling headphones'
)::vector
LIMIT 20
),
reranked AS (
SELECT *
FROM ai.rank(
query => 'wireless noise cancelling headphones',
data => ARRAY(SELECT description FROM candidates ORDER BY id),
ids => ARRAY(SELECT id FROM candidates ORDER BY id)
)
)
SELECT c.id, c.title, r.rank, r.score
FROM candidates c
JOIN reranked r ON r.id = c.id
ORDER BY r.rank ASC
LIMIT 10;
Note
BYOM users: Add model => 'my-embedding' to ai.embed() and model => 'my-reranker' to ai.rank(). Match the embedding model and dimensions to the stored vectors.
Hybrid search + reranking
For the best retrieval quality, combine BM25 full-text search and vector search with Reciprocal Rank Fusion, then rerank the fused results. If you want to run this pattern as a durable, fault-tolerant workflow with automatic retries and checkpointing, see Implement durable AI pipelines in Azure HorizonDB - which supports ai.rank_step() as a built-in pipeline step in azure_ai 2.5.6 or later.
WITH query AS (
SELECT
'wireless noise cancelling headphones' AS q_text,
ai.embed(
text => 'wireless noise cancelling headphones'
)::vector AS q_vec
),
bm25 AS (
SELECT p.id,
ROW_NUMBER() OVER (
ORDER BY p.description <@> to_bm25query(query.q_text, 'idx_products_bm25')
) AS bm25_rank
FROM products p, query
ORDER BY p.description <@> to_bm25query(query.q_text, 'idx_products_bm25')
LIMIT 50
),
vec AS (
SELECT p.id, ROW_NUMBER() OVER (ORDER BY p.embedding <=> query.q_vec) AS vec_rank
FROM products p, query
ORDER BY p.embedding <=> query.q_vec
LIMIT 50
),
fused AS (
SELECT p.id, p.description,
(1.0 / (60 + COALESCE(b.bm25_rank, 1000))) +
(1.0 / (60 + COALESCE(v.vec_rank, 1000))) AS rrf_score
FROM products p
LEFT JOIN bm25 b ON b.id = p.id
LEFT JOIN vec v ON v.id = p.id
WHERE b.id IS NOT NULL OR v.id IS NOT NULL
ORDER BY rrf_score DESC
LIMIT 20
),
reranked AS (
SELECT *
FROM ai.rank(
query => 'wireless noise cancelling headphones',
data => ARRAY(SELECT description FROM fused ORDER BY id),
ids => ARRAY(SELECT id FROM fused ORDER BY id)
)
)
SELECT f.id, f.description, r.rank, r.score
FROM fused f
JOIN reranked r ON r.id = f.id
ORDER BY r.rank ASC
LIMIT 10;
Note
BYOM users: Pass your model aliases by name to ai.embed() and ai.rank() as shown in previous examples.
Performance considerations
| Factor | Recommendation |
|---|---|
| Candidate pool size | Rerank 20-50 candidates. More candidates improve recall but increase latency and cost linearly. |
| Latency | Cross-encoder scoring adds tens to low hundreds of milliseconds depending on the pool size and document length. |
| Document length | Shorter documents rerank faster. If documents are long, consider reranking over summaries or the most relevant chunk rather than the full text. |
| When to skip | If your retrieval stage already returns fewer than five results, reranking adds cost with diminishing accuracy gains. |