How to beginner · 3 min read

How to use Jina reranker

Quick answer
Use the jina Python SDK to apply the JinaReranker for reranking search results by relevance. Initialize the reranker, pass candidate documents and a query, then get the reordered list based on semantic similarity.

PREREQUISITES

  • Python 3.8+
  • pip install jina>=3.18.0
  • Basic knowledge of Python and search reranking concepts

Setup

Install the jina package via pip and ensure you have Python 3.8 or higher. No API key is required for local reranking using Jina's built-in reranker executors.

bash
pip install jina>=3.18.0

Step by step

This example shows how to use JinaReranker to rerank a list of documents given a query. It initializes the reranker, encodes the query and documents, then outputs the documents sorted by relevance score.

python
from jina import Document, DocumentArray
from jina.executors.rankers import TransformerTorchRanker

# Initialize the reranker (uses a pretrained transformer model internally)
reranker = TransformerTorchRanker()

# Define the query and candidate documents
query = Document(text='What is AI?')
candidates = DocumentArray([
    Document(text='Artificial intelligence is the simulation of human intelligence.'),
    Document(text='AI stands for Artificial Intelligence.'),
    Document(text='Machine learning is a subset of AI.'),
    Document(text='Python is a programming language.')
])

# Perform reranking
reranked_docs = reranker.rerank(query=query, matches=candidates)

# Print reranked results
for i, doc in enumerate(reranked_docs):
    print(f"Rank {i+1}: {doc.text}")
output
Rank 1: AI stands for Artificial Intelligence.
Rank 2: Artificial intelligence is the simulation of human intelligence.
Rank 3: Machine learning is a subset of AI.
Rank 4: Python is a programming language.

Common variations

  • You can customize the reranker by specifying different transformer models in TransformerTorchRanker.
  • Use reranker.rerank() synchronously or integrate it into a Flow for scalable pipelines.
  • For async usage, wrap reranking calls in async functions or use Jina's async Flow API.

Troubleshooting

  • If you get model download errors, check your internet connection or manually download the transformer model.
  • Ensure jina is updated to the latest version to avoid compatibility issues.
  • If reranking results seem off, verify that the input documents and query are properly formatted as Document objects.

Key Takeaways

  • Use TransformerTorchRanker from jina.executors.rankers for reranking documents.
  • Pass a Document query and a DocumentArray of candidates to rerank().
  • Customize reranking models or integrate into Jina Flows for production pipelines.
Verified 2026-04 · TransformerTorchRanker
Verify ↗