Spring AI with Milvus Vector Database

This tutorial will guide you through integrating Spring AI with Milvus, a popular vector database, in a Spring Boot application. We will set up a basic Spring Boot project, configure it to use the Milvus vector database, and create endpoints to generate and manage vector data using Spring AI.


Milvus is an open-source vector database designed for scalable similarity search and high-dimensional vector data analysis. Integrating Milvus with Spring AI allows developers to efficiently store, manage, and query embeddings generated by AI models. This tutorial demonstrates how to set up and use Spring AI with Milvus in a Spring Boot application.

1. Setting Up the Project

Step 1: Create a New Spring Boot Project

Use Spring Initializr to create a new Spring Boot project. Include dependencies for Spring Web and Spring AI.

Using Spring Initializr:

  • Go to start.spring.io
  • Select:
    • Project: Maven Project
    • Language: Java
    • Spring Boot: 3.0.0 (or latest)
    • Dependencies: Spring Web
  • Generate the project and unzip it.

Step 2: Add Dependencies

In your project's pom.xml, add the necessary dependencies, including the Milvus VectorStore Spring Boot Starter.


2. Configuring the Milvus Vector Database

Step 1: Set Up Milvus

Follow the Milvus installation guide to set up Milvus on your local machine or a cloud server.

Step 2: Add Configuration to Spring Boot

Add configuration details for your Milvus vector database in application.properties or application.yml.

For application.properties:


For application.yml:

  host: localhost
  port: 19530

Step 3: Create a Configuration Class

Create a configuration class to set up the Milvus client.

package com.example.demo.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.ai.milvus.store.MilvusClientFactory;

public class MilvusConfig {

    public MilvusClientFactory milvusClientFactory() {
        return new MilvusClientFactory("localhost", 19530);

3. Implementing Vector Database Integration

Example: Storing and Querying Vectors

Create a service to handle vector operations, including storing and querying vectors.


package com.example.demo.service;

import org.springframework.ai.milvus.store.MilvusClientFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import io.milvus.param.collection.CreateCollectionParam;
import io.milvus.param.collection.FieldType;
import io.milvus.param.collection.VectorFieldType;
import io.milvus.param.dml.InsertParam;
import io.milvus.param.dml.SearchParam;
import io.milvus.param.schema.Schema;

import java.util.Collections;
import java.util.List;

public class VectorService {

    private MilvusClientFactory milvusClientFactory;

    private static final String COLLECTION_NAME = "example_collection";

    public void createCollection() {
        VectorFieldType vectorField = VectorFieldType.newBuilder()

        FieldType primaryKey = FieldType.newBuilder()

        Schema schema = Schema.newBuilder()


    public void insertVectors(List<List<Float>> vectors) {
        InsertParam insertParam = InsertParam.newBuilder()
                .withFields(Collections.singletonMap("embedding", vectors))

    public List<List<Float>> searchVectors(List<Float> queryVector) {
        SearchParam searchParam = SearchParam.newBuilder()
                .withSearchParams(Collections.singletonMap("nprobe", "10"))
        return milvusClientFactory.getClient().search(searchParam).getResults();


package com.example.demo.controller;

import com.example.demo.service.VectorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

public class VectorController {

    private VectorService vectorService;

    public void createCollection() {

    public void insertVectors(@RequestBody List<List<Float>> vectors) {

    public List<List<Float>> searchVectors(@RequestParam List<Float> queryVector) {
        return vectorService.searchVectors(queryVector);

4. Testing the Integration

Step 1: Run the Application

Run your Spring Boot application. Ensure the application starts without errors.

Step 2: Test Endpoints

Use Postman, curl, or your browser to test the endpoints.

  • Create Collection:

    POST http://localhost:8080/createCollection
  • Insert Vectors:

    POST http://localhost:8080/insertVectors
    Body: [[1.0, 2.0, 3.0, ..., 128.0], ...]
  • Search Vectors:

    GET http://localhost:8080/searchVectors?queryVector=[1.0, 2.0, 3.0, ..., 128.0]


This tutorial demonstrated how to set up and integrate Milvus with a Spring Boot application using Spring AI. You learned how to create a service and controller for storing and querying vectors in the Milvus database, and how to create a simple frontend to interact with the database. This setup provides a foundation for building more complex and feature-rich AI applications. Explore further customization and