Skip to main content

Spring AI VectorStore API

The Spring AI VectorStore API is the cornerstone of the framework's retrieval capabilities. It provides a consistent, vendor-neutral programming model for interacting with vector databases—the engines that power semantic search, similarity retrieval, and ultimately Retrieval-Augmented Generation (RAG). By abstracting away the specifics of each database, Spring AI allows you to write retrieval logic once and seamlessly switch between PostgreSQL with PGVector, Milvus, Pinecone, Redis, and others without touching your application code.

This guide explores the design, architecture, and practical usage of the VectorStore interface. You will learn how to ingest documents, perform similarity searches with metadata filtering, optimize for production, and integrate the vector store into a full RAG pipeline. Whether you are building your first AI-powered search feature or designing an enterprise knowledge platform, mastering the VectorStore API is essential.

Why VectorStore Exists

Before a unified abstraction, integrating a vector database meant learning each vendor’s proprietary client library, data model, and query syntax. A switch from Pinecone to PGVector required rewriting ingestion and retrieval logic, retesting, and often introducing subtle bugs due to different behavior in similarity scoring or filtering.

The VectorStore API solves these problems by:

  • Unifying the programming model: a single set of interfaces for CRUD operations and similarity search.
  • Enabling vendor independence: switch databases by changing a starter dependency and configuration, not code.
  • Standardizing metadata handling: filter documents using a common expression language regardless of the backend.
  • Integrating with the wider Spring ecosystem: auto-configuration, Micrometer metrics, and dependency injection work out of the box.

Under the hood, each supported database provides a VectorStore implementation that adapts its native capabilities to the Spring AI contract. This lets you focus on building retrieval logic while retaining the freedom to choose the best storage for your workload, whether it's a self-hosted PostgreSQL extension for small teams or a fully managed Pinecone index for global scale.

VectorStore Architecture

The VectorStore sits at the heart of the retrieval layer, bridging the gap between embedding models and the large language model.

  • Application: your Spring Boot service that orchestrates the RAG pipeline.
  • EmbeddingModel: converts text queries and documents into dense vector embeddings.
  • VectorStore API: the uniform interface through which the application interacts with the database.
  • Vector Database: the concrete store (PGVector, Milvus, etc.) that persists embeddings and performs similarity searches.
  • Retriever / Advisor: Spring AI components (like QuestionAnswerAdvisor) that query the VectorStore and inject the retrieved documents into the prompt for the LLM.

This layered architecture ensures that the choice of embedding model, vector database, and retrieval strategy are independent concerns, each replaceable without affecting the others.

Core Interfaces

The VectorStore API is built around a few key interfaces and classes. Understanding them is crucial for effective usage.

Interface / ClassResponsibility
VectorStorePrimary interface for adding, deleting, and searching documents.
DocumentA text chunk along with its metadata and optional embedding.
SearchRequestEncapsulates a similarity search query: the text, top‑K, similarity threshold, and optional filter expression.
Filter.ExpressionA portable, structured filter expression for metadata.
EmbeddingModelThe model that generates embeddings from text; used internally by many VectorStore implementations.

VectorStore defines methods such as:

  • void add(List<Document> documents)
  • void delete(String id)
  • List<Document> similaritySearch(SearchRequest request)
  • List<Document> similaritySearch(String query) (convenience)

Document is the core data carrier. It holds:

  • content: the text of the chunk.
  • metadata: a Map<String, Object> for structured attributes like source, date, author.
  • embedding: the float array vector (may be populated before storage or generated lazily).

SearchRequest allows fine‑grained control:

  • query: the search text.
  • topK: number of results to return.
  • similarityThreshold: minimum similarity score (0.0 to 1.0).
  • filterExpression: a Filter.Expression for metadata‑based filtering.

This unified model means that a single search request can be executed against any supported store, with the adapter handling the translation.

Adding Documents

Before you can search, you need to populate the vector store. The add() method accepts a list of Document objects. Here is a typical ingestion flow:

// 1. Load and chunk documents (using DocumentReader and TextSplitter)
List<Document> chunks = documentSplitter.apply(rawDocuments);

