Class PgVectorStore

java.lang.Object
org.springframework.ai.vectorstore.PgVectorStore
All Implemented Interfaces:
VectorStore, org.springframework.context.Lifecycle, org.springframework.context.Phased, org.springframework.context.SmartLifecycle

public class PgVectorStore extends Object implements VectorStore, org.springframework.context.SmartLifecycle
Uses the "vector_store" table to store the Spring AI vector data. The table and the vector index will be auto-created if not available.
Author:
Christian Tzolov
  • Field Details

  • Constructor Details

    • PgVectorStore

      public PgVectorStore(org.springframework.jdbc.core.JdbcTemplate jdbcTemplate, EmbeddingClient embeddingClient)
    • PgVectorStore

      public PgVectorStore(org.springframework.jdbc.core.JdbcTemplate jdbcTemplate, EmbeddingClient embeddingClient, int dimensions)
    • PgVectorStore

      public PgVectorStore(org.springframework.jdbc.core.JdbcTemplate jdbcTemplate, EmbeddingClient embeddingClient, int dimensions, PgVectorStore.PgDistanceType distanceType, boolean removeExistingVectorStoreTable, PgVectorStore.PgIndexType createIndexMethod)
  • Method Details

    • getDistanceType

      public PgVectorStore.PgDistanceType getDistanceType()
    • add

      public void add(List<Document> documents)
      Description copied from interface: VectorStore
      Adds Documents to the vector store.
      Specified by:
      add in interface VectorStore
      Parameters:
      documents - the list of documents to store Will throw an exception if the underlying provider checks for duplicate IDs on add
    • delete

      public Optional<Boolean> delete(List<String> idList)
      Specified by:
      delete in interface VectorStore
    • similaritySearch

      public List<Document> similaritySearch(String query)
      Specified by:
      similaritySearch in interface VectorStore
    • similaritySearch

      public List<Document> similaritySearch(String query, int topK)
      Specified by:
      similaritySearch in interface VectorStore
    • similaritySearch

      public List<Document> similaritySearch(String query, int topK, double similarityThreshold)
      Specified by:
      similaritySearch in interface VectorStore
      Parameters:
      query - The query to send, it will be converted to an embedding based on the configuration of the vector store.
      topK - the top 'k' similar results
      similarityThreshold - the lower bound of the similarity score
      Returns:
      similar documents
    • embeddingDistance

      public List<Double> embeddingDistance(String query)
    • start

      public void start()
      Specified by:
      start in interface org.springframework.context.Lifecycle
    • stop

      public void stop()
      Specified by:
      stop in interface org.springframework.context.Lifecycle
    • isRunning

      public boolean isRunning()
      Specified by:
      isRunning in interface org.springframework.context.Lifecycle
    • isAutoStartup

      public boolean isAutoStartup()
      Specified by:
      isAutoStartup in interface org.springframework.context.SmartLifecycle