Docs Menu
Docs Home
/
MongoDB Atlas
/ /

Get Started with the Spring AI Integration

On this page

  • Background
  • Prerequisites
  • Set Up the Environment
  • Create the Atlas Vector Search Index
  • Use Atlas as a Vector Store
  • Store Custom Data and Run a Semantic Search Query
  • Next Steps

You can integrate Atlas Vector Search with Spring AI to build generative AI applications by using the MongoDB Java Sync Driver. This tutorial demonstrates how to start using Atlas Vector Search as the vector store for Spring AI, then how to perform a semantic search on your data.

Specifically, you perform the following actions:

  1. Set up the environment.

  2. Create an Atlas Vector Search index.

  3. Store vector embedding data on Atlas.

  4. Run a semantic search query on your data.

Tip

Completed Sample Application

To download a completed version of the application that this tutorial demonstrates how to build, see the Next Steps section.

Spring AI is an application framework from Spring that allows you to combine various AI services and plugins with your applications. You can use Spring AI for a variety of text-based AI use cases.

You can use Atlas as a vector database and use Atlas Vector Search to implement RAG by retrieving semantically similar documents from your data. To learn more about RAG, see Retrieval-Augmented Generation (RAG) with Atlas Vector Search.

To complete this tutorial, you must have the following:

  • An Atlas cluster running MongoDB version 6.0.11, 7.0.2, or later. Ensure that your IP address is included in your Atlas project's access list.

  • An OpenAI API Key. You must have a paid OpenAI account with credits available for API requests. To learn more about registering an OpenAI account, see the OpenAI API website.

  • An environment to set up and run a Java application. We recommend that you use an integrated development environment (IDE) such as IntelliJ IDEA or Eclipse IDE to configure Maven to build and run your project.

You must first set up the environment for this tutorial, which includes adding the necessary dependencies and setting configuration properties.

1
  1. Navigate to the Spring Initializr and configure your project with the following settings:

    • Project: Maven

    • Language: Java

    • Spring Boot: You can use the default version that is selected.

    • Project Metadata:

    • Java: 21

    • You can use default values for all other fields.

  2. On the right side of the Spring Initializr, click ADD DEPENDENCIES, then search for and add the following dependencies:

    • MongoDB Atlas Vector Database

    • Spring Data MongoDB

  3. Click GENERATE to download a zipped version of your Spring project. Unzip the file and open it in your IDE.

2
  1. Spring AI provides Spring Boot auto-configuration for Atlas Vector Search.

    Add the following dependencies to the dependencies array in your project's pom.xml file. These dependencies add Spring AI and the auto-configuration library to your application:

    pom.xml
    <dependency>
    <groupId>org.springframework.ai</groupId>
    <artifactId>spring-ai-openai-spring-boot-starter</artifactId>
    </dependency>
    <dependency>
    <groupId>org.springframework.ai</groupId>
    <artifactId>spring-ai-spring-boot-autoconfigure</artifactId>
    </dependency>
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
  2. Next, ensure that your pom.xml file contains a dependencyManagement entry for the Spring AI Bill of Materials (BOM).

    Important

    Set the spring-ai.version constant used for the Spring AI BOM to 1.0.0-SNAPSHOT to implement the most recent Spring AI features into your application.

    To learn more about the Spring AI BOM, see the Dependency Management section of the Spring AI documentation.

  3. Finally, add the Spring AI Snapshot repository into the repositories entry in the pom.xml file:

    pom.xml
    <repository>
    <id>spring-snapshots</id>
    <name>Spring Snapshots</name>
    <url>https://repo.spring.io/snapshot</url>
    <releases>
    <enabled>false</enabled>
    </releases>
    </repository>

    To learn more about these repositories, see the Add Milestone and Snapshot Repositories section of the Spring AI documentation.

    After you finish editing the pom.xml file, reload your project to make sure your dependencies are installed.

3

Locate the src/main/resources/application.properties file and replace the contents of that file with the following properties. Replace the placeholders with your OpenAI API key and your Atlas connection string:

src/main/resources/application.properties
spring.application.name=springai-mongodb
spring.ai.openai.api-key=<OpenAI API Key>
spring.ai.openai.embedding.options.model=text-embedding-ada-002
spring.data.mongodb.uri=<connection string>
spring.data.mongodb.database=springai_test
spring.ai.vectorstore.mongodb.indexName=vector_index
spring.ai.vectorstore.mongodb.collection-name=vector_store

Note

Your connection string should use the following format:

mongodb+srv://<db_username>:<db_password>@<clusterName>.<hostname>.mongodb.net/?<settings>

To learn more about retrieving your connection string, see the Get Started with Atlas tutorial.

To enable vector search queries on your vector store, you must create an Atlas Vector Search index on the springai_test.vector_store collection.

Note

Required Access

