VectorStore
VectorStore in the spring-ai-vector-store module is the interface to manage and query documents in a vector database.
VectorStore extends from VectorStoreRetriever. VectorStoreRetriever provides read-only access to a vector store.
public interface VectorStoreRetriever {
List<Document> similaritySearch(SearchRequest request);
default List<Document> similaritySearch(String query) {
return this.similaritySearch(SearchRequest.builder().query(query).build());
}
}
VectorStore adds methods to manage documents in a vector store.
public interface VectorStore extends DocumentWriter, VectorStoreRetriever {
void add(List<Document> documents);
void delete(List<String> idList);
void delete(Filter.Expression filterExpression);
default void delete(String filterExpression) {}
}
Create VectorStore
VectorStores are created using builders. Different VectorStore implementations have their own builder implementations. VectorStore.Builder defines common methods for VectorStore builders.
| Property type | Method | Description |
|---|---|---|
ObservationRegistry | observationRegistry | Registry for observation support |
VectorStoreObservationConvention | customObservationConvention | Custom convention for creating observations |
BatchingStrategy | batchingStrategy | Batching strategy to embed documents |
Add Documents
Documents are added using the add method.
vectorStore.add(docs);
Delete Documents
Documents are deleted using the delete methods. When deleting documents, we can provide a list of IDs of documents, or a filter expression to select documents. The filter expression can be specified using Filter.Expression or String.
vectorStore.delete("topic == 'AI'");
Search Documents
Documents are searched using the similaritySearch method. Search requests are represented using SearchRequest. SearchRequests are created using builders.
The table below shows methods of SearchRequest builder.
| Property type | Method | Description | Default value |
|---|---|---|---|
String | query | Text to use for embedding similarity comparison | "" |
int | topK | top k similar results to return | 4 |
double | similarityThreshold | Similarity threshold score to filter the search response by | 0.0 |
Filter.Expression | expression | Expression to filter documents | null |
String | textExpression | Expression to filter documents, parsed by FilterExpressionTextParser | null |
The code below shows how to search documents.
vectorStore.similaritySearch(
SearchRequest.builder()
.query("Build agents")
.topK(1)
.build());