Spring AI Vector Store with Infinispan
What You Will Learn
How to use Infinispan as a Spring AI vector store to store document embeddings and perform semantic similarity search. The auto-configuration handles schema registration and cache creation automatically.
Prerequisites
-
Java 17+
-
Docker or Podman (optional)
You can start an Infinispan Server manually with Docker or Podman:
docker run -it --rm -p 11222:11222 -e USER=admin -e PASS=password quay.io/infinispan/server:latest
|
Tip
|
You can replace docker with podman in the command above if you use Podman.
|
|
Tip
|
If no server is running on localhost:11222, the application automatically starts an Infinispan container using Testcontainers.
|
Step 1: Add Dependencies
Add the Infinispan Spring AI vector store auto-configuration and a local embedding model:
<dependency>
<groupId>org.infinispan</groupId>
<artifactId>spring-ai-autoconfigure-vector-store-infinispan</artifactId>
</dependency>
<dependency>
<groupId>org.infinispan</groupId>
<artifactId>spring-ai-infinispan-store</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-transformers</artifactId>
</dependency>
The TransformersEmbeddingModel runs locally using ONNX. No API keys or external services needed.
Step 2: Configure the Connection
In application.properties, configure the Infinispan connection:
infinispan.remote.server-list=127.0.0.1:11222
infinispan.remote.auth-username=admin
infinispan.remote.auth-password=password
The vector store auto-configuration creates the cache and registers the Protobuf schema automatically.
Step 3: Provide an Embedding Model
Define an EmbeddingModel bean. The auto-configuration uses it to convert text to vectors:
@Configuration(proxyBeanMethods = false)
public class Config {
@Bean
public EmbeddingModel embeddingModel() {
return new TransformersEmbeddingModel();
}
}
Step 4: Store Documents
Add documents with metadata to the vector store. Each document is automatically embedded and stored in Infinispan:
@PostMapping(path = "/load")
public String loadData() {
List<Document> documents = List.of(
new Document("Infinispan is a distributed in-memory key/value data store",
Map.of("source", "docs", "topic", "overview")),
new Document("Infinispan supports vector search for AI use cases",
Map.of("source", "docs", "topic", "ai")),
new Document("Spring AI provides a unified API for AI engineering",
Map.of("source", "spring", "topic", "ai")),
new Document("Infinispan can be used as a vector store with Spring AI",
Map.of("source", "tutorial", "topic", "integration")));
vectorStore.add(documents);
return "Loaded " + documents.size() + " documents";
}
Step 5: Search by Semantic Similarity
Query with natural language. The vector store finds semantically similar documents:
@GetMapping(path = "/search")
public List<Document> search(@RequestParam(defaultValue = "How can I use Infinispan with AI?") String query) {
return vectorStore.similaritySearch(
SearchRequest.builder()
.query(query)
.topK(3)
.similarityThreshold(0)
.build());
}
Step 6: Run the Tutorial
mvn spring-boot:run
Then load documents and search:
curl -X POST http://localhost:8080/load
curl "http://localhost:8080/search?query=How+can+I+use+Infinispan+with+AI"
Results are ranked by cosine similarity. Documents about vector search and Spring AI integration will rank highest.
What’s Next
-
Try Spring AI Chat Memory to persist chat conversations in Infinispan
-
Try LangChain4j Embedding Store for direct vector usage without Spring AI


