spring-ai icon indicating copy to clipboard operation
spring-ai copied to clipboard

Add a document reranking post-processor for Modular RAG flows

Open KoreaNirsa opened this issue 7 months ago • 3 comments

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 DocumentReranker as a small functional contract under the RAG post-retrieval document package
  • Add RerankingDocumentPostProcessor as a DocumentPostProcessor implementation
  • Add optional topN support 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 RetrievalAugmentationAdvisor configuration 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 avatar Apr 29 '26 07:04 KoreaNirsa

@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 avatar Apr 29 '26 14:04 sobychacko

@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!

KoreaNirsa avatar Apr 30 '26 01:04 KoreaNirsa

Closing this as a duplicate of #6524. See this comment for details: https://github.com/spring-projects/spring-ai/issues/6524#issuecomment-5428656330

sobychacko avatar Aug 26 '26 17:08 sobychacko