Multi-Stage Reranking
Vector search ranks candidates by vector similarity. Applications may also need to account for other factors, such as popularity or freshness. Multi-stage reranking lets you apply different ranking logic at different points in a search, before and after candidate results are merged.
In Zilliz Cloud, you configure this logic using Function Chains. For ordinary vector search, you can use up to one chain at each of the L0, L1, and L2 stages in a single request. This guide explains the stages and walks through a news-search example that uses all three.
How multi-stage reranking works
Zilliz Cloud divides collection data into segments and searches them on Query Nodes. Each Query Node merges results from its segments. Zilliz Cloud then merges the results from the Query Nodes participating in the search. The reranking stages run at different points in this process:
| Stage | When it runs | Candidates it processes |
|---|---|---|
L0_RERANK | On each Query Node, before its segment results are merged. | The results of one segment at a time. |
L1_RERANK | On each Query Node, after its segment results are merged. | The candidates retained by that Query Node’s local merge. |
L2_RERANK | On the Proxy, after distributed results are merged. | The globally merged candidates that reach L2. |
The following diagram illustrates how Zilliz Cloud applies reranking at the segment, Query Node, and global levels during a search.

For example, a news search can combine similarity and popularity at L0, incorporate freshness at L1, and sort the global results at L2. Each stage has its own chain. The example below follows this pipeline.
A later stage cannot recover a candidate discarded by an earlier retrieval, merge, or limit. An L2 model only evaluates candidates that reach L2.
Function Chain operations
To implement multi-stage reranking, use a Function Chain to define the processing logic for each reranking stage you want to use. Each chain contains an ordered sequence of operations that process candidates or their values. You can configure different operations at different stages within the same search request.
Operation | Description | Example | Available stages and guidance |
|---|---|---|---|
| Calculates a value for each candidate using an expression and writes the result to an output column. Expressions include:
| Use | L0, L1, or L2. Expression availability depends on the stage; for example, Use L0 to let a signal influence early candidate selection, L1 after a Query Node merges its segment results, or L2 after the distributed merge. |
| Orders candidates by a specified column. | Sort candidates by | L1 or L2. Use L2 to order the globally merged candidates. At L1, |
| Keeps a specified number of candidates, optionally skipping an offset. | Keep the top 10 candidates after sorting. | L1 or L2. Use L2 to trim the global result set. L1 limits candidates independently on each Query Node and can reduce recall if set too low. |
| Combines results from multiple search requests in Hybrid Search. | Combine dense and sparse search results using RRF. | Top-level L2 chain in Hybrid Search. It must be the first operation in that chain; ordinary single-vector search does not use it. |
Example: Rerank news articles
This example uses Function Chains to implement multi-stage reranking for a news search application. It configures one chain at each stage:
-
L0 combines the ANN score and popularity.
-
L1 incorporates freshness into the resulting score.
-
L2 sorts and limits the globally merged candidates.
Before you start
Before using this feature, make sure your Zilliz Cloud cluster is upgraded to Milvus 3.0.x (Public Preview).
Create a collection
Create an articles collection with a VARCHAR primary key, a title, a two-dimensional vector field, and numeric popularity and freshness fields. Index the vector field using the inner product (IP) metric.
from pymilvus import MilvusClient, DataType
client = MilvusClient(uri="YOUR_CLUSTER_ENDPOINT")
schema = client.create_schema(auto_id=False, enable_dynamic_field=False)
schema.add_field(field_name="id", datatype=DataType.VARCHAR, is_primary=True, max_length=10)
schema.add_field(field_name="title", datatype=DataType.VARCHAR, max_length=200)
schema.add_field(field_name="embedding", datatype=DataType.FLOAT_VECTOR, dim=2)
schema.add_field(field_name="popularity", datatype=DataType.FLOAT)
schema.add_field(field_name="freshness", datatype=DataType.FLOAT)
index_params = client.prepare_index_params()
index_params.add_index(field_name="embedding", index_type="FLAT", metric_type="IP")
client.create_collection(
collection_name="articles", schema=schema, index_params=index_params,
)
Insert sample data
Insert 15 news articles. Each record contains a title, a two-dimensional embedding, a popularity score, and a freshness score. The following table shows the data used throughout this example.
| id | title | embedding | popularity | freshness |
|---|---|---|---|---|
| A | Solar power storage | [1.80, 3.60] | 0.20 | 0.20 |
| B | Renewable energy breakthroughs | [1.60, 3.20] | 0.80 | 0.90 |
| C | Wind energy investment | [1.40, 2.80] | 0.90 | 0.30 |
| D | Battery technology update | [1.70, 3.40] | 0.40 | 0.70 |
| E | Clean energy policy | [1.30, 2.60] | 0.95 | 0.85 |
| F | Solar farms expand | [1.50, 3.00] | 0.30 | 0.60 |
| G | Grid storage research | [1.20, 2.40] | 0.70 | 0.95 |
| H | Energy market outlook | [1.90, 3.80] | 0.10 | 0.40 |
| I | Wind turbine innovation | [1.10, 2.20] | 0.85 | 0.65 |
| J | Hydrogen energy advances | [1.44, 2.88] | 0.60 | 0.75 |
| K | New solar battery | [1.76, 3.52] | 0.90 | 0.90 |
| L | Offshore wind project | [1.64, 3.28] | 0.50 | 0.40 |
| M | Energy storage funding | [1.36, 2.72] | 0.65 | 0.55 |
| N | Coal plant retirement | [1.00, 2.00] | 0.20 | 0.10 |
| O | Regional power demand | [1.56, 3.12] | 0.20 | 0.15 |
Insert the sample records and load the collection:
data = [
{"id": "A", "title": "Solar power storage", "embedding": [1.80, 3.60], "popularity": 0.20, "freshness": 0.20},
{"id": "B", "title": "Renewable energy breakthroughs", "embedding": [1.60, 3.20], "popularity": 0.80, "freshness": 0.90},
{"id": "C", "title": "Wind energy investment", "embedding": [1.40, 2.80], "popularity": 0.90, "freshness": 0.30},
{"id": "D", "title": "Battery technology update", "embedding": [1.70, 3.40], "popularity": 0.40, "freshness": 0.70},
{"id": "E", "title": "Clean energy policy", "embedding": [1.30, 2.60], "popularity": 0.95, "freshness": 0.85},
{"id": "F", "title": "Solar farms expand", "embedding": [1.50, 3.00], "popularity": 0.30, "freshness": 0.60},
{"id": "G", "title": "Grid storage research", "embedding": [1.20, 2.40], "popularity": 0.70, "freshness": 0.95},
{"id": "H", "title": "Energy market outlook", "embedding": [1.90, 3.80], "popularity": 0.10, "freshness": 0.40},
{"id": "I", "title": "Wind turbine innovation", "embedding": [1.10, 2.20], "popularity": 0.85, "freshness": 0.65},
{"id": "J", "title": "Hydrogen energy advances", "embedding": [1.44, 2.88], "popularity": 0.60, "freshness": 0.75},
{"id": "K", "title": "New solar battery", "embedding": [1.76, 3.52], "popularity": 0.90, "freshness": 0.90},
{"id": "L", "title": "Offshore wind project", "embedding": [1.64, 3.28], "popularity": 0.50, "freshness": 0.40},
{"id": "M", "title": "Energy storage funding", "embedding": [1.36, 2.72], "popularity": 0.65, "freshness": 0.55},
{"id": "N", "title": "Coal plant retirement", "embedding": [1.00, 2.00], "popularity": 0.20, "freshness": 0.10},
{"id": "O", "title": "Regional power demand", "embedding": [1.56, 3.12], "popularity": 0.20, "freshness": 0.15},
]
client.insert(collection_name="articles", data=data)
client.flush(collection_name="articles")
client.load_collection(collection_name="articles")
Configure Function Chains
At L0 and L1, each chain uses map with the num_combine expression to calculate a weighted score and write it to $score. L0 combines the ANN score with popularity; L1 combines the resulting score with freshness. After the candidates are merged, the L2 chain sorts them by their current scores and keeps up to 10 results.
Before combining scores, consider the range and distribution of each input, including the vector similarity score. Normalize inputs where appropriate so that the weights have the intended effect. In this example, popularity and freshness values range from 0 to 1; IP scores are not necessarily restricted to that range.
from pymilvus.function_chain import FunctionChain, FunctionChainStage, col, fn
l0 = (
FunctionChain(FunctionChainStage.L0_RERANK, name="popularity")
.map("$score", fn.num_combine(
col("$score"), col("popularity"),
mode="weighted", weights=[0.8, 0.2],
))
)
l1 = (
FunctionChain(FunctionChainStage.L1_RERANK, name="freshness")
.map("$score", fn.num_combine(
col("$score"), col("freshness"),
mode="weighted", weights=[0.8, 0.2],
))
)
l2 = (
FunctionChain(FunctionChainStage.L2_RERANK, name="final_order")
.sort("$score", desc=True, tie_break_col="$id")
.limit(10)
)
Run vector search
Search the embedding field using the query vector and apply the three chains:
query_vector = [0.1, 0.2]
results = client.search(
collection_name="articles",
data=[query_vector],
anns_field="embedding",
search_params={"metric_type": "IP"},
limit=100,
output_fields=["title", "popularity", "freshness"],
function_chains=[l0, l1, l2],
)
print(results)
Zilliz Cloud executes the chains in L0 → L1 → L2 stage order. Each stage can have at most one chain in an ordinary search request; place multiple operations inside that chain when needed. L0 and L1 rewrite the score in this example. L2 sorts and limits the candidates; it does not calculate a new score.
Understand the results
The following walkthrough traces the 15 articles through the reranking stages, using the query vector [0.1, 0.2].
The tables illustrate one arrangement of articles across segments and Query Nodes. Actual segment placement and intermediate candidates depend on the deployment and search; running the code does not guarantee the same arrangement or intermediate results.
The vector values illustrate the IP calculation: for each record, the dot product with [0.1, 0.2] produces the ANN score shown below. Query Node 1 searches segments 1 and 2; Query Node 2 searches segments 3 and 4.
1. Initial segment candidates
Query Node / segment | Segment rank | Article ID | ANN score (IP) |
|---|---|---|---|
Query Node 1 / segment 1 | 1 | A | 0.90 |
2 | D | 0.85 | |
3 | B | 0.80 | |
4 | C | 0.70 | |
Query Node 1 / segment 2 | 1 | H | 0.95 |
2 | F | 0.75 | |
3 | E | 0.65 | |
4 | G | 0.60 | |
Query Node 2 / segment 3 | 1 | K | 0.88 |
2 | L | 0.82 | |
3 | J | 0.72 | |
4 | I | 0.55 | |
Query Node 2 / segment 4 | 1 | O | 0.78 |
2 | M | 0.68 | |
3 | N | 0.50 |
Each ANN score is the inner product of the query vector and the article embedding. For example, article A scores 0.1 × 1.80 + 0.2 × 3.60 = 0.90. Article H has the highest initial ANN score.
2. L0: Combine similarity and popularity
L0 runs separately on each segment. For each candidate, it overwrites $score with 0.8 × ANN score + 0.2 × popularity. The following table groups candidates by segment and displays them in descending L0 score for comparison; the L0 chain uses map and does not explicitly call sort.
Query Node / segment | Article ID | ANN score | Popularity | L0 score |
|---|---|---|---|---|
Query Node 1 / segment 1 | B | 0.80 | 0.80 | 0.800 |
A | 0.90 | 0.20 | 0.760 | |
D | 0.85 | 0.40 | 0.760 | |
C | 0.70 | 0.90 | 0.740 | |
Query Node 1 / segment 2 | H | 0.95 | 0.10 | 0.780 |
E | 0.65 | 0.95 | 0.710 | |
F | 0.75 | 0.30 | 0.660 | |
G | 0.60 | 0.70 | 0.620 | |
Query Node 2 / segment 3 | K | 0.88 | 0.90 | 0.884 |
L | 0.82 | 0.50 | 0.756 | |
J | 0.72 | 0.60 | 0.696 | |
I | 0.55 | 0.85 | 0.610 | |
Query Node 2 / segment 4 | M | 0.68 | 0.65 | 0.674 |
O | 0.78 | 0.20 | 0.664 | |
N | 0.50 | 0.20 | 0.440 |
For example, in segment 1, article B moves ahead of A and D after popularity is included: B’s L0 score is 0.800, compared with 0.760 for A and D.
Each Query Node merges the candidates from its segments. All 15 candidates continue to L1 in this walkthrough.
3. L1: Incorporate freshness
L1 runs on each Query Node’s retained candidates. It reads the current $score and overwrites it with 0.8 × L0 score + 0.2 × freshness. The table groups candidates by Query Node and displays each group in descending L1 score for comparison; this L1 chain uses map and does not explicitly call sort.
Query Node | Article ID | L0 score | Freshness | L1 score |
|---|---|---|---|---|
Query Node 1 | B | 0.800 | 0.90 | 0.820 |
D | 0.760 | 0.70 | 0.748 | |
E | 0.710 | 0.85 | 0.738 | |
H | 0.780 | 0.40 | 0.704 | |
G | 0.620 | 0.95 | 0.686 | |
C | 0.740 | 0.30 | 0.652 | |
A | 0.760 | 0.20 | 0.648 | |
F | 0.660 | 0.60 | 0.648 | |
Query Node 2 | K | 0.884 | 0.90 | 0.887 |
J | 0.696 | 0.75 | 0.707 | |
L | 0.756 | 0.40 | 0.685 | |
M | 0.674 | 0.55 | 0.649 | |
I | 0.610 | 0.65 | 0.618 | |
O | 0.664 | 0.15 | 0.561 | |
N | 0.440 | 0.10 | 0.372 |
For example, on Query Node 1, article E moves ahead of H after freshness is included: E’s score rises from 0.710 to 0.738, while H’s changes from 0.780 to 0.704.
Zilliz Cloud merges the Query Node results, and all 15 candidates reach L2 in this walkthrough.
4. L2: Sort and limit global candidates
L2 sorts the 15 candidates by their current score in descending order, using the primary key to break ties, and keeps the top 10. The table shows the complete ranking; only ranks 1–10 are returned.
Candidates ranked before the L2 limit
The following calculated ranking shows the candidates before the L2 limit is applied. The five candidates marked “Not returned” are not included in the search response.
| Rank before limit | Article ID | Title | Score | Result |
|---|---|---|---|---|
| 1 | K | New solar battery | 0.887 | Returned |
| 2 | B | Renewable energy breakthroughs | 0.820 | Returned |
| 3 | D | Battery technology update | 0.748 | Returned |
| 4 | E | Clean energy policy | 0.738 | Returned |
| 5 | J | Hydrogen energy advances | 0.707 | Returned |
| 6 | H | Energy market outlook | 0.704 | Returned |
| 7 | G | Grid storage research | 0.686 | Returned |
| 8 | L | Offshore wind project | 0.685 | Returned |
| 9 | C | Wind energy investment | 0.652 | Returned |
| 10 | M | Energy storage funding | 0.649 | Returned |
| 11 | A | Solar power storage | 0.648 | Not returned |
| 12 | F | Solar farms expand | 0.648 | Not returned |
| 13 | I | Wind turbine innovation | 0.618 | Not returned |
| 14 | O | Regional power demand | 0.561 | Not returned |
| 15 | N | Coal plant retirement | 0.372 | Not returned |
Returned search results
After limit(10), the response contains the top 10 candidates. The following values are calculated from the example data and rounded to three decimal places.
| Rank | Article ID | Title | Score |
|---|---|---|---|
| 1 | K | New solar battery | 0.887 |
| 2 | B | Renewable energy breakthroughs | 0.820 |
| 3 | D | Battery technology update | 0.748 |
| 4 | E | Clean energy policy | 0.738 |
| 5 | J | Hydrogen energy advances | 0.707 |
| 6 | H | Energy market outlook | 0.704 |
| 7 | G | Grid storage research | 0.686 |
| 8 | L | Offshore wind project | 0.685 |
| 9 | C | Wind energy investment | 0.652 |
| 10 | M | Energy storage funding | 0.649 |
Article K ranks first after popularity and freshness are incorporated, while article H had the highest initial ANN score. The L2 limit(10) keeps the top 10 candidates; the remaining five are not returned. Scores are rounded to three decimal places. The chain uses primary keys to break ties when the actual sort scores are equal.
$score is the current score used by the chain, and each map("$score", ...) replaces it. The tables show inferred intermediate values for explanation; print(results) prints the final search response, not these stage-by-stage tables.
Control candidate counts with limits
For a vector search with an L2 Function Chain, use the chain’s limit operation to set the maximum number of results retained by that chain. If fewer candidates reach the operation, fewer results are returned.
The search request’s limit controls the search result window and affects the candidates available for reranking. An L2 limit cannot return candidates that were not retrieved or that were removed before L2.
Use sort before limit when you want to retain the highest-ranked candidates according to a specific score. The limit keeps candidates in their current order; it does not rank them.
A limit in an L1 chain applies independently to each Query Node before the global merge. Setting it too low can discard candidates that would otherwise rank highly in the final results. Choose candidate limits based on the recall and latency requirements of your application, and verify their behavior for your search type.
Next steps
To refine the globally merged candidates with a model, explore the Model Ranker Overview. A model score written to $score replaces the current score; combine the scores explicitly if your ranking logic needs both.
Limits and considerations
-
Candidate quality: Earlier scoring and trimming affect which candidates can reach later stages. Benchmark relevance and end-to-end latency together.
-
Compatibility: In ordinary Python search,
function_chainsand the legacyrankerparameter are mutually exclusive. Hybrid search uses a different placement and fusion workflow; check the reference for your version. -
Search iterator: Function Chains cannot be used with Search Iterator.
-
Ordering: Function Chains cannot be used with
order_by. -
Search aggregation:
L1_RERANKcannot be used withsearch_aggregation. -
System columns:
$scoreis the only writable system column. Other system columns, such as$id, cannot be written.