AI EngineeringBuild your first rag
Vector Store
What is Simple Vector Store
- A Simple Vector Store is an in-memory implementation of a vector storage mechanism used in Spring AI.
- It stores documents along with their vector embeddings, which are numerical representations generated by an EmbeddingModel.
- It supports similarity search, so the system can retrieve documents that are semantically similar to a given text query.
- In this setup, SimpleVectorStore is created using a builder pattern, which takes the EmbeddingModel and constructs a VectorStore instance.
- This VectorStore is exposed as a Spring bean, so it can be injected into components like data initialization classes and REST controllers.
Why Simple Vector Store is Needed
- It provides a quick and lightweight way to try out vector-based search without any external database or infrastructure.
- Being in-memory, it is ideal for development, demos, small applications, and learning.
- It helps developers understand core Spring AI concepts such as embeddings, vector stores, and similarity search before moving to a persistent vector database.
- It works smoothly with Spring’s dependency injection, allowing easy sharing of the same VectorStore instance across multiple components.
- For small datasets, such as a simple product catalog, it gives fast retrieval and minimal setup.
How to Implement Simple Vector Store – Step-by-Step Guide
- Configuration of the VectorStore bean
- Create a Spring configuration class (e.g., AppConfig).
- Define a method that returns a VectorStore and annotate it with @Bean.
- Inside this method, use SimpleVectorStore.builder(embeddingModel).build() to create the in-memory store.
- This makes the VectorStore available throughout the application by dependency injection.
- Data Initialization
- Create a Spring component (e.g., DataInit) to load initial data.
- Inject the VectorStore into this component using @Autowired.
- Use a lifecycle method annotated with @PostConstruct to run after bean creation.
- Use TextReader with ClassPathResource("product_details.txt") to read a text file from the classpath.
- Use TokenTextSplitter to split the raw text into multiple Document objects.
- Add the resulting list of documents to the vectorStore using vectorStore.add(documents).
- REST API for Similarity Search
- Create a REST controller (e.g., AiController) annotated with @RestController.
- Inject the same VectorStore bean into this controller.
- Define a GET endpoint (e.g., /api/products) that accepts a query string as @RequestParam String text.
- Inside the method, call vectorStore.similaritySearch(text) and return the matching documents.
- This exposes semantic product search over HTTP.
- Product Data Source
- product_details.txt in the classpath contains simple e-commerce product data such as electronics, utensils, and daily needs items.
- This file acts as the knowledge base for the vector store; once loaded, its content becomes searchable via similarity search.
Simple Vector Store Maven Dependency
- The Simple Vector Store requires a Spring AI Maven dependency that provides:
- The VectorStore interface.
- The SimpleVectorStore implementation.
- Supporting utilities like EmbeddingModel, TextReader, TokenTextSplitter, and Document.
- This dependency must be added in the project’s Maven configuration so the Simple Vector Store features are available at compile time and runtime.
- After adding the dependency, the application can:
- Build an in-memory vector store.
- Generate embeddings for documents.
- Perform semantic similarity searches on loaded documents.
Simple Vector Store Implementation
// AppConfig.java
@Configuration
public class AppConfig {
@Bean
public VectorStore getVectorStore(EmbeddingModel embeddingModel) {
return SimpleVectorStore
.builder(embeddingModel)
.build();
}
}
// DataInit.java
@Component
public class DataInit {
@Autowired
private VectorStore vectorStore;
@PostConstruct
public void initData() {
TextReader textReader = new TextReader(new ClassPathResource("product_details.txt"));
TokenTextSplitter splitter = new TokenTextSplitter();
List<Document> documents = splitter.split(textReader.read());
vectorStore.add(documents);
}
}
// AiController.java
@RestController
public class AiController {
@Autowired
private VectorStore vectorStore;
@GetMapping("/api/products")
public List<Document> getProducts(@RequestParam String text) {
return vectorStore.similaritySearch(text);
}
}About MariaDB Vector Store
What is MariaDB Vector Store
- MariaDB Vector Store is a vector storage implementation that uses MariaDB as the underlying database instead of in-memory storage.
- It stores documents, metadata, and vector embeddings in a relational table in MariaDB.
- It is integrated with Spring AI and built using Spring’s JdbcTemplate and an EmbeddingModel.
- It allows configuration of:
- The table name for vector data (e.g., "vector_store").
- The embedding dimension (e.g., 1536).
- The distance type for similarity search (e.g., COSINE).
- Whether the schema is auto-initialized when the application starts.
Why We Need MariaDB Vector Store
- It provides persistent storage, so vectors and documents survive application restarts.
- It is suitable for production and larger datasets, where in-memory storage is not enough.
- It uses MariaDB features like indexes, transactions, and scalability.
- It supports efficient similarity search using a vector column and HNSW-based index defined in the schema.
- It integrates well with existing relational database infrastructure, simplifying deployment and operations.
Installation of MariaDB for Windows
- Download MariaDB Community Server
- Go to the official MariaDB Downloads page.
- Choose MariaDB Community Server and select MS Windows (64-bit x86) as the OS.
- Download the .msi installer for the desired MariaDB version.
- Run the MSI Installer
- Double-click the downloaded .msi file to start the MariaDB MSI installer.
- Follow the wizard screens in Typical / Custom installation mode.
- Configure Server Options in the Wizard
- Set the root password (and optionally enable root access from remote hosts).
- Confirm the TCP port (default 3306) that MariaDB will listen on.
- Choose whether to install MariaDB as a Windows service, and define the service name if needed.
- Optionally, configure automatic start of the MariaDB service at system boot.
- Complete the Installation
- Finish the wizard and let it install the necessary binaries, data directory, and service configuration.
- Ensure the MariaDB service is running via Services (services.msc) or the installer finish screen.
- Verify the Installation
- Use the MariaDB client (mysql) or a GUI tool to connect:
- Host: localhost
- Port: 3306
- User: root
- Password: the one you configured in the wizard.
- Run a simple query like SHOW DATABASES; to verify the server is working.
- Use the MariaDB client (mysql) or a GUI tool to connect:
Installation of MariaDB for Mac (macOS)
- Install Xcode Command Line Tools (if not already installed)
- Open Terminal and run:
- xcode-select --install
- This install the command-line tools needed by Homebrew and many build tools.
- Open Terminal and run:
- Install Homebrew (if not installed)
- In Terminal, run the official Homebrew install script from brew.sh (shown in MariaDB’s Homebrew guide):
- /bin/bash-c"$(curl-fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh\)"
- In Terminal, run the official Homebrew install script from brew.sh (shown in MariaDB’s Homebrew guide):
- Update Homebrew
- Run:
- brew update
- Run:
- Install MariaDB via Homebrew
- In Terminal, run:
- brew install mariadb
- This installs MariaDB Server and client programs as a Homebrew “bottle” (precompiled package).
- In Terminal, run:
- Start MariaDB Server
- To start manually for the current session:
- mysql.server start
- To start automatically on login (recommended):
- brew services start mariadb
- To start manually for the current session:
- Secure and Test the Installation
- (Optional) run:
- mysql_secure_installation
- Connect using:
- mysql -u root -p
- Run SHOW DATABASES; to confirm the server is running properly.
- (Optional) run:
Implementation of MariaDB Vector Store in Spring AI
- Add MariaDB Vector Store Maven Dependency
- Add a Maven dependency that provides the MariaDB-backed VectorStore (e.g., MariaDBVectorStore) for Spring AI.
- This dependency enables the application to construct a VectorStore that persists vectors in MariaDB.
- Add Spring Data JDBC / Spring JDBC Maven Dependency
- Include the Maven dependency for Spring JDBC / Spring Data JDBC.
- This provides classes like JdbcTemplate used by MariaDBVectorStore to interact with MariaDB.
- Configure Database Connection in application.properties
- Define the datasource properties so Spring Boot can connect to MariaDB:
- spring.datasource.url=jdbc:mariadb://localhost:3306/your_database_name
- spring.datasource.username= - database username.
- spring.datasource.password= - database password.
- Define the datasource properties so Spring Boot can connect to MariaDB:
- Define the MariaDB VectorStore Bean
- Create a Spring configuration class (e.g., AppConfig).
- Define a method that takes JdbcTemplate and EmbeddingModel and returns a VectorStore.
- Use the MariaDBVectorStore builder to specify:
- Table name (e.g., "vector_store").
- Dimensions (e.g., 1536).
- Distance type (e.g., COSINE).
- Schema initialization flag (to create table/index automatically).
- Expose this method as a @Bean, so the VectorStore can be injected into other components.
- Database Schema Initialization (schema.sql)
- Place a schema.sql file under the appropriate resources/init folder (or as per Spring Boot conventions).
- Define the table and index for storing embeddings, for example:
- A table with:
- ID - primary key.
- CONTENT - document text.
- METADATA - JSON metadata.
- EMBEDDING - vector column with the specified dimension.
- An HNSW index on the embedding column for fast similarity search.
- A table with:
- End-to-End Flow with MariaDB Vector Store
- The Spring application connects to MariaDB using the configured datasource.
- Documents and their embeddings are stored in the VECTOR_STORE table.
- MariaDBVectorStore uses the chosen distance metric (e.g., COSINE) and HNSW index to run similarity search queries.
- Other Spring components (data loaders, controllers) use this VectorStore similar to the Simple Vector Store, but now with persistent, database-backed storage.
Implementation Code for MariaDB Vector Store & Schema
- Java Configuration
// AppConfig.java
@Configuration
public class AppConfig {
@Bean
public VectorStore getVectorStore(JdbcTemplate jdbcTemplate,
EmbeddingModel embeddingModel) {
return MariaDBVectorStore
.builder(jdbcTemplate, embeddingModel)
.vectorTableName("vector_store")
.dimensions(1536) // embedding dimensions
.distanceType(MariaDBVectorStore.MariaDBDistanceType.COSINE)
.initializeSchema(true)
.build();
}
}- Sql Script
-- Schema.sql in /resources/init/
CREATE TABLE IF NOT EXISTS VECTOR_STORE (
ID TEXT PRIMARY KEY,
CONTENT TEXT,
METADATA JSON,
EMBEDDING VECTOR(1536)
);
CREATE INDEX IF NOT EXISTS VECTOR_STORE_EMBEDDING_IDX
ON VECTOR_STORE USING HNSW (
embedding_column_name vector_cosine_ops
);- Application Properties
# Datasource Properties in application.properties
spring.datasource.url=jdbc_url
spring.datasource.username=your_db_username
spring.datasource.password=your_db_passwordLast updated on
