How to Turn Text into Searchable Vectors with Spring AI Embeddings and Vector Stores
This article explains why traditional keyword search fails on synonyms, introduces Spring AI's local ONNX‑based EmbeddingModel to convert text into high‑dimensional vectors, shows how to store and query those vectors with SimpleVectorStore, and compares production‑grade vector store options.
To let AI answer based on your documents, the first step is to make the documents semantically searchable. Keyword matching cannot handle synonyms such as "tomato" and "西红柿" – embeddings solve this.
Why Vectors Are Needed
Traditional keyword search performs literal matching, so a query for "tomato" will not retrieve a document that only contains the Chinese term "西红柿" even though the meanings are identical. An embedding maps a piece of text to a high‑dimensional numeric vector (e.g., 384 dimensions). The closer the semantics, the smaller the vector distance, turning "find related content" into "find the nearest vectors". This is the core principle of semantic retrieval and the foundation of Retrieval‑Augmented Generation (RAG).
EmbeddingModel: Text to Vector
⚠️ DeepSeek does not provide an embedding model. This guide uses Spring AI’s built‑in local Transformers, which run offline on ONNX with zero API cost.
Add the starter dependency:
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-starter-model-transformers</artifactId>
</dependency>Inject the model and convert a sentence to a vector in one line:
@Autowired
EmbeddingModel embeddingModel;
float[] vector = embeddingModel.embed("Spring AI 是什么");
System.out.println(vector.length); // e.g., 384Common methods: embed(String) – single string embed(List<String>) – batch dimensions() – view vector dimension
VectorStore: Store Vectors + Similarity Search
Add the vector‑store module:
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-vector-store</artifactId>
</dependency>For learning use the in‑memory SimpleVectorStore:
// Build an in‑memory vector store using the embedding model
VectorStore vectorStore = SimpleVectorStore.builder(embeddingModel).build();
// Add documents (internally vectorized)
vectorStore.add(List.of(
new Document("Spring AI 是 Spring 官方的 AI 应用开发框架"),
new Document("DeepSeek 是国产大模型"),
new Document("Docker 用于应用容器化部署")));
// Semantic search: retrieve the top 2 most relevant documents
List<Document> results = vectorStore.similaritySearch(
SearchRequest.builder()
.query("介绍一下 Spring AI")
.topK(2)
.build());
results.forEach(doc -> System.out.println(doc.getText()));
// Prints the first document even though the query wording differs SearchRequestcan configure topK (number of results) and similarityThreshold (relevance cutoff).
Production Vector Stores
SimpleVectorStoreloses data on restart and cannot handle large datasets, so production should switch to a dedicated vector store. The VectorStore interface remains unchanged; only the starter and connection settings need to be changed.
PgVector – PostgreSQL plugin, runs directly on PostgreSQL.
Redis – Fast, suitable for teams already using Redis.
Milvus – Professional vector database for massive data.
Elasticsearch – Combines full‑text search with vectors.
Day 8 Summary
Embedding : Maps text to a vector; semantically similar texts produce nearby vectors.
EmbeddingModel : Vectorization model; embed(text) → float[].
Transformers starter : Local ONNX embedding, offline with zero cost.
VectorStore : Stores vectors and enables similarity search.
SimpleVectorStore : In‑memory vector store for learning.
similaritySearch : Semantic retrieval with configurable topK and similarity threshold.
Next Preview
With a vector store in place, the next step is to automatically chunk a PDF or Word product manual, embed the chunks, and load them into the store. Day 9 will cover the document ETL pipeline – read, transform, write.
Related Links
Embeddings API: https://docs.spring.io/spring-ai/reference/api/embeddings.html
Local ONNX embeddings: https://docs.spring.io/spring-ai/reference/api/embeddings/onnx.html
Vector DB API: https://docs.spring.io/spring-ai/reference/api/vectordbs.htmlSigned-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Tech Ocean
Focused on AI programming, sharing ready-to-use development efficiency solutions.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
