Get Started with the Spring AI Integration
On this page
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:
Set up the environment.
Create an Atlas Vector Search index.
Store vector embedding data on Atlas.
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.
Background
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.
Prerequisites
To complete this tutorial, you must have the following:
An Atlas account with a cluster running MongoDB version 6.0.11, 7.0.2, or later (including RCs). Ensure that your IP address is included in your Atlas project's access list. To learn more, see Create a Cluster.
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.
Java Development Kit (JDK) version 8 or later.
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 or Gradle to build and run your project.
Set Up the Environment
You must first set up the environment for this tutorial, which includes adding the necessary dependencies and setting configuration properties.
Create a Spring Java application.
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.
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
Click GENERATE to download a zipped version of your Spring project. Unzip the file and open it in your IDE.
Add dependencies.
Spring AI provides Spring Boot auto-configuration for Atlas Vector Search.
Add the following dependencies to the
dependencies
array in your project'spom.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> Next, ensure that your
pom.xml
file contains adependencyManagement
entry for the Spring AI Bill of Materials (BOM).Important
Set the
spring-ai.version
constant used for the Spring AI BOM to1.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.
Finally, add the Spring AI Snapshot repository into the
repositories
entry in thepom.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.
Define application properties.
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:
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.
Create the Atlas Vector Search Index
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.
Enable schema initialization.
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:
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" } ] }
Use Atlas as a Vector Store
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.
Add import statements.
Paste the following code into the Config.java
file to
import the necessary classes:
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;
Reference application properties.
Paste the following code into the Config.java
file to
reference the values that you set in your application properties file:
public class Config { private String openAiKey; private String databaseName; private String collectionName; private String indexName; private String mongoUri; private Boolean initSchema; // Add beans here... }
Create the VectorStore
Spring bean.
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.
public VectorStore mongodbVectorStore(MongoTemplate mongoTemplate, EmbeddingModel embeddingModel) { return new MongoDBAtlasVectorStore(mongoTemplate, embeddingModel, MongoDBAtlasVectorStore.MongoDBVectorStoreConfig.builder().build(), initSchema); }
Store Custom Data and Run a Semantic Search Query
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.
Create Endpoints
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.
Add import statements.
Paste the following code into the Controller.java
file to
import the necessary classes:
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;
Create the controller.
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.
public class Controller { private VectorStore vectorStore; // Add endpoints here... }
Create an endpoint to add documents to the vector store.
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:
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"; }
Create an endpoint to perform a semantic search.
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:
1 2 public 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 }
(Optional) Perform a semantic search with metadata filtering.
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:
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.
Access Endpoints
After you run your application, you can access the endpoints to first add documents to the vector store, then perform a semantic search query.
Access endpoints from within your terminal.
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"}}]
Next Steps
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: