|
| 1 | += MariaDB Vector |
| 2 | + |
| 3 | +This section walks you through setting up the MariaDB `VectorStore` to store document embeddings and perform similarity searches. |
| 4 | + |
| 5 | +link:https://mariadb.org/projects/mariadb-vector/[MariaDB vector] is part of MariaDB 11.7 and enables storing and searching over machine learning-generated embeddings. |
| 6 | + |
| 7 | +== Auto-Configuration |
| 8 | + |
| 9 | +Add the MariaDBVectorStore boot starter dependency to your project: |
| 10 | + |
| 11 | +[source,xml] |
| 12 | +---- |
| 13 | +<dependency> |
| 14 | + <groupId>org.springframework.ai</groupId> |
| 15 | + <artifactId>spring-ai-mariadb-store-spring-boot-starter</artifactId> |
| 16 | +</dependency> |
| 17 | +---- |
| 18 | + |
| 19 | +or to your Gradle `build.gradle` build file. |
| 20 | + |
| 21 | +[source,groovy] |
| 22 | +---- |
| 23 | +dependencies { |
| 24 | + implementation 'org.springframework.ai:spring-ai-mariadb-store-spring-boot-starter' |
| 25 | +} |
| 26 | +---- |
| 27 | + |
| 28 | +The vector store implementation can initialize the required schema for you, but you must opt-in by specifying the `initializeSchema` boolean in the appropriate constructor or by setting `...initialize-schema=true` in the `application.properties` file. |
| 29 | + |
| 30 | +The Vector Store also requires an `EmbeddingModel` instance to calculate embeddings for the documents. |
| 31 | +You can pick one of the available xref:api/embeddings.adoc#available-implementations[EmbeddingModel Implementations]. |
| 32 | + |
| 33 | +For example, to use the xref:api/embeddings/openai-embeddings.adoc[OpenAI EmbeddingModel], add the following dependency to your project: |
| 34 | + |
| 35 | +[source,xml] |
| 36 | +---- |
| 37 | +<dependency> |
| 38 | + <groupId>org.springframework.ai</groupId> |
| 39 | + <artifactId>spring-ai-openai-spring-boot-starter</artifactId> |
| 40 | +</dependency> |
| 41 | +---- |
| 42 | + |
| 43 | +or to your Gradle `build.gradle` build file. |
| 44 | + |
| 45 | +[source,groovy] |
| 46 | +---- |
| 47 | +dependencies { |
| 48 | + implementation 'org.springframework.ai:spring-ai-openai-spring-boot-starter' |
| 49 | +} |
| 50 | +---- |
| 51 | + |
| 52 | +TIP: Refer to the xref:getting-started.adoc#dependency-management[Dependency Management] section to add the Spring AI BOM to your build file. |
| 53 | +Refer to the xref:getting-started.adoc#repositories[Repositories] section to add Milestone and/or Snapshot Repositories to your build file. |
| 54 | + |
| 55 | +To connect to and configure the `MariaDBVectorStore`, you need to provide access details for your instance. |
| 56 | +A simple configuration can be provided via Spring Boot's `application.yml`. |
| 57 | + |
| 58 | +[yml] |
| 59 | +---- |
| 60 | +spring: |
| 61 | + datasource: |
| 62 | + url: jdbc:mariadb://localhost/db |
| 63 | + username: myUser |
| 64 | + password: myPassword |
| 65 | + ai: |
| 66 | + vectorstore: |
| 67 | + mariadbvector: |
| 68 | + distance-type: COSINE |
| 69 | + dimensions: 1536 |
| 70 | +---- |
| 71 | + |
| 72 | +TIP: If you run MariaDBvector as a Spring Boot dev service via link:https://docs.spring.io/spring-boot/reference/features/dev-services.html#features.dev-services.docker-compose[Docker Compose] |
| 73 | +or link:https://docs.spring.io/spring-boot/reference/features/dev-services.html#features.dev-services.testcontainers[Testcontainers], |
| 74 | +you don't need to configure URL, username and password since they are autoconfigured by Spring Boot. |
| 75 | + |
| 76 | +TIP: Check the list of xref:#mariadbvector-properties[configuration parameters] to learn about the default values and configuration options. |
| 77 | + |
| 78 | +Now you can auto-wire the `MariaDBVectorStore` in your application and use it |
| 79 | + |
| 80 | +[source,java] |
| 81 | +---- |
| 82 | +@Autowired VectorStore vectorStore; |
| 83 | +
|
| 84 | +// ... |
| 85 | +
|
| 86 | +List<Document> documents = List.of( |
| 87 | + new Document("Spring AI rocks!! Spring AI rocks!! Spring AI rocks!! Spring AI rocks!! Spring AI rocks!!", Map.of("meta1", "meta1")), |
| 88 | + new Document("The World is Big and Salvation Lurks Around the Corner"), |
| 89 | + new Document("You walk forward facing the past and you turn back toward the future.", Map.of("meta2", "meta2"))); |
| 90 | +
|
| 91 | +// Add the documents to PGVector |
| 92 | +vectorStore.add(documents); |
| 93 | +
|
| 94 | +// Retrieve documents similar to a query |
| 95 | +List<Document> results = this.vectorStore.similaritySearch(SearchRequest.query("Spring").withTopK(5)); |
| 96 | +---- |
| 97 | + |
| 98 | +[[mariadbvector-properties]] |
| 99 | +=== Configuration properties |
| 100 | + |
| 101 | +You can use the following properties in your Spring Boot configuration to customize the MariaDB vector store. |
| 102 | + |
| 103 | +[cols="2,5,1",stripes=even] |
| 104 | +|=== |
| 105 | +|Property| Description | Default value |
| 106 | + |
| 107 | +|`spring.ai.vectorstore.mariadb.distance-type`| Search distance type. Defaults to `COSINE`. But if vectors are normalized to length 1, you can use `EUCLIDEAN` for best performance.| COSINE |
| 108 | +|`spring.ai.vectorstore.mariadb.dimensions`| Embeddings dimension. If not specified explicitly the PgVectorStore will retrieve the dimensions form the provided `EmbeddingModel`. Dimensions are set to the embedding column the on table creation. If you change the dimensions your would have to re-create the vector_store table as well. | - |
| 109 | +|`spring.ai.vectorstore.mariadb.remove-existing-vector-store-table` | Deletes the existing `vector_store` table on start up. | false |
| 110 | +|`spring.ai.vectorstore.mariadb.initialize-schema` | Whether to initialize the required schema | false |
| 111 | +|`spring.ai.vectorstore.mariadb.schema-name` | Vector store schema name | null |
| 112 | +|`spring.ai.vectorstore.mariadb.table-name` | Vector store table name | `vector_store` |
| 113 | +|`spring.ai.vectorstore.mariadb.schema-validation` | Enables schema and table name validation to ensure they are valid and existing objects. | false |
| 114 | + |
| 115 | +|=== |
| 116 | + |
| 117 | +TIP: If you configure a custom schema and/or table name, consider enabling schema validation by setting `spring.ai.vectorstore.mariadb.schema-validation=true`. |
| 118 | +This ensures the correctness of the names and reduces the risk of SQL injection attacks. |
| 119 | + |
| 120 | +== Metadata filtering |
| 121 | + |
| 122 | +You can leverage the generic, portable link:https://docs.spring.io/spring-ai/reference/api/vectordbs.html#_metadata_filters[metadata filters] with the MariaDB Vector store. |
| 123 | + |
| 124 | +For example, you can use either the text expression language: |
| 125 | + |
| 126 | +[source,java] |
| 127 | +---- |
| 128 | +vectorStore.similaritySearch( |
| 129 | + SearchRequest.defaults() |
| 130 | + .withQuery("The World") |
| 131 | + .withTopK(TOP_K) |
| 132 | + .withSimilarityThreshold(SIMILARITY_THRESHOLD) |
| 133 | + .withFilterExpression("author in ['john', 'jill'] && article_type == 'blog'")); |
| 134 | +---- |
| 135 | + |
| 136 | +or programmatically using the `Filter.Expression` DSL: |
| 137 | + |
| 138 | +[source,java] |
| 139 | +---- |
| 140 | +FilterExpressionBuilder b = new FilterExpressionBuilder(); |
| 141 | +
|
| 142 | +vectorStore.similaritySearch(SearchRequest.defaults() |
| 143 | + .withQuery("The World") |
| 144 | + .withTopK(TOP_K) |
| 145 | + .withSimilarityThreshold(SIMILARITY_THRESHOLD) |
| 146 | + .withFilterExpression(b.and( |
| 147 | + b.in("author","john", "jill"), |
| 148 | + b.eq("article_type", "blog")).build())); |
| 149 | +---- |
| 150 | + |
| 151 | +NOTE: These filter expressions are converted into the equivalent PgVector filters. |
| 152 | + |
| 153 | +== Manual Configuration |
| 154 | + |
| 155 | +Instead of using the Spring Boot auto-configuration, you can manually configure the `MariaDBVectorStore`. |
| 156 | +For this you need to add the MariaDB connector and `JdbcTemplate` auto-configuration dependencies to your project: |
| 157 | + |
| 158 | +[source,xml] |
| 159 | +---- |
| 160 | +<dependency> |
| 161 | + <groupId>org.springframework.boot</groupId> |
| 162 | + <artifactId>spring-boot-starter-jdbc</artifactId> |
| 163 | +</dependency> |
| 164 | +
|
| 165 | +<dependency> |
| 166 | + <groupId>org.mariadb.jdbc</groupId> |
| 167 | + <artifactId>mariadb-java-client</artifactId> |
| 168 | + <scope>runtime</scope> |
| 169 | +</dependency> |
| 170 | +
|
| 171 | +<dependency> |
| 172 | + <groupId>org.springframework.ai</groupId> |
| 173 | + <artifactId>spring-ai-mariadb-store</artifactId> |
| 174 | +</dependency> |
| 175 | +---- |
| 176 | + |
| 177 | +TIP: Refer to the xref:getting-started.adoc#dependency-management[Dependency Management] section to add the Spring AI BOM to your build file. |
| 178 | + |
| 179 | +To configure MariaDB Vector in your application, you can use the following setup: |
| 180 | + |
| 181 | +[source,java] |
| 182 | +---- |
| 183 | +@Bean |
| 184 | +public VectorStore vectorStore(JdbcTemplate jdbcTemplate, EmbeddingModel embeddingModel) { |
| 185 | + return new MariaDBVectorStore(jdbcTemplate, embeddingModel); |
| 186 | +} |
| 187 | +---- |
0 commit comments