eBook – Guide Spring Cloud – NPI EA (cat=Spring Cloud)
announcement - icon

Let's get started with a Microservice Architecture with Spring Cloud:

>> Join Pro and download the eBook

eBook – Mockito – NPI EA (tag = Mockito)
announcement - icon

Mocking is an essential part of unit testing, and the Mockito library makes it easy to write clean and intuitive unit tests for your Java code.

Get started with mocking and improve your application tests using our Mockito guide:

Download the eBook

eBook – Java Concurrency – NPI EA (cat=Java Concurrency)
announcement - icon

Handling concurrency in an application can be a tricky process with many potential pitfalls. A solid grasp of the fundamentals will go a long way to help minimize these issues.

Get started with understanding multi-threaded applications with our Java Concurrency guide:

>> Download the eBook

eBook – Reactive – NPI EA (cat=Reactive)
announcement - icon

Spring 5 added support for reactive programming with the Spring WebFlux module, which has been improved upon ever since. Get started with the Reactor project basics and reactive programming in Spring Boot:

>> Join Pro and download the eBook

eBook – Java Streams – NPI EA (cat=Java Streams)
announcement - icon

Since its introduction in Java 8, the Stream API has become a staple of Java development. The basic operations like iterating, filtering, mapping sequences of elements are deceptively simple to use.

But these can also be overused and fall into some common pitfalls.

To get a better understanding on how Streams work and how to combine them with other language features, check out our guide to Java Streams:

>> Join Pro and download the eBook

eBook – Jackson – NPI EA (cat=Jackson)
announcement - icon

Do JSON right with Jackson

Download the E-book

eBook – HTTP Client – NPI EA (cat=Http Client-Side)
announcement - icon

Get the most out of the Apache HTTP Client

Download the E-book

eBook – Maven – NPI EA (cat = Maven)
announcement - icon

Get Started with Apache Maven:

Download the E-book

eBook – Persistence – NPI EA (cat=Persistence)
announcement - icon

Working on getting your persistence layer right with Spring?

Explore the eBook

eBook – RwS – NPI EA (cat=Spring MVC)
announcement - icon

Building a REST API with Spring?

Download the E-book

Course – LS – NPI EA (cat=Jackson)
announcement - icon

Get started with Spring and Spring Boot, through the Learn Spring course:

>> LEARN SPRING
Course – RWSB – NPI EA (cat=REST)
announcement - icon

Explore Spring Boot 3 and Spring 6 in-depth through building a full REST API with the framework:

>> The New “REST With Spring Boot”

Course – LSS – NPI EA (cat=Spring Security)
announcement - icon

Yes, Spring Security can be complex, from the more advanced functionality within the Core to the deep OAuth support in the framework.

I built the security material as two full courses - Core and OAuth, to get practical with these more complex scenarios. We explore when and how to use each feature and code through it on the backing project.

You can explore the course here:

>> Learn Spring Security

Course – All Access – NPI EA (cat= Spring)
announcement - icon

All Access is finally out, with all of my Spring courses. Learn JUnit is out as well, and Learn Maven is coming fast. And, of course, quite a bit more affordable. Finally.

>> GET THE COURSE
Course – LSD – NPI EA (tag=Spring Data JPA)
announcement - icon

Spring Data JPA is a great way to handle the complexity of JPA with the powerful simplicity of Spring Boot.

Get started with Spring Data JPA through the guided reference course:

>> CHECK OUT THE COURSE

Partner – Moderne – NPI EA (cat=Spring Boot)
announcement - icon

Refactor Java code safely — and automatically — with OpenRewrite.

Refactoring big codebases by hand is slow, risky, and easy to put off. That’s where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.

Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions — one for newcomers and one for experienced users. You’ll see how recipes work, how to apply them across projects, and how to modernize code with confidence.

Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.

Course – LJB – NPI EA (cat = Core Java)
announcement - icon

Code your way through and build up a solid, practical foundation of Java:

>> Learn Java Basics

1. Overview

Retrieval-Augmented Generation (RAG) queries relevant documents from a knowledge base, such as a vector database, and then feeds them to the LLM as part of the prompt so it can generate a more accurate response.

Spring AI provides a modular RAG architecture that divides RAG into four stages:

  • Pre-retrieval,
  • Retrieval,
  • Post-retrieval, and
  • Generation

In this tutorial, we’ll focus on query transformation in the pre-retrieval stage that processes the user query before retrieval.

2. Why Transform Queries?

Retrieving from a vector store is basically a similarity search over the datastore embeddings, with the query embedding as the search key.

Therefore, the relevance of the retrieved information depends on the query wording. For example, a query may be an inefficient search key due to:

  • Lack of clarity – a query may be ambiguous or too verbose and mention many concepts, many irrelevant to the search. In both cases, the query’s embedding is less aligned with relevant documents.
  • Language mismatch – a query in a different language from the stored documents may not produce a match even if the content is there. This gets worse if the embedding model wasn’t trained on the documents in the query’s language.
  • Missing context – A follow-up question within a long conversation, such as “And how does it support RAG?”, relies on previous messages. The user knows what “it” refers to, but the RAG system searches with the latest message only. Without the chat history, the query is ambiguous.

Query transformers use an LLM to rewrite queries before retrieval in a way we expect to improve the relevance of the retrieved data.

3. Setup

Let’s set up our project. First, we need the Spring AI RAG dependency to work with modular RAG:

<dependency>
   <groupId>org.springframework.ai</groupId>
   <artifactId>spring-ai-rag</artifactId>
</dependency>

As every query transformer needs a chat model that connects to an LLM, we’ll use the Spring AI OpenAI dependency:

<dependency>
   <groupId>org.springframework.ai</groupId>
   <artifactId>spring-ai-starter-model-openai</artifactId>
</dependency>

Now, let’s configure our OpenAI API key and the chat model in our application.yml:

spring:
  ai:
    openai:
      api-key: ${OPENAI_API_KEY}
      chat:
        options:
          model: gpt-4o-mini
          temperature: 0.0

A high temperature makes the model more creative, so the same user query can be transformed differently in each call. For that reason, the Spring AI documentation recommends using a low temperature such as 0.0 to get more deterministic results.

4. Modular RAG

In Spring AI, RetrievalAugmentationAdvisor ties the RAG stages together. Once we register it on a ChatClient, every prompt runs through the full pipeline, e.g.:

RetrievalAugmentationAdvisor ragAdvisor = RetrievalAugmentationAdvisor.builder()
  .queryTransformers(
    RewriteQueryTransformer.builder()
      .chatClientBuilder(chatClientBuilder)
      .build(),
    TranslationQueryTransformer.builder()
      .chatClientBuilder(chatClientBuilder)
      .targetLanguage("english")
      .build(),
    CompressionQueryTransformer.builder()
      .chatClientBuilder(chatClientBuilder)
      .build())
  .documentRetriever(VectorStoreDocumentRetriever.builder()
    .vectorStore(vectorStore)
    .topK(3)
    .build())
  .build();

ChatClient chatClient = chatClientBuilder.defaultAdvisors(ragAdvisor)
  .build();

Here, vectorStore is a VectorStore instance holding our embedded documents, e.g. backed by an Oracle vector database.

The builder of the RetrievalAugmentationAdvisor exposes at least one method for each RAG stage. In our example, we configure queryTransformers() for the pre-retrieval stage and the documentRetriever() method for the retrieval stage. Let’s focus on the former.

The queryTransformers() method accepts multiple query transformers. They implement the QueryTransformer interface and run in sequence, each one receiving the previous one’s output. Each transformer makes its own LLM call that requires a ChatClient.Builder.

Spring AI provides three query transformers. To demonstrate each transformer separately, we’ll implement a test method for each in our test class:

@SpringBootTest
class PreRetrievalQueryTransformerLiveTest {
    private static final Logger logger = LoggerFactory.getLogger(PreRetrievalQueryTransformerLiveTest.class);

    @Autowired
    private ChatClient.Builder chatClientBuilder;
}

Let’s check them out.

5. RewriteQueryTransformer

RewriteQueryTransformer filters out irrelevant information from a query. By keeping only the relevant parts, it produces a concise text that we expect to find better results. This transformer is useful when the user query is lengthy and unclear.

As an example, let’s show how it handles a verbose query:

@Test
void whenQueryIsVerbose_thenRewrite() {
    QueryTransformer queryTransformer = RewriteQueryTransformer.builder()
      .chatClientBuilder(chatClientBuilder)
      .build();

    String userQuery = "I'm studying machine learning and honestly the whole thing is confusing. What is an LLM anyway?";
    Query transformed = queryTransformer.transform(new Query(userQuery));

    logger.info("Rewrite: [{}] -> [{}]", userQuery, transformed.text());
}

We can see the following or similar log when executing the test:

Rewrite: [I'm studying machine learning and honestly the whole thing is confusing. What is an LLM anyway?] -> [What is an LLM in machine learning?]

The transformer identifies machine learning and LLM as important context to keep and discards the user’s introduction that is irrelevant for the search.

6. TranslationQueryTransformer

Embedding models work best for the languages they were trained on. For example, if most of the training data is in English, the model represents English texts more precisely than other written languages. Consequently, a query written in another language such as Chinese or French may align poorly with documents stored in English, even when they have the same meaning.

TranslationQueryTransformer solves this by translating the user’s query into the target language: the language of the documents in our datastore.

To see this, let’s add a new test method to send a Traditional Chinese query 什麼是向量資料庫? asking “What is a vector database?” to the transformer:

@Test
void whenQueryIsInTraditionalChinese_thenTranslate() {
    QueryTransformer queryTransformer = TranslationQueryTransformer.builder()
      .chatClientBuilder(chatClientBuilder)
      .targetLanguage("english")
      .build();

    String userQuery = "什麼是向量資料庫?";
    Query transformed = queryTransformer.transform(new Query(userQuery));

    logger.info("Translation: [{}] -> [{}]", userQuery, transformed.text());
}

We specify the target language explicitly using the targetLanguage() method. The log shows the Chinese query has been translated correctly:

Translation: [什麼是向量資料庫?] -> [What is a vector database?]

If the query is already in the target language or in a language the LLM can’t recognize, it’s returned unchanged. For example, if we use a query in English, the output is identical:

Translation: [What is a vector database?] -> [What is a vector database?]

Similarly, a query with made-up words of an unknown language passes through unchanged as well:

Translation: [Gblorvak tensig quimbo darnel?] -> [Gblorvak tensig quimbo darnel?]

7. CompressionQueryTransformer

A user query usually refers to previous messages and doesn’t contain complete information on its own. For that reason, a single query in a chat may be ambiguous and not return anything useful from the vector store.

CompressionQueryTransformer solves this by compressing the conversation history and using it to rewrite the query to resolve possible ambiguities.

Unlike the previous two transformers, it requires the history of the Query, so must supply both the query text and the history:

@Test
void whenQueryIsFollowUpWithHistory_thenCompress() {
    QueryTransformer queryTransformer = CompressionQueryTransformer.builder()
      .chatClientBuilder(chatClientBuilder)
      .build();

    List<Message> history = List.of(
      new UserMessage("What is Spring AI?"),
      new AssistantMessage("Spring AI is an application framework that brings Spring's design principles to AI engineering."));
    
    String followUpQuery = "And how does it support RAG?";
    
    Query query = Query.builder()
      .text(followUpQuery)
      .history(history)
      .build();

    Query transformed = queryTransformer.transform(query);

    logger.info("Compression: [{}] -> [{}]", followUpQuery, transformed.text());
}

Here, we construct the conversation history explicitly as a list of messages. Executing the test should produce a log like this one:

Compression: [And how does it support RAG?] -> [How does Spring AI support Retrieval-Augmented Generation (RAG)?]

The original query was unclear on its own because of “it.” The transformer compresses it and the conversation history into a new query where the meaning of “it” is clear.

In a real application, a ChatClient doesn’t keep previous messages by default, so we need to register a chat memory advisor with the RAG advisor. Otherwise, the transformer receives no history to work with.

However, the chat history may not always be relevant as the user may switch the topic during conversation. Let’s run the same test again with an unrelated query:

String followUpQuery = "What's a good recipe for carbonara?";

The transformer should recognize the topic switch and not include anything from previous messages:

Compression: [What's a good recipe for carbonara?] -> [Can you provide a good recipe for carbonara?]

8. Query Transformer Considerations

All query transformers have these common characteristics:

  • They run sequentially, and each receives the previous one’s output.
  • Each transformer adds an LLM call.
  • Temperature needs to be low (e.g., 0.0) to ensure more deterministic results.

Because of additional LLM calls, the overall latency adds up. So, we should enable only the transformers we actually need:

  • RewriteQueryTransformer simplifies verbose and unclear queries to provide clarity.
  • TranslationQueryTransformer translates queries into the language of the database.
  • CompressionQueryTransformer compresses and merges a query with its history into a new query containing relevant context from the chat history.

9. Conclusion

In this article, we explained query transformation in the pre-retrieval stage of Spring AI’s modular RAG architecture.

Three QueryTransformer implementations that Spring AI provide transform queries to improve retrieval performance.

As always, the full source code is available over on GitHub.

Baeldung Pro – NPI EA (cat = Baeldung)
announcement - icon

Baeldung Pro comes with both absolutely No-Ads as well as finally with Dark Mode, for a clean learning experience:

>> Explore a clean Baeldung

eBook – HTTP Client – NPI EA (cat=HTTP Client-Side)
announcement - icon

The Apache HTTP Client is a very robust library, suitable for both simple and advanced use cases when testing HTTP endpoints. Check out our guide covering basic request and response handling, as well as security, cookies, timeouts, and more:

>> Download the eBook

eBook – Java Concurrency – NPI EA (cat=Java Concurrency)
announcement - icon

Handling concurrency in an application can be a tricky process with many potential pitfalls. A solid grasp of the fundamentals will go a long way to help minimize these issues.

Get started with understanding multi-threaded applications with our Java Concurrency guide:

>> Download the eBook

eBook – Java Streams – NPI EA (cat=Java Streams)
announcement - icon

Since its introduction in Java 8, the Stream API has become a staple of Java development. The basic operations like iterating, filtering, mapping sequences of elements are deceptively simple to use.

But these can also be overused and fall into some common pitfalls.

To get a better understanding on how Streams work and how to combine them with other language features, check out our guide to Java Streams:

>> Join Pro and download the eBook

eBook – Persistence – NPI EA (cat=Persistence)
announcement - icon

Working on getting your persistence layer right with Spring?

Explore the eBook

Course – LS – NPI EA (cat=REST)

announcement - icon

Get started with Spring Boot and with core Spring, through the Learn Spring course:

>> CHECK OUT THE COURSE

Partner – Moderne – NPI EA (tag=Refactoring)
announcement - icon

Modern Java teams move fast — but codebases don’t always keep up. Frameworks change, dependencies drift, and tech debt builds until it starts to drag on delivery. OpenRewrite was built to fix that: an open-source refactoring engine that automates repetitive code changes while keeping developer intent intact.

The monthly training series, led by the creators and maintainers of OpenRewrite at Moderne, walks through real-world migrations and modernization patterns. Whether you’re new to recipes or ready to write your own, you’ll learn practical ways to refactor safely and at scale.

If you’ve ever wished refactoring felt as natural — and as fast — as writing code, this is a good place to start.

eBook Jackson – NPI EA – 3 (cat = Jackson)
guest
0 Comments
Oldest
Newest