跳到主要内容

Vector Store

VectorStore in the spring-ai-vector-store module is the interface to manage and query documents in a vector database.

VectorStore
public interface VectorStore extends DocumentWriter {
void add(List<Document> documents);

void delete(List<String> idList);

void delete(Filter.Expression filterExpression);

default void delete(String filterExpression) {}

List<Document> similaritySearch(SearchRequest request);
}

Create VectorStore

VectorStores are created using builders. Different VectorStore implementations have their own builder implementations. VectorStore.Builder defines common methods for VectorStore builders.

Property typeMethodDescription
ObservationRegistryobservationRegistryRegistry for observation support
VectorStoreObservationConventioncustomObservationConventionCustom convention for creating observations
BatchingStrategybatchingStrategyBatching strategy to embed documents

Add Documents

Documents are added using the add method.

Add documents
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.

Delete documents
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 typeMethodDescriptionDefault value
StringqueryText to use for embedding similarity comparison""
inttopKtop k similar results to return4
doublesimilarityThresholdSimilarity threshold score to filter the search response by0.0
Filter.ExpressionexpressionExpression to filter documentsnull
StringtextExpressionExpression to filter documents, parsed by FilterExpressionTextParsernull

The code below shows how to search documents.

Search documents
vectorStore.similaritySearch(
SearchRequest.builder()
.query("Build agents")
.topK(1)
.build());