Important
Native Reranking is available as a Preview feature. The feature and the corresponding documentation might change at any time during the Preview period. Therefore, we don't recommend using this feature in production environments. We do not use any customer data from this feature to train our models at this time. To learn more, see Preview Features.
$rerankThe
$rerankstage reorders input documents using Voyage AI's reranking models and returns the same documents sorted by relevance to the query. The$rerankstage can appear anywhere in an aggregation pipeline. MongoDB recommends that you use$rerankafter a$vectorSearch,$search,$rankFusion, or$scoreFusionstage.
Syntax
A $rerank pipeline stage has the following syntax:
{ "$rerank": { "query": { "text": "<query-text>" }, "path": "<text-field-name>", "numDocsToRerank": <number-of-documents-to-rerank>, "model": "<reranker-model>" } }
Fields
The $rerank stage takes a document with the following fields:
Field | Type | Necessity | Description |
|---|---|---|---|
| Object | Required | Query to use for reranking. |
| String | Required | Query text to use for reranking. For example, you can specify:
|
| String or Array of Strings | Required | Path to the fields to use for reranking. If NOTE: The |
| Int | Required | Maximum number of documents to send to Voyage AI for reranking and return in the results. Documents are selected based on the pipeline's defined document order. The maximum value must be less than or equal to |
| Object | Required | Voyage AI model to use to rerank the documents. Value can be one of the following:
|
Considerations
Review the requirements, limitations, and $rerank behavior before using $rerank.
Requirements
To use $rerank:
Ensure that your cluster is running MongoDB 8.3 or later by selecting Latest version with auto-upgrades in the Atlas UI Cluster Builder page.
Enable Native Reranking through Project Settings. To learn more, see Enable or Disable Native Reranking.
Limitations
You cannot use the $rerank stage:
For self-managed or Atlas Local deployments.
For
$rankFusionor$scoreFusioninput pipelines.For queries on a View.
If you have a MongoDB Search or MongoDB Vector Search index on a View, query the index by running
.aggregate()on the source collection instead of the View.
Behavior
$rerank can appear anywhere in the pipeline. However, MongoDB recommends using $rerank after a stage like $search or $vectorSearch that already returns relevant documents in sorted order.
$rerank reranks and returns the first numDocsToRerank documents that are passed to the stage. If $rerank is the first stage, or prior stages do not return deterministically sorted results, then the documents used for $rerank might change between queries.
$rerank returns an error if any of the fields specified in path are not present in one or more input documents. To mitigate this, use the:
Query
The query.text field determines how the reranker model scores each document. The specified Voyage reranker model computes a relevance score between the query text and the content of each document at the specified path. For most use cases, set $rerank.query.text to the same or similar text as the query from the preceding $search or $vectorSearch stage.
Scoring
Use the score variable with the $meta expression to retrieve the score of each document in the results of the $rerank stage.
{ "$addFields": { "rerankScore": { "$meta": "score" } } }
The $rerank stage replaces the value of $meta: "score" with a new score. To preserve the value from a preceding stage like $rankFusion, you can project the scores into named fields before the $rerank stage.
{ "$addFields": { "originalRankFusionScore": { "$meta": "score" } } }, { "$rerank": { ... } }, { "$addFields": { "rerankScore": { "$meta": "score" } } }
Performance and Accuracy
The $rerank stage can be computationally expensive, depending on the size of the input such as the fields to rerank over, the number of documents to rerank, and the query text. $rerank processes queries and documents jointly at query time. Therefore, $rerank might be slower than relevancy search powered by an index, such as the indexes for $search or $vectorSearch. To balance accuracy and performance, configure numDocsToRerank and path to limit the input size. $rerank does not run Voyage AI reranking models on your Atlas cluster resources.
$rerank is best suited for workloads where retrieval quality is prioritized over ultra-low latency, such as RAG and agentic AI applications. For workloads that require ultra-low latency, MongoDB recommends using an index-powered search stage like $search or $vectorSearch.
Example
The following example demonstrates how to use the $rerank stage to reorder documents in the sample_mflix.embedded_movies collection based on a Voyage AI reranker model. The query uses $rerank after the $match stage to reorder documents using the rerank-2.5 reranker model. In the following aggregation pipeline, the:
$matchstage filters the documents to include only documents that have aplotfield of type string.$sortstage sorts the documents in descending order of thereleasedfield to ensure deterministic ordering.$rerankstage reorders the documents to match the query using thererank-2.5reranker model.$addFieldsstage adds a field namedrerankScoreto the documents.
1 db.embedded_movies.aggregate([ 2 { 3 "$match": { 4 "plot": { "$exists": true, "$type": "string" } 5 } 6 }, 7 { 8 "$sort": { "released": -1 } 9 }, 10 { 11 "$rerank": { 12 "model": "rerank-2.5", 13 "query": { 14 "text": "a group of heroes band together to stop a powerful enemy and save the world" 15 }, 16 "numDocsToRerank": 100, 17 "path": ["title", "plot"] 18 } 19 }, 20 { 21 "$addFields": { 22 "rerankScore": { "$meta": "score" } 23 } 24 }, 25 { "$limit": 10 }, 26 { 27 "$project": { 28 "_id": 0, 29 "title": 1, 30 "plot": 1, 31 "rerankScore": 1 32 } 33 } 34 ])
[ { plot: 'No treason, no surrender.', title: 'Ti mene nosis', rerankScore: 0.5986876487731934 }, { plot: 'The life of the greatest karate master of a generation.', title: 'The Real Miyagi', rerankScore: 0.5986876487731934 }, { plot: 'A shy genius is employed by his former university to design robot software.', title: 'Eva', rerankScore: 0.5986876487731934 }, { plot: 'The owners of a failing security company start robbing houses to boost business.', title: 'Armed Response', rerankScore: 0.5986876487731934 }, { plot: 'A live telecast of the beloved J. M. Barrie story.', title: 'Peter Pan Live!', rerankScore: 0.5986876487731934 }, { plot: "A French police magistrate spends years trying to take down one of the country's most powerful drug rings.", title: 'The Connection', rerankScore: 0.5986876487731934 }, { plot: 'A documentary that follows undercover activists trying to stave off a man-made mass extinction.', title: 'Racing Extinction', rerankScore: 0.5986876487731934 }, { plot: 'An ex-hitman comes out of retirement to track down the gangsters that took everything from him.', title: 'John Wick', rerankScore: 0.5986876487731934 }, { plot: 'A former hit-man for a drug cartel becomes a vigilante to pay for his sins and find redemption.', title: 'Redeemer', rerankScore: 0.5986876487731934 }, { plot: 'Charles Ingvar Jènsson gathers three criminals to take vengeance upon the people who killed his uncle.', title: 'The Master Plan', rerankScore: 0.5986876487731934 } ]