Class MariaDBVectorStore

java.lang.Object
org.springframework.ai.vectorstore.observation.AbstractObservationVectorStore
org.springframework.ai.vectorstore.mariadb.MariaDBVectorStore
All Implemented Interfaces:
Consumer<List<Document>>, DocumentWriter, VectorStore, org.springframework.beans.factory.InitializingBean

public class MariaDBVectorStore extends AbstractObservationVectorStore implements org.springframework.beans.factory.InitializingBean
MariaDB-based vector store implementation using MariaDB's vector search capabilities.

The store uses MariaDB's vector search functionality to persist and query vector embeddings along with their associated document content and metadata. The implementation leverages MariaDB's vector index for efficient k-NN search operations.

Features:

  • Automatic schema initialization with configurable index creation
  • Support for multiple distance functions: Cosine and Euclidean
  • Metadata filtering using JSON path expressions
  • Configurable similarity thresholds for search results
  • Batch processing support with configurable strategies
  • Observation and metrics support through Micrometer

Basic usage example:


 MariaDBVectorStore vectorStore = MariaDBVectorStore.builder(jdbcTemplate, embeddingModel)
     .initializeSchema(true)
     .build();

 // Add documents
 vectorStore.add(List.of(
     new Document("content1", Map.of("key1", "value1")),
     new Document("content2", Map.of("key2", "value2"))
 ));

 // Search with filters
 List<Document> results = vectorStore.similaritySearch(
     SearchRequest.query("search text")
         .withTopK(5)
         .withSimilarityThreshold(0.7)
         .withFilterExpression("key1 == 'value1'")
 );
 

Advanced configuration example:


 MariaDBVectorStore vectorStore = MariaDBVectorStore.builder(jdbcTemplate, embeddingModel)
     .schemaName("mydb")
     .distanceType(MariaDBDistanceType.COSINE)
     .dimensions(1536)
     .vectorTableName("custom_vectors")
     .contentFieldName("text")
     .embeddingFieldName("embedding")
     .idFieldName("doc_id")
     .metadataFieldName("meta")
     .initializeSchema(true)
     .batchingStrategy(new TokenCountBatchingStrategy())
     .build();
 

Requirements:

  • MariaDB 11.3.0 or later
  • Table schema with id (UUID), text (TEXT), metadata (JSON), and embedding (VECTOR) properties

Distance Functions:

  • cosine: Default, suitable for most use cases. Measures cosine similarity between vectors.
  • euclidean: Euclidean distance between vectors. Lower values indicate higher similarity.
Since:
1.0.0
Author:
Diego Dupin, Ilayaperumal Gopinathan, Soby Chacko