|
| 1 | += MongoDB Atlas |
| 2 | + |
| 3 | +WIP: Please consider contributing docs: https://github.com/spring-projects/spring-ai/issues/456[Add documentation for the MongoDB Atlas Vector Store] |
| 4 | + |
| 5 | +https://www.mongodb.com/basics/vector-databases[MongoDB Atlas Vector Search] is a fully managed cloud database service that provides the easiest way to deploy, operate, and scale a MongoDB database in the cloud. |
| 6 | + |
| 7 | +== Prerequisites |
| 8 | + |
| 9 | +TODO: Add prerequisites instructions |
| 10 | + |
| 11 | +== Auto-configuration |
| 12 | + |
| 13 | +Spring AI provides Spring Boot auto-configuration for the MongoDB Atlas Vector Sore. |
| 14 | +To enable it, add the following dependency to your project's Maven `pom.xml` file: |
| 15 | + |
| 16 | +[source, xml] |
| 17 | +---- |
| 18 | +<dependency> |
| 19 | + <groupId>org.springframework.ai</groupId> |
| 20 | + <artifactId>spring-ai-mongodb-atlas-store-spring-boot-starter</artifactId> |
| 21 | +</dependency> |
| 22 | +---- |
| 23 | + |
| 24 | +or to your Gradle `build.gradle` build file. |
| 25 | + |
| 26 | +[source,groovy] |
| 27 | +---- |
| 28 | +dependencies { |
| 29 | + implementation 'org.springframework.ai:spring-ai-mongodb-atlas-store-spring-boot-starter' |
| 30 | +} |
| 31 | +---- |
| 32 | + |
| 33 | +TIP: Refer to the xref:getting-started.adoc#dependency-management[Dependency Management] section to add the Spring AI BOM to your build file. |
| 34 | + |
| 35 | +TIP: Refer to the xref:getting-started.adoc#repositories[Repositories] section to add Milestone and/or Snapshot Repositories to your build file. |
| 36 | + |
| 37 | +Additionally, you will need a configured `EmbeddingClient` bean. Refer to the xref:api/embeddings.adoc#available-implementations[EmbeddingClient] section for more information. |
| 38 | + |
| 39 | +Here is an example of the needed bean: |
| 40 | + |
| 41 | +[source,java] |
| 42 | +---- |
| 43 | +@Bean |
| 44 | +public EmbeddingClient embeddingClient() { |
| 45 | + // Can be any other EmbeddingClient implementation. |
| 46 | + return new OpenAiEmbeddingClient(new OpenAiApi(System.getenv("SPRING_AI_OPENAI_API_KEY"))); |
| 47 | +} |
| 48 | +---- |
| 49 | + |
| 50 | +== Metadata filtering |
| 51 | + |
| 52 | +You can leverage the generic, portable xref:api/vectordbs.adoc#metadata-filters[metadata filters] with MongoDB Atlas store as well. |
| 53 | + |
| 54 | +For example, you can use either the text expression language: |
| 55 | + |
| 56 | +[source,java] |
| 57 | +---- |
| 58 | +vectorStore.similaritySearch( |
| 59 | + SearchRequest.defaults() |
| 60 | + .withQuery("The World") |
| 61 | + .withTopK(TOP_K) |
| 62 | + .withSimilarityThreshold(SIMILARITY_THRESHOLD) |
| 63 | + .withFilterExpression("author in ['john', 'jill'] && 'article_type' == 'blog'")); |
| 64 | +---- |
| 65 | + |
| 66 | +or programmatically using the `Filter.Expression` DSL: |
| 67 | + |
| 68 | +[source,java] |
| 69 | +---- |
| 70 | +FilterExpressionBuilder b = new FilterExpressionBuilder(); |
| 71 | +
|
| 72 | +vectorStore.similaritySearch(SearchRequest.defaults() |
| 73 | + .withQuery("The World") |
| 74 | + .withTopK(TOP_K) |
| 75 | + .withSimilarityThreshold(SIMILARITY_THRESHOLD) |
| 76 | + .withFilterExpression(b.and( |
| 77 | + b.in("john", "jill"), |
| 78 | + b.eq("article_type", "blog")).build())); |
| 79 | +---- |
| 80 | + |
| 81 | +NOTE: Those (portable) filter expressions get automatically converted into the proprietary MongoDB Atlas filter expressions. |
| 82 | + |
| 83 | +== MongoDB Atlas properties |
| 84 | + |
| 85 | +You can use the following properties in your Spring Boot configuration to customize the MongoDB Atlas vector store. |
| 86 | + |
| 87 | +|=== |
| 88 | +|Property| Description | Default value |
| 89 | + |
| 90 | +|`spring.ai.vectorstore.mongodb.collection-name`| The name of the collection to store the vectors. | `vector_store` |
| 91 | +|`spring.ai.vectorstore.mongodb.path-name`| The name of the path to store the vectors. | `embedding` |
| 92 | +|`spring.ai.vectorstore.mongodb.indexName`| The name of the index to store the vectors. | `vector_index` |
| 93 | +|=== |
0 commit comments