Scrivio Interview library
CodingFoundations6 min read · updated 12 November 2026

Build a Spring AI ingestion pipeline that reads PDF manuals, splits them into chunks, adds the source file name and page as metadata, and writes them to a vector store. Name the classes you would use.

Spring AI's ingestion follows read, transform, write. We read the PDFs with PagePdfDocumentReader (or ParagraphPdfDocumentReader, or TikaDocumentReader for mixed formats), which returns a List<Document> with metadata such as the page number; we split them with TokenTextSplitter, optionally enrich them with KeywordMetadataEnricher or SummaryMetadataEnricher, make sure source and page are in the metadata, and write them with vectorStore.add(documents), which embeds each chunk with the EmbeddingModel. The whole thing runs as a batch job, separate from the query path, with counts and failures logged per file.

Why this question is asked

Retrieval-augmented generation, or RAG, answers questions using our own documents, and its quality is decided long before the user asks anything: in how we read, split and label those documents. Product manuals are a classic case, because users want the answer and also the page it came from. Interviewers ask this question to see whether we know Spring AI's ETL classes and why each step exists. In this answer, we will recap the ingestion model, then read, split, label and write the manuals, and finally run it as a proper batch job.

1A Quick Recap of Spring AI's ETL Model

Spring AI models ingestion as extract, transform, load. A DocumentReader extracts content and returns a list of Document objects. A Document holds text, a metadata map (for example file name and page number) and an id. A DocumentTransformer takes a list of documents and returns a new list, for example split into smaller chunks or with extra metadata. A DocumentWriter stores them. VectorStore implements DocumentWriter, so writing to a vector store is the last step of the same pipeline.

An embedding is a list of numbers, a vector, that represents the meaning of a text. The vector store saves each chunk's text, metadata and embedding, so that later a question can be embedded the same way and the closest chunks found.

Step throughIngesting one manual
  1. Read the PDFPagePdfDocumentReader: one Document per page
  2. Split into chunksTokenTextSplitter
  3. Enrich metadatasource, page, optional keywords
  4. Embed and writevectorStore.add(chunks)
  5. Log counts and failuresper file

5 steps in order. Play them, or step through them one at a time.

2Reading the PDFs

For PDFs, Spring AI offers two readers in the PDF reader module. PagePdfDocumentReader produces one Document per page (or per group of pages), which is ideal when we want to cite pages. ParagraphPdfDocumentReader uses the PDF's outline, its table of contents, to split by section, but it only works for PDFs that have an outline. For a mix of formats such as Word, HTML and PDF, TikaDocumentReader uses Apache Tika to extract text from almost anything, with less control over pages.

Java
Resource pdf = new FileSystemResource("manuals/pump-x200.pdf");
var reader = new PagePdfDocumentReader(pdf,
PdfDocumentReaderConfig.builder()
.withPageTopMargin(0)
.withPagesPerDocument(1) // one Document per page
.build());
List<Document> pages = reader.get();
System.out.println(pages.size()); // 84
System.out.println(pages.get(11).getMetadata()); // {page_number=12, file_name=pump-x200.pdf} (keys: check your version)

Notice that the reader already puts page and file information in the metadata. That is what will let an answer say see page 12 of the X200 manual.

3Splitting Into Chunks

A page of a manual can hold 600 to 1,000 tokens and several topics. Smaller chunks give more precise retrieval and smaller prompts, so we split. TokenTextSplitter splits text into chunks of a target number of tokens, trying to break at sentence ends, and copies the metadata of the original document to every chunk. Its defaults are around 800 tokens per chunk; we usually go smaller for manuals.

Java
TokenTextSplitter splitter = TokenTextSplitter.builder()
.withChunkSize(400) // target tokens per chunk
.withMinChunkSizeChars(200) // avoid tiny fragments
.build(); // builder and option names: check your version
List<Document> chunks = splitter.apply(pages);
System.out.println(chunks.size()); // 197
System.out.println(chunks.get(30).getMetadata().get("page_number")); // 15

Overlap, where each chunk repeats the last few sentences of the previous one, helps when an answer spans a chunk boundary. Check whether your version's TokenTextSplitter supports an overlap setting; if it does not, we can write a small DocumentTransformer that adds overlap, or keep chunks at page boundaries, which often matters more for manuals anyway.

4Adding and Enriching Metadata

Metadata does two jobs: it lets us show sources in answers and it lets us filter searches. Besides file name and page, we add our own fields such as product, document type and language. We can do this by mapping the documents before writing. Spring AI also has enrichers that call a chat model: KeywordMetadataEnricher adds a list of keywords per chunk and SummaryMetadataEnricher adds a short summary, which can improve retrieval but costs one model call per chunk.