// 2. (Optional) Enrich metadata
chunks.forEach(chunk -> {
chunk.getMetadata().put("source", "employee-handbook.pdf");
chunk.getMetadata().put("department", "HR");
chunk.getMetadata().put("version", "2026");
});

// 3. Add to vector store – embeddings are generated automatically
vectorStore.add(chunks);

When add() is called, the VectorStore implementation typically:

  • Uses the configured EmbeddingModel to generate an embedding for each document’s content.
  • Persists the content, metadata, and vector in the database.
  • Optimizes the write via batching if the backend supports it.

You can also pre‑compute embeddings if you need to control the embedding process:

document.setEmbedding(embeddingModel.embed(document.getContent()));
vectorStore.add(List.of(document));

This gives you full control over embedding caching, model selection, and error handling.

The primary purpose of a vector store is to retrieve documents that are semantically similar to a query. The similaritySearch methods do exactly that.

Simple search:

List<Document> results = vectorStore.similaritySearch(
SearchRequest.builder()
.query("What is the vacation policy for remote employees?")
.topK(3)
.build()
);

This embeds the query text, performs a similarity search, and returns the top 3 most similar documents.

Advanced search with filters and threshold:

List<Document> results = vectorStore.similaritySearch(
SearchRequest.builder()
.query("vacation policy remote employees")
.topK(5)
.similarityThreshold(0.75)
.filterExpression("department == 'HR' && year >= 2026")
.build()
);

The filter expression uses Spring AI’s portable filter language and is translated into the native query of the underlying database. The similarity threshold discards documents that are too far from the query, improving precision.

The returned Document objects contain the text content, metadata, and (if requested) the similarity score. This data can be used directly to augment a prompt for the LLM.

Metadata Filtering

Metadata is what turns a basic similarity search into a powerful, targeted retrieval system. Each document can carry arbitrary key‑value pairs, and the Filter.Expression syntax enables rich query conditions.

Common metadata fields:

  • source: file name or URL
  • category: article, policy, manual
  • tenantId: for multi‑tenancy
  • date: publication date
  • accessLevel: public, internal, confidential

Filter expression examples:

  • "department == 'Engineering' && language == 'en'"
  • "year >= 2025 && year <= 2026"
  • "topic in ['Spring AI', 'RAG', 'Vector Search']"
  • "tenantId == 'acme-corp'"

Filter expressions are evaluated by the database during the similarity search, which reduces the candidate set and improves both relevance and query performance. Because the expression language is part of the VectorStore API, your filter logic remains portable across different database backends.

Supported Vector Databases

Spring AI provides out‑of‑the‑box VectorStore implementations for a wide range of databases, each with distinct strengths.

DatabaseSelf-hostedManaged CloudFilteringScalabilityBest For
PGVectorYesYes (e.g., Supabase)Full SQLModerate (single-node PG)Teams already on PostgreSQL
MilvusYesYes (Zilliz Cloud)AdvancedMassive (distributed)Large‑scale, high‑throughput
PineconeNoFully managedNativeServerless, automaticZero‑ops, fast time‑to‑market
Redis VectorYesYes (Redis Cloud)LimitedIn‑memory, very low latencyCaching, real‑time
WeaviateYesYes (Weaviate Cloud)RichDistributedHybrid search, vectorizer modules
QdrantYesYes (Qdrant Cloud)ExcellentHorizontalPerformance‑sensitive, filtering
OpenSearchYesYes (AWS, etc.)Full query DSLClusterElasticsearch users, hybrid search

All implementations adhere to the same VectorStore interface, so you can start with a simple PGVector setup for development and move to Milvus or Pinecone in production with minimal changes.

VectorStore in a RAG Pipeline

The VectorStore is the retrieval backbone of a RAG system. Here’s the complete workflow:

  1. DocumentChunking: raw documents are split into smaller, manageable pieces.
  2. ChunkingEmbedding Generation: each chunk is converted into a dense vector using EmbeddingModel.
  3. Embedding GenerationVectorStore: vectors, text, and metadata are stored.
  4. VectorStoreRetrieval: a user query is embedded and the similaritySearch method fetches the most relevant chunks.
  5. RetrievalPrompt Augmentation: the retrieved chunks are inserted into the prompt as context.
  6. Prompt AugmentationLLM: the model generates a grounded answer.

