Spring / Spring AI interview questions
What is the Document class in Spring AI and how is it used in RAG?
The Document class is Spring AI's core data carrier for textual content flowing through a RAG pipeline. It wraps a piece of text together with a metadata map and an optional embedding vector, giving every chunk a consistent identity regardless of where it originated.
Key fields:
- id — auto-generated UUID uniquely identifying the chunk.
- content — the plain text of the chunk that gets embedded and later injected into prompts.
- metadata — a
Map<String, Object>carrying provenance data (source filename, page number, URL, creation date). This metadata is preserved through the VectorStore and returned alongside similarity search results, so you can cite sources in answers. - embedding — the float vector populated by the EmbeddingModel; null until the document is embedded.
// Creating a Document manually Document doc = new Document( "Spring AI simplifies AI integration in Java applications.", Map.of("source", "spring-ai-docs.pdf", "page", 1) ); // After similarity search you can access metadata List<Document> results = vectorStore.similaritySearch( SearchRequest.query(question).withTopK(3)); results.forEach(d -> { System.out.println(d.getContent()); System.out.println("Source: " + d.getMetadata().get("source")); });
When you add documents to a VectorStore via vectorStore.add(docs), the store internally calls the EmbeddingModel to populate the embedding field before persisting. The caller does not need to embed documents separately in the typical flow.
More Related questions...