Class PgVectorStore

java.lang.Object
sk.iway.iwcm.rag.vectorstore.PgVectorStore
All Implemented Interfaces:
VectorStore

@Service public class PgVectorStore extends Object implements VectorStore
Stores and searches embeddings in PostgreSQL using pgvector and native full-text queries. Manages vector updates and schema initialization while normal chunk persistence uses the JPA repository. Connects to the resolved RAG datasource, either the primary PostgreSQL database or rag_jpa.
  • Constructor Details

    • PgVectorStore

      public PgVectorStore()
  • Method Details

    • updateEmbedding

      public void updateEmbedding(Long id, float[] embedding)
      Description copied from interface: VectorStore
      Update the embedding vector for an existing chunk entity. The entity must already be saved via EmbeddingChunkRepository.
      Specified by:
      updateEmbedding in interface VectorStore
      Parameters:
      id - the chunk entity ID
      embedding - the embedding vector
    • updateEmbeddingBatch

      public void updateEmbeddingBatch(List<Long> ids, List<float[]> embeddings)
      Description copied from interface: VectorStore
      Update embedding vectors for multiple existing chunk entities in a batch. All entities must already be saved via EmbeddingChunkRepository.
      Specified by:
      updateEmbeddingBatch in interface VectorStore
      Parameters:
      ids - the chunk entity IDs
      embeddings - the embedding vectors (must match ids in size and order)
    • search

      public List<VectorSearchResult> search(float[] queryEmbedding, String embeddingProvider, String embeddingModel, RagEntityType entityType, Integer domainId, String language, int limit, Map<String,Object> bonusParams)
      Description copied from interface: VectorStore
      Find the most similar chunks to the query embedding.
      Specified by:
      search in interface VectorStore
      Parameters:
      queryEmbedding - the query vector
      embeddingProvider - provider used to generate/query embeddings
      embeddingModel - model used to generate/query embeddings
      entityType - entity type to filter by (null for all)
      domainId - domain ID to filter by (null for all)
      language - language to filter by (null for all)
      limit - max number of results
      bonusParams - optional store-specific filters, such as document root groups
      Returns:
      list of search results ordered by similarity (descending)
    • searchFulltext

      public List<VectorSearchResult> searchFulltext(String query, String embeddingProvider, String embeddingModel, RagEntityType entityType, Integer domainId, String language, int limit, Map<String,Object> bonusParams)
      Description copied from interface: VectorStore
      Find relevant chunks by fulltext search in chunk text.
      Specified by:
      searchFulltext in interface VectorStore
      Parameters:
      query - textual query
      embeddingProvider - provider used to filter rows
      embeddingModel - model used to filter rows
      entityType - entity type to filter by (null for all)
      domainId - domain ID to filter by (null for all)
      language - language to filter by (null for all)
      limit - max number of results
      bonusParams - optional store-specific filters, such as document root groups and fallback flags
      Returns:
      list of search results ordered by fulltext rank (descending)
    • isAvailable

      public boolean isAvailable()
      Description copied from interface: VectorStore
      Check if the vector store can be used in the current runtime configuration. This does not guarantee that required schema objects already exist.
      Specified by:
      isAvailable in interface VectorStore
    • isAvailableAndInitialized

      public boolean isAvailableAndInitialized()
      Description copied from interface: VectorStore
      Check if the vector store is available and schema is initialized. Implementations should return true only when operations can be executed safely.
      Specified by:
      isAvailableAndInitialized in interface VectorStore
    • initializeSchema

      public boolean initializeSchema()
      Description copied from interface: VectorStore
      Initialize the database-specific vector schema if needed.
      Specified by:
      initializeSchema in interface VectorStore
    • recreateIndex

      public boolean recreateIndex()
      Recreate the database-specific vector index using the configured metric. Implementations without a native index-management capability may keep the default.
      Specified by:
      recreateIndex in interface VectorStore
      Returns:
      true when the index was recreated successfully
    • recreateHnswIndex

      public boolean recreateHnswIndex()
      Recreates the PostgreSQL HNSW index according to configured distance metric.
      Returns:
      true when the index was recreated successfully
    • getExistingEmbeddingsByHash

      public Map<String,float[]> getExistingEmbeddingsByHash(String entityType, long entityId, String embeddingProvider, String embeddingModel, int domainId)
      Fetches completed embeddings for content reuse within one source, provider, model, and domain.
      Specified by:
      getExistingEmbeddingsByHash in interface VectorStore
      Parameters:
      entityType - stored source entity type
      entityId - source identifier
      embeddingProvider - provider that generated the vectors
      embeddingModel - model that generated the vectors
      domainId - storage domain, including zero for shared Markdown
      Returns:
      vectors keyed by content hash, or an empty map when the datasource or query is unavailable
    • resetDimensions

      public boolean resetDimensions(int dimensions)
      Description copied from interface: VectorStore
      Delete all stored embeddings and resize the vector column.
      Specified by:
      resetDimensions in interface VectorStore
      Parameters:
      dimensions - new vector dimensions