To create an Atlas Vector Search index, you must have Project Data Access Admin or higher access to the Atlas project.

1

When you configure Atlas as the vector store in your application, Spring AI can initialize the backend schema automatically. This initialization includes creating an Atlas Vector Search index on the collection that contains your vector embeddings.

To enable schema initialization, add the following setting to your application.properties file:

src/main/resources/application.properties
spring.ai.vectorstore.mongodb.initialize-schema=true

Specifying initialize-schema=true causes Spring AI to programmatically create an Atlas Vector Search index on your cluster. To learn more, see Create an Atlas Vector Search Index.

Note

Known Issue: Existing Index

If you have an existing Atlas Vector Search index called vector_index on the springai_test.vector_store collection, Spring AI won't create an additional index. Because of this, you might experience errors later in the tutorial if the existing index was configured with incompatible settings, such as a different number of dimensions.

Ensure that your index has the following configuration:

{
"fields": [
{
"numDimensions": 1536,
"path": "embedding",
"similarity": "cosine",
"type": "vector"
}
]
}

This section demonstrates how to configure Atlas as a vector database, also known as a vector store, so that you can store the vector embeddings of your custom data.

Locate the src/main/java/com/example/demo/DemoApplication.java file in your project. At the same level as this file, create a directory called config, then create a file in this directory called Config.java to set up your Spring App configuration.

The following steps demonstrate how to create the Bean objects needed to prepare the vector store.

1

Paste the following code into the Config.java file to import the necessary classes:

/config/Config.java
import org.springframework.ai.embedding.EmbeddingModel;
import org.springframework.ai.openai.OpenAiEmbeddingModel;
import org.springframework.ai.openai.api.OpenAiApi;
import org.springframework.ai.vectorstore.MongoDBAtlasVectorStore;
import org.springframework.ai.vectorstore.VectorStore;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.mongodb.core.MongoTemplate;
2

Paste the following code into the Config.java file to reference the values that you set in your application properties file:

/config/Config.java
@Configuration
@SpringBootConfiguration
@EnableAutoConfiguration
public class Config {
@Value("${spring.ai.openai.api-key}")
private String openAiKey;
@Value("${spring.data.mongodb.database}")
private String databaseName;
@Value("${spring.ai.vectorstore.mongodb.collection-name:vector_store}")
private String collectionName;
@Value("${spring.ai.vectorstore.mongodb.indexName:vector_index}")
private String indexName;
@Value("${spring.data.mongodb.uri}")
private String mongoUri;
@Value("${spring.ai.vectorstore.mongodb.initialize-schema}")
private Boolean initSchema;
// Add beans here...
}
3

Next, paste the following code to generate the OpenAiEmbeddingModel instance that uses the OpenAI API to create vector embeddings:

/config/Config.java
@Bean
public EmbeddingModel embeddingModel() {
return new OpenAiEmbeddingModel(new OpenAiApi(openAiKey));
}
4

Finally, paste the following code to create bean that returns a VectorStore instance. The VectorStore instance uses the MongoTemplate that corresponds to your deployment and the OpenAiEmbeddingModel created in the preceding step.

/config/Config.java
@Bean
public VectorStore mongodbVectorStore(MongoTemplate mongoTemplate, EmbeddingModel embeddingModel) {
return new MongoDBAtlasVectorStore(mongoTemplate, embeddingModel,
MongoDBAtlasVectorStore.MongoDBVectorStoreConfig.builder().build(), initSchema);
}

In this section, you can learn how to create endpoints in your Java application to store vector embeddings of custom data in Atlas, then run a semantic search query on that data.

At the same level as the config folder, create a controller folder, then create a Controller.java file to set up your API endpoints. The following steps demonstrate how to create GET endpoints to add data to your vector store and run a semantic search query by using the similaritySearch() method.

1

Paste the following code into the Controller.java file to import the necessary classes:

/controller/Controller.java
import org.springframework.ai.document.Document;
import org.springframework.ai.vectorstore.VectorStore;
import org.springframework.ai.vectorstore.SearchRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
2

Paste the following code to perform the following tasks:

  • Annotate the Controller class to mark it as the application controller.

  • Create a mapping to map requests to the /tutorial path.

  • Autowire the VectorStore bean.

/controller/Controller.java
@RestController
@RequestMapping("/tutorial")
public class Controller {
@Autowired
private VectorStore vectorStore;
// Add endpoints here...
}
3

Paste the following code into your controller to create a GET endpoint that creates sample documents and saves them to your vector store as vector embeddings:

/controller/Controller.java
@GetMapping("/add")
public String addDocuments() {
List<Document> docs = List.of(
new Document("Proper tuber planting involves site selection, proper timing, and exceptional care. Choose spots with well-drained soil and adequate sun exposure. Tubers are generally planted in spring, but depending on the plant, timing varies. Always plant with the eyes facing upward at a depth two to three times the tuber's height. Ensure 4 inch spacing between small tubers, expand to 12 inches for large ones. Adequate moisture is needed, yet do not overwater. Mulching can help preserve moisture and prevent weed growth.", Map.of("author", "A", "type","post")),
new Document("Successful oil painting necessitates patience, proper equipment, and technique. Begin with a carefully prepared, primed canvas. Sketch your composition lightly before applying paint. Use high-quality brushes and oils to create vibrant, long-lasting artworks. Remember to paint 'fat over lean,' meaning each subsequent layer should contain more oil to prevent cracking. Allow each layer to dry before applying another. Clean your brushes often and avoid solvents that might damage them. Finally, always work in a well-ventilated space.", Map.of("author", "A")),
new Document("For a natural lawn, selection of the right grass type suitable for your climate is crucial. Balanced watering, generally 1 to 1.5 inches per week, is important; overwatering invites disease. Opt for organic fertilizers over synthetic versions to provide necessary nutrients and improve soil structure. Regular lawn aeration helps root growth and prevents soil compaction. Practice natural pest control and consider overseeding to maintain a dense sward, which naturally combats weeds and pest.", Map.of("author", "B", "type","post"))
);
vectorStore.add(docs);
return "Documents added successfully!\n";
}
4

Paste the following code into your controller to create a GET endpoint that performs a semantic search query for the phrase "learn how to grow things" and returns the two most relevant results:

/controller/Controller.java
1@GetMapping("/search")
2public List<Map<String, Object>> searchDocuments() {
3
4 List<Document> results = vectorStore.similaritySearch(
5 SearchRequest
6 .query("learn how to grow things")
7 .withTopK(2)
8 );
9
10 return results.stream().map(doc -> Map.of(
11 "content", doc.getContent(),
12 "metadata", doc.getMetadata()
13 )).collect(Collectors.toList());
14}
5

To perform a search with metadata filtering, you can use the Filter.Expression builder class from the Java Sync driver.

You can use an MQL match expression to pre-filter for documents. This example filters for documents in which the value of the author field is "A". Then, it performs a semantic search query for the phrase "learn how to grow things".

In the body of the searchDocuments() method defined in the preceding step, replace the code that calls the similaritySearch() method (lines 4-8 in the preceding block) with the following code:

/controller/Controller.java
FilterExpressionBuilder b = new FilterExpressionBuilder();
List<Document> results = vectorStore.similaritySearch(
SearchRequest.defaults()
.withQuery("learn how to grow things")
.withTopK(2)
.withSimilarityThreshold(0.5)
.withFilterExpression(b.eq("author", "A").build())
);

Note

You must add the path for your metadata field to your Atlas Vector Search index. See the About the filter Type section of the How to Index Fields for Vector Search tutorial to learn more.

To learn more about metadata pre-filtering, see Atlas Vector Search Pre-Filter.

After you run your application, you can access the endpoints to first add documents to the vector store, then perform a semantic search query.

1

Build and run your application by using your IDE tools. If you are using default settings, your application runs locally on port 8080.

2

After you confirm that your application is running, run the following command in your terminal to access the add endpoint, which converts the sample data to vector embeddings and inserts the embeddings in Atlas:

curl -X GET http://localhost:8080/tutorial/add
Documents added successfully!

Tip

After accessing the endpoint, you can view your vector embeddings in the Atlas UI by navigating to the springai_test.vector_store collection in your cluster.

Then, run the following command in your terminal to access the search endpoint to perform the semantic search:

curl -X GET http://localhost:8080/tutorial/search
[{"content":"For a natural lawn, selection of the right grass type
suitable for your climate is crucial. Balanced watering, generally 1 to
1.5 inches per week, is important; overwatering invites disease. Opt for
organic fertilizers over synthetic versions to provide necessary
nutrients and improve soil structure. Regular lawn aeration helps root
growth and prevents soil compaction. Practice natural pest control and
consider overseeding to maintain a dense sward, which naturally combats
weeds and
pest.","metadata":{"type":"post","author":"B"}},{"content":"Proper tuber
planting involves site selection, proper timing, and exceptional care.
Choose spots with well-drained soil and adequate sun exposure. Tubers
are generally planted in spring, but depending on the plant, timing
varies. Always plant with the eyes facing upward at a depth two to three
times the tuber's height. Ensure 4 inch spacing between small tubers,
expand to 12 inches for large ones. Adequate moisture is needed, yet do
not overwater. Mulching can help preserve moisture and prevent weed
growth.","metadata":{"type":"post","author":"A"}}]

You can view and download a completed version of this app from GitHub. You can use the full app to troubleshoot your own application or to test the functionality quickly.

MongoDB also provides the following developer resources:

Back

Haystack