Spring AI advisors (like QuestionAnswerAdvisor) automate steps 4‑6, so the application code only needs to configure the advisor with a VectorStore instance.

Performance Optimization

For production workloads, raw similarity search is not enough. Consider the following optimizations:

  • Batch ingestion: use add(List<Document>) rather than single‑document inserts. Most stores support efficient bulk writes.
  • Index tuning: leverage database‑specific index types (HNSW, IVFFlat) and tune parameters like efConstruction, efSearch, and M. Spring AI exposes these via configuration properties.
  • Embedding caching: cache embeddings for stable content to avoid redundant API calls. Use a key‑value store keyed by a content hash.
  • Metadata design: store only necessary metadata; indexed fields should be kept minimal to reduce index size and improve filter performance.
  • Pagination: for queries that might return large result sets, use paginated search to avoid overwhelming the LLM context window.
  • Asynchronous processing: offload ingestion and embedding generation to background workers using queues, ensuring that search latency remains unaffected.

Monitoring retrieval latency, recall, and token usage is essential for maintaining performance at scale.

Production Best Practices

  • Schema design: plan your metadata schema before ingestion. Changing it later may require re‑indexing.
  • Namespace strategy: use prefixes or dedicated collections/indices to isolate different types of content (e.g., docs.legal, docs.engineering).
  • Tenant isolation: for multi‑tenant applications, include a tenantId field and enforce it in every filter expression.
  • Versioning: attach a version timestamp to documents so that stale data can be identified and purged.
  • Backup: regularly back up your vector database, especially if it contains critical business data.
  • Observability: expose metrics via Micrometer (ingestion rate, search latency, error rate) and set up dashboards and alerts.
  • Retries: implement exponential backoff and circuit breakers for vector store calls, as transient network issues can occur.

Following these practices ensures that your vector store remains reliable, secure, and manageable as your AI workload grows.

Common Pitfalls

  • Embedding mismatch: using a different EmbeddingModel for ingestion and query leads to incompatible vector spaces and poor retrieval. Always use the same model.
  • Poor chunking: chunks that are too large or too small reduce retrieval precision. Experiment and evaluate with real queries.
  • Too much metadata: storing excessive metadata bloats the index and slows filtering. Store only what is needed for retrieval and filtering.
  • Missing filters: queries without metadata filters can return irrelevant results, especially in multi‑tenant or multi‑department setups.
  • Large documents: documents that exceed the embedding model’s token limit are truncated, losing context. Always split them properly.
  • Duplicate vectors: ingesting the same document multiple times without deduplication pollutes the index and wastes storage.

Being aware of these issues and designing around them from the start will save significant rework later.

Best Practices Checklist

  • Document design: pre‑process and clean text before chunking.
  • Chunking: choose chunk size and overlap based on document type and retrieval requirements.
  • Metadata: define a consistent schema; always include source, date, and access‑control fields.
  • Embeddings: use a high‑quality embedding model appropriate for your language and domain; cache when possible.
  • Retrieval: always set topK and similarityThreshold to control context size and quality.
  • Filtering: leverage metadata filters to narrow the search space.
  • Monitoring: instrument the pipeline with metrics and logs.
  • Testing: write integration tests against an in‑memory vector store or a test‑containerized database.
  • Scalability: design for horizontal scaling if you expect billions of vectors; choose a database that supports it.

Conclusion

The VectorStore API is the foundation of Spring AI’s retrieval capabilities. It abstracts away the complexity of different vector databases, providing a clean, consistent interface for adding documents and performing similarity searches. Whether you are building a simple knowledge base or a large‑scale RAG platform, mastering this API is essential.

Its strength lies in portability: your retrieval logic remains unchanged as you migrate from one database to another, and it integrates seamlessly with the embedding model, advisors, and chat components. Combined with careful chunking, metadata design, and production tuning, the VectorStore gives you everything you need to deliver accurate, context‑aware AI responses.

Continue deepening your knowledge by exploring the related guides below, which cover embedding models, chunking strategies, metadata filtering, and full RAG tutorials.