vai logo
vai
Use CasesShared SpaceDocs
Get Started
Retrieval
Reranking
MongoDB Atlas
RAG

Two-Stage Retrieval With Reranking

Walk through the classic retrieval stack: embed the query, run Atlas vector search, rerank the candidates, then compare the result to a vector-only pass.

MongoDB Atlas
API key
View Source TapeOpen Docs
Prerequisites

A valid VOYAGE_API_KEY is set in the environment.

MongoDB Atlas is configured through MONGODB_URI or vai config set mongodb-uri.

The `vai_demo.knowledge` collection already exists, for example by running the pipeline demo first.

Under the hood

See the exact VAI command, the matching Voyage AI layer, and the MongoDB query shape behind the demo.

vai query 'how does vector search work?' --db vai_demo --collection knowledge --model voyage-4-lite

The high-level command packages the canonical RAG retrieval pattern into one CLI step. The second command in the tape disables reranking so the precision gain is visible, not theoretical.

Share or copy this demo

Keep it lightweight. The prepared text stays behind the buttons.

Open canonical URL

Share

Copy

LinkedIn opens the share dialog and copies the prepared text so you can paste it in quickly.

Exact commands

The full walkthrough is included here so anyone can replay the demo exactly as published.

$vai explain two-stage
$echo '=> stage 1: POST /v1/embeddings model:voyage-4-lite input_type:query'
$echo '=> stage 1: $vectorSearch numCandidates:100 limit:20'
$echo '=> stage 2: POST /v1/rerank model:rerank-2.5 top_k:5'
$vai query 'how does vector search work?' --db vai_demo --collection knowledge --model voyage-4-lite
$echo '=> now skip reranking to see stage 1 results alone'
$vai query 'how does vector search work?' --db vai_demo --collection knowledge --model voyage-4-lite --no-rerank
$echo '=> reranking reorders candidates -- same docs, better precision'

Related demos

More shareable workflows from the same VAI demo library.

Pipeline
MongoDB Atlas
Featured
End-to-End Atlas Pipeline

Run the full workflow in one command: create sample docs, chunk them, embed them, store them in Atlas, and auto-create the vector index.

Atlas
API key

VAI command

vai pipeline /tmp/vai-demo-docs/ --db vai_demo --collection knowledge --create-index

Show Under the Hood

Prerequisites

A valid VOYAGE_API_KEY is set in the environment.

View DemoSource
Reranking
Retrieval
Standalone Reranking

Rerank intentionally messy candidate documents against a query, then compare the full reranker to the lite version to show the latency-precision tradeoff.

API key

VAI command

vai rerank 'how do I connect to MongoDB Atlas?' --documents 'Use the connection string from your Atlas dashboard' 'Python is a popular language' 'Atlas supports vectorSearch aggregation' 'Copy your URI and pass it to MongoClient' 'The weather in San Francisco is mild'

Show Under the Hood

Prerequisites

A valid VOYAGE_API_KEY is set in the environment.

RAG
Local Inference
Featured
Local RAG Chat With Ollama And Nano

Build a tiny Atlas-backed RAG chat flow using local nano embeddings and Ollama for generation.

Requires Ollama
Atlas

VAI command

vai chat --db "$DEMO_DB" --collection "$DEMO_COLLECTION" --local --llm-provider ollama --llm-model "$OLLAMA_MODEL" --llm-base-url http://localhost:11434 --no-history --no-stream

Show Under the Hood

Prerequisites

Ollama is installed and running locally.