RAG & Vector Operations
Contexa provides a layered vector storage architecture for RAG (Retrieval-Augmented Generation) workflows. The VectorOperations interface defines the contract, UnifiedVectorService provides general-purpose storage with caching, and AbstractVectorLabService enables domain-specific vector services with metrics, enrichment, and validation.
Overview
Vector operations power the CONTEXT_RETRIEVAL step of the AI pipeline. Documents are stored with metadata, embedded via Spring AI's embedding model, and persisted through the configured Spring AI VectorStore. When PgVector is configured, the same contracts operate on top of PgVector storage. Similarity searches retrieve relevant context before LLM execution.
The architecture has two layers:
- UnifiedVectorService — General-purpose vector operations with cache integration. Used for standard document storage and retrieval.
- AbstractVectorLabService — Base class for domain-specific vector services. Adds lab-specific metadata enrichment, validation, metrics tracking, and filter expressions.
BehaviorVectorServiceis the primary implementation for behavioral analysis data.
VectorOperations
The core interface for all vector store interactions.
public interface VectorOperations
SearchRequest.UnifiedVectorService
General-purpose implementation with cache integration through VectorStoreCacheLayer.
public class UnifiedVectorService implements VectorOperations
Key Behaviors
- Enriches documents with auto-generated
id,timestamp,documentType, andversionmetadata. - Uses
VectorStoreCacheLayerfor search result caching. Cache is invalidated on write operations. - Batch inserts use the configured
batchSizefromPgVectorStoreProperties. - All write operations are
@Transactional.
AbstractVectorLabService
Base class for domain-specific vector services that adds lab-aware processing, metrics, enrichment, and validation.
public abstract class AbstractVectorLabService implements VectorOperations
Abstract Methods (Must Implement)
Metrics Integration
When a VectorStoreMetrics bean is available, all operations (STORE, SEARCH, DELETE) are automatically tracked with operation counts, durations, and error rates per lab.
BehaviorVectorService
Specialized vector service for storing and querying user behavioral analysis data. Used by the HCAD system for anomaly detection.
public class BehaviorVectorService extends AbstractVectorLabService
Metadata Enrichment
Automatically adds temporal metadata (hour, dayOfWeek, isWeekend) and network metadata (networkSegment extracted from IP address) to each stored document.
Configuration
spring:
ai:
vectorstore:
pgvector:
index-type: HNSW
distance-type: COSINE_DISTANCE
dimensions: 384
contexa:
rag:
lab:
batch-size: 50
top-k: 100
similarity-threshold: 0.75
validation-enabled: true
enrichment-enabled: true
Code Examples
Storing and Searching Documents
// Store a security policy document
Document policyDoc = new Document(
"Users must use MFA for admin operations",
Map.of("documentType", "POLICY", "category", "auth"));
unifiedVectorService.storeDocument(policyDoc);
// Search for relevant context
List<Document> context = unifiedVectorService.searchSimilar(
"multi-factor authentication requirements",
Map.of("category", "auth"));
Behavior Pattern Storage
BehavioralAnalysisContext context = BehavioralAnalysisContext.builder()
.userId("user-456")
.remoteIp("10.0.1.55")
.currentActivity("ADMIN_ACCESS")
.userAgent("Mozilla/5.0")
.build();
behaviorVectorService.storeBehavior(context);
// Find similar historical behaviors
List<Document> similar = behaviorVectorService
.findSimilarBehaviors("user-456", "10.0.1.55", "/admin", 5);
Related
For RAG and vector store
application.yml properties, see AI Configuration — covers ContexaRagProperties (defaults, lab tuning, ETL options) and PgVectorStoreProperties (connection, batch size, dimensions, indexing).