Java
List<Document> labelled = chunks.stream()
.map(d -> {
Map<String, Object> meta = new HashMap<>(d.getMetadata());
meta.put("source", "pump-x200.pdf");
meta.put("product", "X200");
meta.put("doc_type", "manual");
return new Document(d.getText(), meta);
})
.toList();
List<Document> enriched = new KeywordMetadataEnricher(chatModel, 5).apply(labelled); // optional, 197 model calls

We keep the enrichers optional because of their cost: 197 chunks means 197 extra model calls per manual.

5Writing to the Vector Store

Writing is one call. vectorStore.add(documents) sends the chunk texts to the configured EmbeddingModel, usually in batches, and stores the text, metadata and vector together.

Java
vectorStore.add(labelled); // embeds 197 chunks and inserts 197 rows
// later, at query time:
vectorStore.similaritySearch(SearchRequest.builder()
.query("How do I reset the X200 pressure sensor?")
.topK(4)
.filterExpression("product == 'X200'")
.build());
// -> chunks from pages 15, 16 and 41, each with source and page_number in metadata

Embedding has a cost and a rate limit, so for large libraries we add documents in batches of a few hundred and handle failures per batch.

6Running Ingestion as a Batch Job

Ingestion should not run inside the request that answers a user. Reading and embedding hundreds of pages takes minutes and costs money, and if it shares a thread pool or a database connection pool with the chat endpoint, users feel it. So we run it as a separate job: a scheduled task, a Spring Batch job, or a command-line run of the same application with a different profile. For each file, the job logs how many pages were read, how many chunks were written and how long it took, and a failed file is recorded and retried without stopping the rest.

Java
for (Resource file : manuals) {
try {
int written = ingest(file);
log.info("ingested file={} chunks={}", file.getFilename(), written); // file=pump-x200.pdf chunks=197
} catch (Exception e) {
failures.add(file.getFilename());
log.error("ingestion failed file={}", file.getFilename(), e);
}
}
log.info("done files={} failed={}", manuals.size(), failures.size()); // done files=42 failed=1

7Follow-up questions

How would you choose chunk size and overlap?

Start from the content and the questions. Manuals with short procedures work well with chunks of 300 to 500 tokens, one topic each, and a small overlap of 10 to 15 percent if an answer often spans boundaries. Then measure: build a set of real questions with the page that answers each, try two or three chunk sizes, and compare how often the right chunk is in the top results and how many prompt tokens each answer uses. Smaller chunks are more precise but lose context; larger chunks carry context but cost more and dilute the match.

What about scanned PDFs?

Readers extract the text layer, so a scanned PDF without one yields empty pages. Those need OCR first, and the job should flag files with near-empty text so they do not silently disappear from search.

Why keep the page number?

Users trust answers they can check, and support staff need to point to the source. The page also helps debug wrong answers, because we can see exactly which chunk the model was given.

8Conclusion

In this answer, we built an ingestion pipeline with Spring AI's read, transform, write model. PagePdfDocumentReader or ParagraphPdfDocumentReader reads PDFs into documents with page metadata, TikaDocumentReader handles mixed formats, TokenTextSplitter splits them into chunks, our mapping and optional enrichers add source, page and product, and vectorStore.add embeds and stores every chunk. Running it as a separate batch job with counts and failures per file keeps the query path fast and the ingestion observable.

What to say in the interviewAbout 70 seconds aloud

Open with

Spring AI uses a read, transform, write model. To read, I use PagePdfDocumentReader, which gives one Document per page with the file name and page number in metadata, or ParagraphPdfDocumentReader if the PDFs have an outline and I want sections; for mixed formats I would use TikaDocumentReader.

Then, if they want more

To transform, I split the pages with TokenTextSplitter into chunks of a few hundred tokens; the metadata is copied to every chunk, so each chunk still knows its file and page.

I add my own fields such as source, product and document type by mapping the documents, and optionally use KeywordMetadataEnricher or SummaryMetadataEnricher, keeping in mind they cost a model call per chunk. To write, I call vectorStore.add, since VectorStore is a DocumentWriter; it calls the EmbeddingModel for each chunk and stores text, metadata and vector.

I run this as a batch job separate from the chat endpoint, because it is slow and costly, and I log pages, chunks and failures per file so one broken PDF does not stop the run. I would tune chunk size and overlap with a set of real questions.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Official documentation
Spring AI reference
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Build a Spring AI ingestion pipeline that reads PDF manuals, splits them into chunks...": https://scrivio-interview-library.netlify.app/spring-boot/spring-ai/1c624b5db1/