Class VectorStoreDocumentRetriever

java.lang.Object
org.springframework.ai.rag.retrieval.search.VectorStoreDocumentRetriever
All Implemented Interfaces:
Function<Query,List<org.springframework.ai.document.Document>>, DocumentRetriever

public final class VectorStoreDocumentRetriever extends Object implements DocumentRetriever
Retrieves documents from a vector store that are semantically similar to the input query. It supports filtering based on metadata, similarity threshold, and top-k results.

Example usage:


 VectorStoreDocumentRetriever retriever = VectorStoreDocumentRetriever.builder()
     .vectorStore(vectorStore)
     .similarityThreshold(0.73)
     .topK(5)
     .filterExpression(filterExpression)
     .build();
 List<Document> documents = retriever.retrieve(new Query("example query"));
 

The FILTER_EXPRESSION context key can be used to provide a filter expression for a specific query. This key accepts either a string representation of a filter expression or a Filter.Expression object directly.

Since:
1.0.0
Author:
Thomas Vitale
  • Field Details

  • Constructor Details

    • VectorStoreDocumentRetriever

      public VectorStoreDocumentRetriever(org.springframework.ai.vectorstore.VectorStore vectorStore, @Nullable Double similarityThreshold, @Nullable Integer topK, @Nullable Supplier<org.springframework.ai.vectorstore.filter.Filter.Expression> filterExpression)
  • Method Details

    • retrieve

      public List<org.springframework.ai.document.Document> retrieve(Query query)
      Description copied from interface: DocumentRetriever
      Retrieves relevant documents from an underlying data source based on the given query.
      Specified by:
      retrieve in interface DocumentRetriever
      Parameters:
      query - The query to use for retrieving documents
      Returns:
      The list of relevant documents
    • builder

      public static VectorStoreDocumentRetriever.Builder builder()