Java + Embeddings
Embeddings are one of the fundamental building blocks of modern AI applications.
An embedding converts information such as text into a numerical vector that represents characteristics and relationships in the input. Applications can then compare vectors to identify semantically related content.
For example:
"Java inheritance"
↓
Embedding Model
↓
[0.12, -0.31, 0.74, 0.18, ...]
Another sentence:
"One Java class can extend another"
↓
Embedding Model
↓
[0.15, -0.29, 0.71, 0.22, ...]
The actual numbers are generated by the embedding model. Applications use them for tasks such as semantic search, clustering, classification, and RAG.
Spring AI currently defines an EmbeddingModel abstraction with methods for embedding text, documents, and batches of text, while supporting implementations include OpenAI, Ollama, Google GenAI, Transformers, and others.
1. What Is an Embedding?
An embedding is a numerical representation of information.
For text:
Text
↓
Embedding Model
↓
Vector
For example:
"How does Java inheritance work?"
might become a vector:
[
0.124,
-0.382,
0.761,
0.052,
...
]
The vector can have hundreds or thousands of dimensions.
The individual numbers are generally not interpreted manually.
Their relationship is what matters.
2. Why Embeddings Are Important
Normal keyword search looks for matching words.
For example:
Query:
Java inheritance
might prioritize documents containing exactly:
Java
inheritance
Embeddings enable semantic search.
A query such as:
How can one Java class get behavior
from another class?
can retrieve content about:
Java inheritance
even though the wording is different.
Spring AI describes embeddings as numerical representations that capture relationships between inputs, with distance between vectors providing a way to measure similarity.
3. Embedding Model
The component that produces embeddings is called an embedding model.
The architecture is:
Text
↓
Embedding Model
↓
Embedding Vector
Examples include:
OpenAI embedding models
Google GenAI embedding models
Ollama embedding models
Hugging Face / Transformer models
Mistral embedding models
Amazon Bedrock embedding models
Spring AI currently provides EmbeddingModel implementations for several of these providers.
4. Embedding vs LLM
This is an important distinction.
An embedding model:
Text
↓
Vector
A language model:
Prompt
↓
Generated Text
So:
Embedding Model
= Representation
LLM
= Generation
A RAG system commonly uses both.
Documents
↓
Embedding Model
↓
Vector Database
Question
↓
Embedding Model
↓
Vector Search
↓
Relevant Documents
↓
LLM
↓
Answer
5. Embedding Dimensions
An embedding vector has a dimensionality.
For example:
[0.1, 0.2, 0.3, ...]
could have:
384 dimensions
Another model might produce:
768 dimensions
Another might produce a different dimensionality.
The vector database must be configured consistently with the selected embedding model.
Spring AI's EmbeddingModel API exposes a dimensions() method, and its PGVector integration can derive the dimension from the configured embedding model when it is not explicitly supplied.
6. Why Dimensions Matter
Suppose your vector database is configured for:
VECTOR(768)
but your embedding model produces:
1536 dimensions
Those vectors cannot simply be inserted into that column.
The dimensions must match.
This is why your architecture should explicitly track:
Embedding Model
Embedding Dimension
Vector Database Configuration
7. One Embedding Model Per Vector Space
A common mistake is to mix embeddings from unrelated models inside the same vector index.
For example:
Document A
→ Model A → Vector
Document B
→ Model B → Vector
and then search them using:
Model C
This is generally not a valid design.
A better approach is:
Documents
↓
Same Embedding Model
↓
Vectors
↓
Same Vector Space
and:
Question
↓
Same Embedding Model
↓
Query Vector
↓
Search
The query and indexed documents need to be represented in a compatible embedding space.
8. Embedding a Single String in Java
Spring AI provides:
EmbeddingModel embeddingModel;
You can embed text with:
float[] vector =
embeddingModel.embed("Java inheritance");
The current Spring AI EmbeddingModel interface explicitly provides embed(String) and embed(List<String>) methods.
Then:
System.out.println(vector.length);
can show the vector dimension.
9. Batch Embeddings
You often have many documents.
Instead of:
Document 1 → API
Document 2 → API
Document 3 → API
Document 4 → API
you can use batch embedding:
Document 1
Document 2
Document 3
Document 4
↓
Embedding Model
↓
Vectors
Spring AI's current EmbeddingModel interface provides embed(List<String>) and document-batch operations.
Batching can be important when indexing thousands of documents.
10. Java Example
A simple example:
import org.springframework.ai.embedding.EmbeddingModel;
import org.springframework.stereotype.Service;
@Service
public class EmbeddingService {
private final EmbeddingModel embeddingModel;
public EmbeddingService(EmbeddingModel embeddingModel) {
this.embeddingModel = embeddingModel;
}
public float[] createEmbedding(String text) {
return embeddingModel.embed(text);
}
}
Then:
float[] vector =
embeddingService.createEmbedding(
"Java Spring Boot"
);
You now have a numerical representation that can be stored or compared.
11. Printing an Embedding
For learning purposes:
float[] vector =
embeddingModel.embed("Java");
System.out.println(
java.util.Arrays.toString(vector)
);
You might see:
[0.012, -0.044, 0.231, ...]
Do not expect the same values across different embedding models.
12. Similarity
Once you have vectors, the next task is comparing them.
Suppose:
Vector A
Vector B
A similarity calculation measures how closely related they are.
Common approaches include:
Cosine similarity
Dot product
Euclidean distance
L1 distance
The supported metric depends on the vector database and configuration.
For example, PGVector supports L2 distance, inner product, cosine distance, and L1 distance.
13. Cosine Similarity
Cosine similarity compares the orientation of vectors.
Conceptually:
Vector A
↗
/
/
/
Query Vector
Vectors pointing in similar directions have high cosine similarity.
This is commonly used for semantic search.
You do not normally calculate cosine similarity manually in a production RAG system.
The vector database usually handles the search.
14. Euclidean Distance
Another approach is Euclidean distance.
Conceptually:
Vector A ●────────● Vector B
distance
A smaller distance means the vectors are closer according to that metric.
PGVector supports L2/Euclidean distance through its vector operators.
15. Dot Product
Dot product is another common similarity measure.
Depending on the vector representation and normalization, dot product can be used for efficient similarity calculations.
PGVector supports inner-product search as well.
The important lesson is:
Choose a metric
that is compatible
with your embedding model
and search design.
16. Embeddings and Semantic Search
The complete process is:
Documents
↓
Embedding Model
↓
Vectors
↓
Vector Database
User Question
↓
Embedding Model
↓
Query Vector
↓
Vector Search
↓
Similar Documents
This is semantic search.
The LLM does not have to be involved yet.
17. Embeddings + Vector Database
The vector database stores information such as:
ID
Vector
Text
Metadata
For example:
{
"id": "doc-101",
"text": "Spring Boot dependency injection...",
"metadata": {
"category": "java",
"source": "spring-guide.pdf"
},
"embedding": [
0.13,
-0.42,
0.77
]
}
The actual vector is usually much larger than the abbreviated example above.
18. Embeddings + RAG
This is where embeddings become especially useful.
Suppose your application contains:
100,000 documents
A user asks:
How do I configure Spring Boot security?
The application does:
Question
↓
Embedding
↓
Vector Search
↓
Top Relevant Chunks
↓
LLM
↓
Answer
This is the retrieval portion of RAG.
19. Complete RAG Embedding Flow
The indexing side is:
PDF
↓
Text Extraction
↓
Chunking
↓
Embedding Model
↓
Vectors
↓
Vector Database
The question side is:
Question
↓
Embedding Model
↓
Query Vector
↓
Vector Database
↓
Top-K Results
Then:
Retrieved Content
+
Question
↓
LLM
↓
Answer
20. Local Embeddings with Ollama
Embeddings can be generated locally.
Spring AI currently provides OllamaEmbeddingModel, which uses Ollama's embedding API. Its current documentation shows configuration through spring.ai.ollama.embedding and examples using local embedding models such as mxbai-embed-large.
The architecture is:
Java
↓
Spring AI
↓
Ollama
↓
Local Embedding Model
↓
Vector
This is useful when you want your embedding generation to remain local.
21. Ollama Embedding Configuration
A Spring Boot application can configure an Ollama embedding model using properties such as:
spring:
ai:
ollama:
base-url: http://localhost:11434
embedding:
model: mxbai-embed-large
The current Spring AI documentation identifies http://localhost:11434 as the default Ollama base URL and documents the embedding model configuration.
The actual model must be available in your Ollama installation.
22. Local Embeddings + Local LLM
A fully local RAG system can use local models for both stages:
Document
↓
Local Embedding Model
↓
Vector Database
and:
Question
↓
Local Embedding Model
↓
Vector Search
↓
Context
↓
Ollama LLM
↓
Answer
So you could have:
Java
↓
Spring AI
├── Embedding Model
├── Vector Store
└── Ollama Chat Model
23. Cloud Embeddings
Embeddings do not have to be local.
A Java application can use a cloud embedding provider.
For example:
Java
↓
Spring AI
↓
Cloud Embedding API
↓
Vector
Spring AI currently supports embedding implementations including OpenAI and Google GenAI, among others.
For example, the current Spring AI OpenAI integration supports text embedding models and their configurable dimensions where supported by the model.
24. Google GenAI Embeddings
Google's GenAI embedding integration provides text embeddings through Gemini Developer API or Vertex AI.
Spring AI's current documentation describes these as dense vector representations designed to capture semantic relationships.
The architecture is:
Java
↓
Spring AI
↓
Google GenAI Embedding Model
↓
Vector
25. OpenAI Embeddings
OpenAI also provides embedding models.
Through Spring AI:
Java
↓
Spring AI
↓
OpenAI Embedding Model
↓
Vector
Spring AI's current OpenAI embedding documentation provides configuration through OpenAiEmbeddingModel and supports model-specific options such as dimensions for supported embedding models.
26. LangChain4j Embeddings
LangChain4j uses:
EmbeddingModel
as its abstraction for converting text into embeddings.
Its current API includes implementations for providers and local models such as OpenAI, Google, Ollama, Hugging Face, Jlama, ONNX, and others.
The conceptual API is:
Embedding embedding =
embeddingModel.embed(
"Java Spring Boot"
).content();
The exact implementation depends on the selected LangChain4j module.
27. Spring AI vs LangChain4j Embeddings
Spring AI
Uses:
EmbeddingModel
and integrates naturally with:
Spring Boot
VectorStore
ChatClient
RAG
LangChain4j
Uses:
EmbeddingModel
EmbeddingStore
ContentRetriever
and integrates closely with:
RAG
AI Services
Tools
Agents
Both approaches let you switch embedding providers without rewriting all application logic. Spring AI explicitly emphasizes portability of its EmbeddingModel abstraction, while LangChain4j provides a provider-independent embedding interface.
28. Embedding Store
In LangChain4j, embeddings are commonly placed into:
EmbeddingStore
The architecture becomes:
Text
↓
EmbeddingModel
↓
Embedding
↓
EmbeddingStore
Then:
Question
↓
EmbeddingModel
↓
Query Embedding
↓
EmbeddingStore
↓
Similar Content
29. Spring AI VectorStore
Spring AI uses:
VectorStore
The flow is:
Document
↓
EmbeddingModel
↓
VectorStore
Spring AI's VectorStore abstraction is designed to store and retrieve documents through vector similarity operations.
30. Embedding Batches
Imagine you have:
100,000 chunks
Generating embeddings one at a time can produce unnecessary overhead.
A better indexing pipeline is:
Chunks
↓
Batch
↓
Embedding Model
↓
Vectors
↓
Vector Store
Spring AI's embedding API supports batch embedding operations and batching strategies for document lists.
31. Embedding a Document
Spring AI supports embedding a Document directly:
float[] vector =
embeddingModel.embed(document);
This allows the embedding model to work with the document abstraction rather than only a raw string.
Conceptually:
Document
├── Text
└── Metadata
↓
Embedding Model
↓
Vector
Whether metadata is included in the actual embedding input depends on the embedding implementation and configuration. Spring AI's current API documents MetadataMode handling for implementations that support it.
32. Chunking Before Embedding
Do not normally embed a huge document as one giant item for RAG.
Instead:
PDF
↓
Text
↓
Chunks
↓
Embeddings
↓
Vector Database
For example:
Chapter 1
↓
Chunk 1
Chunk 2
Chunk 3
This makes retrieval more precise.
33. Metadata + Embeddings
Each vector can have metadata:
{
"source": "java-book.pdf",
"page": 52,
"category": "java",
"version": "21"
}
Then retrieval can apply metadata filters.
For example:
category = java
or:
version = 21
This is useful in large knowledge bases.
34. User-Specific Embeddings
For a multi-user application:
User A
↓
Document A
↓
Embedding
User B
↓
Document B
↓
Embedding
Metadata can associate vectors with:
userId
organizationId
departmentId
documentId
Then Java can apply authorization-aware filtering before results reach the LLM.
35. Embeddings for Semantic Search
You do not need an LLM to use embeddings.
Example:
Java
↓
Embedding Model
↓
Vector Database
↓
Semantic Search Results
The result can simply be a list:
1. Java inheritance tutorial
2. Object-oriented programming guide
3. Java class hierarchy
This is useful for:
Document Search
Product Search
FAQ Search
Knowledge Search
Recommendation Systems
36. Embeddings for Recommendations
Embeddings can also represent products or articles.
For example:
Product Description
↓
Embedding
↓
Vector
Then:
Current Product
↓
Embedding
↓
Similar Products
This creates a semantic recommendation system.
37. Embeddings for Classification
Embeddings can also be used as features for classification workflows.
For example:
Customer Message
↓
Embedding
↓
Similarity / Classifier
↓
Category
Potential categories:
Billing
Technical Support
Account
Sales
General
A developer can combine embeddings with rules or a separate classifier.
38. Embeddings for Duplicate Detection
Suppose users upload two documents:
Document A
Document B
Generate embeddings:
A → Vector A
B → Vector B
Then compare them.
High semantic similarity may indicate that the documents contain substantially similar information.
This is different from exact file comparison.
39. Embeddings for Question Matching
Suppose your application has:
10,000 FAQ questions
A user asks:
How can I reset my password?
Your stored FAQ may contain:
What should I do if I forgot my password?
Embedding-based search can identify them as semantically related.
This is a powerful use case for customer-support applications.
40. Embeddings + Chat Memory
Embeddings can also be used to create long-term semantic memory.
Instead of saving every conversation message only as raw text:
Conversation
↓
Embedding
↓
Vector Store
Later:
New User Message
↓
Embedding
↓
Memory Search
↓
Relevant Past Information
This is different from ordinary chat-history storage.
It is semantic memory.
41. Embeddings + AI Agent
An agent can use semantic search as one of its capabilities.
For example:
User
↓
AI Agent
├── Search Knowledge Base
├── Search Memory
├── Query Database
└── Call API
The vector store becomes one of the agent's information sources.
42. Vector Dimension + PGVector
PGVector is a practical option for Java developers using PostgreSQL.
Spring AI's current PGVector documentation shows a vector column such as:
embedding vector(1536)
and notes that the dimension should be replaced by the actual dimension produced by your embedding model.
For example:
CREATE TABLE documents (
id BIGSERIAL PRIMARY KEY,
content TEXT,
embedding VECTOR(768)
);
The 768 is only an example.
43. PGVector Indexing
PGVector supports approximate nearest-neighbor indexes such as HNSW and IVFFlat as well as exact search without a vector index.
Conceptually:
Vectors
↓
Vector Index
↓
Fast Search
HNSW and IVFFlat involve different trade-offs around build time, memory, speed, and recall.
For a beginner, the important idea is:
Vector Database
+
Vector Index
=
Efficient Similarity Search
44. PGVector + Spring AI Configuration
The current Spring AI PGVector documentation provides configuration such as:
spring:
ai:
vectorstore:
pgvector:
index-type: HNSW
distance-type: COSINE_DISTANCE
dimensions: 1536
The values must match your intended embedding configuration.
45. Embedding Pipeline Example
A complete Java ingestion pipeline can look like:
PDF
↓
DocumentReader
↓
Documents
↓
Text Splitter
↓
Chunks
↓
EmbeddingModel
↓
Vectors
↓
VectorStore
Spring AI's ETL architecture explicitly provides document readers, document transformers, and document writers for this kind of pipeline.
46. Query Pipeline Example
Then:
User Question
↓
EmbeddingModel
↓
Query Vector
↓
VectorStore
↓
Top-K Similar Chunks
↓
Prompt
↓
Chat Model
↓
Answer
This is the core of RAG.
47. Choosing a Local Embedding Model
For local development, you might use:
Ollama
↓
Embedding Model
The exact choice should depend on:
RAM
GPU/VRAM
Embedding Quality
Speed
Language Support
Vector Dimension
Context Requirements
Ollama's current Spring AI integration allows embedding models to be selected through the Ollama embedding configuration.
48. Do Not Change Embedding Models Casually
Suppose your current vector store contains:
100,000 vectors
generated using:
Embedding Model A
Now you switch to:
Embedding Model B
Your existing vectors may no longer be compatible with the new query vectors.
You may need to re-embed the documents.
So in production, treat embedding-model changes as an important data migration.
49. Reindexing
When changing the embedding model:
Existing Documents
↓
New Embedding Model
↓
New Vectors
↓
Replace / Rebuild Vector Index
This can be expensive for large databases.
Keep track of:
Embedding Model
Model Version
Dimension
Distance Metric
Index Configuration
50. Embedding Model Versioning
A useful metadata design is:
{
"embeddingModel": "my-model",
"embeddingVersion": "1",
"documentVersion": "3"
}
Then your application can identify which vectors were generated with which model.
This makes migrations easier.
51. RAG Accuracy Depends on More Than Embeddings
Good embeddings alone do not guarantee good RAG.
You also need:
Good chunking
Good queries
Good metadata
Good retrieval
Good ranking
Good context construction
Good LLM
Think of it as:
RAG Quality
=
Data
+
Chunking
+
Embeddings
+
Retrieval
+
Prompt
+
LLM
52. Embedding Search Is Not Exact Truth
A vector search returns items that are mathematically close according to the selected representation and metric.
That does not mean:
Top result
=
Guaranteed correct answer
The result must still be evaluated.
A good system can also use:
Similarity threshold
Metadata filtering
Reranking
Keyword search
LLM verification
53. Hybrid Search
Sometimes the best retrieval architecture combines:
Keyword Search
+
Vector Search
For example:
"CVE-2026-12345"
benefits from exact matching.
Whereas:
How do I configure Java authentication?
can benefit from semantic search.
Hybrid search combines both approaches.
54. Embeddings in an Enterprise Java Application
A production architecture could be:
USER
│
▼
Spring Boot API
│
▼
User Query
│
▼
Embedding Model
│
▼
Vector Database
│
▼
Relevant Documents
│
▼
Chat Model
│
▼
Answer
The Java application controls the entire workflow.
55. Embedding Service Abstraction
A good Java architecture can hide the provider behind an interface.
For example:
public interface EmbeddingService {
float[] embed(String text);
}
Then implementations can include:
OllamaEmbeddingService
OpenAiEmbeddingService
GoogleEmbeddingService
LocalEmbeddingService
Now your application code does not need to know which provider is being used.
56. Multi-Provider Embedding Architecture
The architecture can be:
EmbeddingService
│
┌──────────────┼──────────────┐
▼ ▼ ▼
Ollama OpenAI Google
│ │ │
Local Cloud Cloud
This is useful for testing and migration.
For example:
Development
→ Local Embeddings
Production
→ Selected Embedding Provider
The rest of your Java application stays behind the abstraction.
57. Beginner Project
A good first project is:
Java Semantic Search
Create:
10,000 Java questions
Then:
Questions
↓
Embedding Model
↓
Vector Store
When the user searches:
How do I inherit another Java class?
the system retrieves:
Java inheritance
Java extends keyword
Class inheritance examples
No LLM is required initially.
58. Next Project: Java RAG
Then add an LLM:
User Question
↓
Embedding
↓
Vector Search
↓
Relevant Documents
↓
LLM
↓
Answer
Now you have a RAG application.
59. Local Java RAG Project
A fully local learning project can be:
Spring Boot
│
├── Spring AI
│
├── Ollama Embedding Model
│
├── PGVector / Vector Store
│
└── Ollama Chat Model
Data flow:
PDF
↓
Local Embeddings
↓
PGVector
↓
Question
↓
Local Embedding
↓
Similarity Search
↓
Ollama
↓
Answer
60. Final Embedding Architecture
The complete concept is:
DOCUMENTS
│
▼
Text Chunking
│
▼
Embedding Model
│
▼
Embedding Vectors
│
▼
Vector Database
│
│
│
USER │
│ │
▼ │
Question │
│ │
▼ │
Embedding Model │
│ │
└──────────┬──────────┘
▼
Similarity Search
│
▼
Relevant Documents
│
▼
LLM
│
▼
Answer
The roles are:
Embedding Model
→ Converts information into vectors
Vector
→ Numerical representation
Vector Database
→ Stores and searches vectors
Similarity Search
→ Finds semantically related information
RAG
→ Uses retrieved information to help generate an answer
LLM
→ Generates the final response
61. Java Embeddings Learning Path
A practical learning order is:
Java
↓
Spring Boot
↓
LLM API
↓
Embedding Concept
↓
Embedding Model
↓
Vector Dimension
↓
Similarity
↓
Vector Database
↓
Metadata
↓
Semantic Search
↓
RAG
↓
Reranking
↓
Hybrid Search
↓
AI Agents
Start with this simple concept:
Text
↓
Embedding
↓
Vector
Then:
Vector
↓
Vector Database
↓
Similarity Search
Finally:
Similarity Search
↓
Retrieved Context
↓
LLM
↓
RAG Answer
Conclusion
Embeddings are the bridge between human language and vector-based AI search.
The fundamental flow is:
Text
↓
Embedding Model
↓
Vector
↓
Vector Database
↓
Similarity Search
And when combined with an LLM:
Question
↓
Embedding
↓
Vector Search
↓
Relevant Context
↓
LLM
↓
Answer
That architecture powers many modern AI applications, including:
Semantic Search
RAG
Document Question Answering
Recommendation Systems
Knowledge Assistants
Chatbots
AI Agents
Duplicate Detection
Semantic Classification
For Java developers, Spring AI provides a portable EmbeddingModel abstraction and integrations for providers including Ollama, OpenAI, Google GenAI, Transformers, and others.
LangChain4j provides a similar EmbeddingModel abstraction and integrates embeddings with embedding stores and RAG workflows.
For local AI, Spring AI can connect Java applications to Ollama's embedding API. For persistent vector storage, PostgreSQL with PGVector is one practical Java-friendly option, with the important requirement that the database vector dimension matches the embedding model's output dimension.
Once you understand embeddings, the complete picture becomes:
Java
↓
Embedding Model
↓
Vector Database
↓
RAG
↓
LLM
↓
AI Application
That is the foundation for building semantic-search systems, document assistants, private knowledge bases, and more advanced Java AI agents.

Post a Comment