How to Generate and Store Document Embeddings in Spring Boot with Spring AI
Fast recall
Quick revision
Core answer
EmbeddingModel bean (auto-configured from spring.ai.openai.api-key or similar). (2) Create Document objects from your text chunks — new Document(text, metadata). (3) Call vectorStore.add(List.of(doc1, doc2, ...)) — Spring AI calls embeddingModel.embed() internally and stores each vector. (4) For search, call vectorStore.similaritySearch(SearchRequest.query(userQuery).withTopK(5)). With PgVectorStore, the backing table uses a vector(1536) column and an HNSW index created via the pgvector extension. Spring AI's auto-configuration creates the schema automatically with spring.ai.vectorstore.pgvector.initialize-schema=true.Whether the candidate knows Spring AI's concrete API — EmbeddingModel, VectorStore, Document, SearchRequest — and can describe the full ingest-then-search lifecycle including the PGVector schema setup
Manually calling embeddingModel.embed() and then separately inserting into a database. Spring AI's VectorStore.add() handles both steps atomically.
Mention chunking strategies (fixed-size with overlap, sentence-boundary), explain the metadata map on Document for filtered retrieval, and describe how initialize-schema auto-creates the pgvector table with the correct vector dimension.
Interview-ready guide
Interview answer
In Spring AI 1.0.0 the flow is: configure, create Documents, call add, then similaritySearch.
Auto-configuration wires an EmbeddingModel from your properties — OpenAI, Ollama, Azure, etc. You create Document objects with your text chunks and a metadata map. Then vectorStore.add(documents) does the embedding and the persistence in one call.
With PgVectorStore, Spring AI creates a table with a vector column and an HNSW index when initialize-schema is true. The pgvector extension needs to be installed in your Postgres instance first.
At query time: embed the user question automatically inside similaritySearch, get the top-K Documents back, inject them into the LLM prompt. The non-obvious sharp edge is chunking: a 2000-token chunk that crosses a topic boundary degrades retrieval quality more than most teams expect.
How to Generate and Store Document Embeddings in Spring Boot with Spring AI
Direct Answer
In Spring AI 1.0.0: (1) Declare an EmbeddingModel bean (auto-configured from spring.ai.openai.api-key or similar). (2) Create Document objects from your text chunks — new Document(text, metadata). (3) Call vectorStore.add(List.of(doc1, doc2, ...)) — Spring AI calls embeddingModel.embed() internally and stores each vector. (4) For search, call vectorStore.similaritySearch(SearchRequest.query(userQuery).withTopK(5)). With PgVectorStore, the backing table uses a vector(1536) column and an HNSW index created via the pgvector extension. Spring AI's auto-configuration creates the schema automatically with spring.ai.vectorstore.pgvector.initialize-schema=true.
Deep Dive & Architecture
PGVector is a Postgres extension — no new infrastructure, vectors live in the same DB as your relational data, supports HNSW and ivfflat.
Best when your team already runs Postgres. Pinecone is a fully managed serverless vector service — zero ops, auto-scales, but vendor-locked and priced per query/storage.
Best for teams who want no vector infra at all. Weaviate is an open-source standalone vector database with built-in vectorization (it can call an embedding model itself during ingest) and a GraphQL/REST API. Best when you need rich filtering, multi-tenancy, or hybrid (keyword + vector) search without managing the embedding step. Spring AI 1.0.0 has first-class support for all three via PgVectorStore, PineconeVectorStore, and WeaviateVectorStore.
Describing a vector database as just a key-value store that happens to hold arrays. The key distinction is the approximate nearest-neighbour index — without it you'd do a full table scan on every query.
Common Mistakes & Pitfalls
Manually calling embeddingModel.embed() and then separately inserting into a database. Spring AI's VectorStore.add() handles both steps atomically.
Check your understanding
Answer these without looking back at the article.
- What is a vector database and how does it enable semantic search?
- Compare PGVector, Pinecone, and Weaviate for Java backend use — when do you pick each?
- What is cosine similarity vs Euclidean distance in vector search, and when does it matter?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.