Add a document reranking post-processor for Modular RAG flows
Overview
Spring AI's Modular RAG architecture already exposes a DocumentPostProcessor extension point for post-retrieval document processing.
One common RAG pattern is to retrieve a broader candidate set from a vector store, rerank those candidates with a more precise relevance signal, and then pass only the highest-ranked documents to generation.
Currently, users need to implement this pattern manually for each application. A reusable reranking post-processor would make this flow easier to configure and align it with the existing RAG module abstractions.
Proposal
Add a reusable document reranking post-processor for Modular RAG flows.
The reranking component would not prescribe a ranking strategy. Instead, users can provide their own DocumentReranker implementation.
For example, users could
- Call an external reranking provider such as Cohere, Jina, Voyage, or another cross-encoder service
- Sort by a custom score derived from document metadata, freshness, authority, or business priority
- Apply simple rule-based ranking for application-specific constraints
- Combine vector similarity scores, keyword matches, metadata scores, and external model scores
- Reduce the final context size by keeping only the top N reranked documents
Scope
- Add
DocumentRerankeras a small functional contract under the RAG post-retrieval document package - Add
RerankingDocumentPostProcessoras aDocumentPostProcessorimplementation - Add optional
topNsupport to keep only the first N reranked documents - Add unit tests covering reranking behavior,
topN, validation, and empty input handling - Update the RAG documentation with a basic
RetrievalAugmentationAdvisorconfiguration example
Example Usage
@Bean
Advisor retrievalAugmentationAdvisor(VectorStore vectorStore, DocumentReranker documentReranker) {
return RetrievalAugmentationAdvisor.builder()
.documentRetriever(VectorStoreDocumentRetriever.builder()
.vectorStore(vectorStore)
.topK(20)
.build())
.documentPostProcessors(RerankingDocumentPostProcessor.builder()
.documentReranker(documentReranker)
.topN(5)
.build())
.build();
}
Expected Outcome
Users can retrieve a broad candidate set, rerank it using their preferred strategy or provider, and keep the top N documents before query augmentation.
This improves context precision while preserving the recall benefits of retrieving more candidates from the vector store.
@KoreaNirsa Re-ranking and hybrid search are a post 2.0.0 feature item. I am afraid this has to wait until then when we can prioritize it. Thank you!
@sobychacko Thanks for the clarification.
That makes sense.
I’d be happy to revisit this and help with the implementation when the timing is right.
Thanks!
Closing this as a duplicate of #6524. See this comment for details: https://github.com/spring-projects/spring-ai/issues/6524#issuecomment-5428656330