Back to AI Tools & Agents

langchain4j-vector-stores-configuration

LangChain4JRAGVector StoresEmbedding StorageSemantic SearchPostgreSQLPineconeAI Development
⭐ 282πŸ“„ MITπŸ•’ 2026-06-15Source β†—

Install this skill

npx skills add giuseppe-trisciuoglio/developer-kit

Works across Claude Code, Cursor, Codex, Copilot & Antigravity

The langchain4j-vector-stores-configuration skill provides a systematic approach for integrating Java-based applications with vector databases via the LangChain4J library. It handles the low-level infrastructure requirements for Retrieval-Augmented Generation, such as establishing connectivity to PostgreSQL (via PgVector) or MongoDB, defining embedding dimensions, and managing connection pools through HikariCP. Developers use this skill to map internal document segments into searchable vector space, implement metadata filtering for precise retrieval, and build health checks for infrastructure observability. By abstracting store interactions into bean-based configurations, it allows teams to swap underlying vector databases or manage multiple specialized stores within a single Spring Boot context. This skill ensures that your Java backend maintains a stable, performant link to semantic storage while managing complex document ingestion pipelines effectively.

When to Use This Skill

  • β€’Developing RAG systems for private enterprise knowledge bases
  • β€’Implementing hybrid search models with metadata constraints
  • β€’Managing isolated vector stores for chat memory vs document indexing
  • β€’Scaling database connections for high-throughput AI services

How to Invoke This Skill

Example prompts that trigger this skill in Claude Code, Cursor, or Antigravity:

  • β€œConfigure a new vector store in LangChain4J
  • β€œHow to set up PgVector with LangChain4J
  • β€œImplement metadata filtering for embedding search
  • β€œCreate a Spring Boot configuration for vector stores
  • β€œOptimize connection settings for my vector database

Pro Tips

  • πŸ’‘Always align your chosen vector store's capabilities with your application's data volume, query patterns, and latency requirements for optimal performance.
  • πŸ’‘Implement robust indexing strategies and monitor query execution plans within your vector database to identify and resolve performance bottlenecks early.
  • πŸ’‘Utilize LangChain4J's flexible builder patterns to abstract vector store configurations, allowing for easy swapping between providers and environments without significant code changes.

What this skill does

  • β€’Declarative embedding store configuration using Spring Beans
  • β€’Automated document ingestion and segmentation pipelines
  • β€’Metadata-based search filtering for specific retrieval logic
  • β€’Connection pooling integration for production stability
  • β€’Custom health indicators for vector database uptime tracking

When not to use it

  • βœ•Small projects not requiring persistent storage or semantic search
  • βœ•Scenarios using non-JVM languages or lightweight frontend-only AI
  • βœ•Projects requiring extremely simple in-memory vector storage without complex metadata

Example workflow

  1. Define the target database infrastructure bean
  2. Initialize the EmbeddingStore with dimension parameters
  3. Configure the EmbeddingStoreIngestor with a document splitter
  4. Establish metadata field mappings for indexing
  5. Execute a retrieval request using custom filter criteria
  6. Validate connectivity through a custom health monitor

Prerequisites

  • –Java 17+
  • –Spring Boot framework
  • –Active LangChain4J dependency
  • –Running instance of a supported vector database

Pitfalls & limitations

  • !Mismatched embedding dimensions between the model and database store
  • !Exhausting database connection limits by failing to use connection pooling
  • !Over-splitting documents leading to loss of context for retrieval
  • !Unoptimized database indexes causing slow semantic search performance

FAQ

Can I use multiple vector stores at once?
Yes, by defining separate Spring Beans with unique qualifiers, you can point different parts of your application to different vector stores.
What is the importance of dimension settings?
The dimension count must exactly match the output format of your chosen embedding model, or the database will fail to store or query your vectors correctly.
How do I filter by non-vector data?
Use the metadata field mapping during store initialization, then apply filters in your EmbeddingSearchRequest using supported criteria like isEqualTo or isGreaterThan.

How it compares

Doing this manually requires writing boilerplate JDBC or NoSQL drivers and custom vector-to-text mapping logic; this skill provides a unified, maintainable abstraction layer across various vector database providers.

Source & trust

⭐ 282 starsπŸ“„ MITπŸ•’ Updated 2026-06-15
πŸ“„ Full skill instructions β€” original source: giuseppe-trisciuoglio/developer-kit
# LangChain4J Vector Stores Configuration

Configure vector stores for Retrieval-Augmented Generation applications with LangChain4J.

## When to Use

To configure vector stores when:

- Building RAG applications requiring embedding storage and retrieval
- Implementing semantic search in Java applications
- Integrating LLMs with vector databases for context-aware responses
- Configuring multi-modal embedding storage for text, images, or other data
- Setting up hybrid search combining vector similarity and full-text search
- Migrating between different vector store providers
- Optimizing vector database performance for production workloads
- Building AI-powered applications with memory and persistence
- Implementing document chunking and embedding pipelines
- Creating recommendation systems based on vector similarity

## Instructions

### Set Up Basic Vector Store

Configure an embedding store for vector operations:

@Bean
public EmbeddingStore<TextSegment> embeddingStore() {
return PgVectorEmbeddingStore.builder()
.host("localhost")
.port(5432)
.database("vectordb")
.user("username")
.password("password")
.table("embeddings")
.dimension(1536) // OpenAI embedding dimension
.createTable(true)
.useIndex(true)
.build();
}


### Configure Multiple Vector Stores

Use different stores for different use cases:

@Configuration
public class MultiVectorStoreConfiguration {

@Bean
@Qualifier("documentsStore")
public EmbeddingStore<TextSegment> documentsEmbeddingStore() {
return PgVectorEmbeddingStore.builder()
.table("document_embeddings")
.dimension(1536)
.build();
}

@Bean
@Qualifier("chatHistoryStore")
public EmbeddingStore<TextSegment> chatHistoryEmbeddingStore() {
return MongoDbEmbeddingStore.builder()
.collectionName("chat_embeddings")
.build();
}
}


### Implement Document Ingestion

Use EmbeddingStoreIngestor for automated document processing:

@Bean
public EmbeddingStoreIngestor embeddingStoreIngestor(
EmbeddingStore<TextSegment> embeddingStore,
EmbeddingModel embeddingModel) {

return EmbeddingStoreIngestor.builder()
.documentSplitter(DocumentSplitters.recursive(
300, // maxSegmentSizeInTokens
20, // maxOverlapSizeInTokens
new OpenAiTokenizer(GPT_3_5_TURBO)
))
.embeddingModel(embeddingModel)
.embeddingStore(embeddingStore)
.build();
}


### Set Up Metadata Filtering

Configure metadata-based filtering capabilities:

// MongoDB with metadata field mapping
IndexMapping indexMapping = IndexMapping.builder()
.dimension(1536)
.metadataFieldNames(Set.of("category", "source", "created_date", "author"))
.build();

// Search with metadata filters
EmbeddingSearchRequest request = EmbeddingSearchRequest.builder()
.queryEmbedding(queryEmbedding)
.maxResults(10)
.filter(and(
metadataKey("category").isEqualTo("technical_docs"),
metadataKey("created_date").isGreaterThan(LocalDate.now().minusMonths(6))
))
.build();


### Configure Production Settings

Implement connection pooling and monitoring:

@Bean
public EmbeddingStore<TextSegment> optimizedPgVectorStore() {
HikariConfig hikariConfig = new HikariConfig();
hikariConfig.setJdbcUrl("jdbc:postgresql://localhost:5432/vectordb");
hikariConfig.setUsername("username");
hikariConfig.setPassword("password");
hikariConfig.setMaximumPoolSize(20);
hikariConfig.setMinimumIdle(5);
hikariConfig.setConnectionTimeout(30000);

DataSource dataSource = new HikariDataSource(hikariConfig);

return PgVectorEmbeddingStore.builder()
.dataSource(dataSource)
.table("embeddings")
.dimension(1536)
.useIndex(true)
.build();
}


### Implement Health Checks

Monitor vector store connectivity:

@Component
public class VectorStoreHealthIndicator implements HealthIndicator {

private final EmbeddingStore<TextSegment> embeddingStore;

@Override
public Health health() {
try {
embeddingStore.search(EmbeddingSearchRequest.builder()
.queryEmbedding(new Embedding(Collections.nCopies(1536, 0.0f)))
.maxResults(1)
.build());

return Health.up()
.withDetail("store", embeddingStore.getClass().getSimpleName())
.build();
} catch (Exception e) {
return Health.down()
.withDetail("error", e.getMessage())
.build();
}
}
}


## Examples

### Basic RAG Application Setup

@Configuration
public class SimpleRagConfig {

@Bean
public EmbeddingStore<TextSegment> embeddingStore() {
return PgVectorEmbeddingStore.builder()
.host("localhost")
.database("rag_db")
.table("documents")
.dimension(1536)
.build();
}

@Bean
public ChatLanguageModel chatModel() {
return OpenAiChatModel.withApiKey(System.getenv("OPENAI_API_KEY"));
}
}


### Semantic Search Service

@Service
public class SemanticSearchService {

private final EmbeddingStore<TextSegment> store;
private final EmbeddingModel embeddingModel;

public List<String> search(String query, int maxResults) {
Embedding queryEmbedding = embeddingModel.embed(query).content();

EmbeddingSearchRequest request = EmbeddingSearchRequest.builder()
.queryEmbedding(queryEmbedding)
.maxResults(maxResults)
.minScore(0.75)
.build();

return store.search(request).matches().stream()
.map(match -> match.embedded().text())
.toList();
}
}


### Production Setup with Monitoring

@Configuration
public class ProductionVectorStoreConfig {

@Bean
public EmbeddingStore<TextSegment> vectorStore(
@Value("${vector.store.host}") String host,
MeterRegistry meterRegistry) {

EmbeddingStore<TextSegment> store = PgVectorEmbeddingStore.builder()
.host(host)
.database("production_vectors")
.useIndex(true)
.indexListSize(200)
.build();

return new MonitoredEmbeddingStore<>(store, meterRegistry);
}
}


## Best Practices

### Choose the Right Vector Store

**For Development:**
- Use InMemoryEmbeddingStore for local development and testing
- Fast setup, no external dependencies
- Data lost on application restart

**For Production:**
- **PostgreSQL + pgvector**: Excellent for existing PostgreSQL environments
- **Pinecone**: Managed service, good for rapid prototyping
- **MongoDB Atlas**: Good integration with existing MongoDB applications
- **Milvus/Zilliz**: High performance for large-scale deployments

### Configure Appropriate Index Types

Choose index types based on performance requirements:

// For high recall requirements
.indexType(IndexType.FLAT) // Exact search, slower but accurate

// For balanced performance
.indexType(IndexType.IVF_FLAT) // Good balance of speed and accuracy

// For high-speed approximate search
.indexType(IndexType.HNSW) // Fastest, slightly less accurate


### Optimize Vector Dimensions

Match embedding dimensions to your model:

// OpenAI text-embedding-3-small
.dimension(1536)

// OpenAI text-embedding-3-large
.dimension(3072)

// Sentence Transformers
.dimension(384) // all-MiniLM-L6-v2
.dimension(768) // all-mpnet-base-v2


### Implement Batch Operations

Use batch operations for better performance:

@Service
public class BatchEmbeddingService {

private static final int BATCH_SIZE = 100;

public void addDocumentsBatch(List<Document> documents) {
for (List<Document> batch : Lists.partition(documents, BATCH_SIZE)) {
List<TextSegment> segments = batch.stream()
.map(doc -> TextSegment.from(doc.text(), doc.metadata()))
.collect(Collectors.toList());

List<Embedding> embeddings = embeddingModel.embedAll(segments)
.content();

embeddingStore.addAll(embeddings, segments);
}
}
}


### Secure Configuration

Protect sensitive configuration:

// Use environment variables
@Value("${vector.store.api.key:#{null}}")
private String apiKey;

// Validate configuration
@PostConstruct
public void validateConfiguration() {
if (StringUtils.isBlank(apiKey)) {
throw new IllegalStateException("Vector store API key must be configured");
}
}


## References

For comprehensive documentation and advanced configurations, see:

- [API Reference](references/api-reference.md) - Complete API documentation
- [Examples](references/examples.md) - Production-ready examples

How to Use This Skill Unit

Option A: Project-Specific (Recommended)

  1. Click "Download" above
  2. In your project, create the directory: .agent/skills/langchain4j-vector-stores-configuration/
  3. Save the file as SKILL.md
  4. The agent will automatically discover the skill based on its description.

Option B: Global Installation (All Agents)

Save the file to these locations to make it available across all projects:

  • Claude Code: ~/.claude/skills/giuseppe-trisciuoglio/developer-kit/langchain4j-vector-stores-configuration/SKILL.md
  • Cursor: ~/.cursor/skills/giuseppe-trisciuoglio/developer-kit/langchain4j-vector-stores-configuration/SKILL.md
  • Antigravity: ~/.gemini/antigravity/skills/giuseppe-trisciuoglio/developer-kit/langchain4j-vector-stores-configuration/SKILL.md

πŸš€ Install with CLI:
npx skills add giuseppe-trisciuoglio/developer-kit

Read the Master Guide: Mastering Agent Skills β†’

Recommended Rules

View more rules β†’

Recommended Workflows

View more workflows β†’

Recommended MCP Servers

View more MCP servers β†’

Take It Further

Maximize your productivity with these powerful resources

πŸ“‹

Define Your Standards

Set up coding standards to ensure this workflow produces consistent, high-quality results.

Browse Rules Library
πŸ“–

Master Workflows

Learn how to create custom workflows, use Turbo Mode, and build your automation library.

Complete Guide

How to use this Skill in Claude Code & Cursor

For Claude Code (CLI)

To use this skill in Claude Code, copy the rule content into your project's custom instructions or follow our Add-Skill CLI guide. This ensures Claude follows your standards during every code generation.

For Cursor & Windsurf

For Cursor or Windsurf, individual skills are best used in the "Rules for AI" section. This specific unit helps the agent avoid ai tools & agents issues, leading to cleaner, more efficient code.

Why the skill format matters: the standardized Agent Skills format lets your AI agent load detailed instructions only when they are relevant, keeping your prompt clean while improving results.

Source & attribution

This skill is categorized under AI Tools & Agents and is published by Giuseppe Trisciuoglio, maintained in giuseppe-trisciuoglio/developer-kit.

← Browse All Agent Skills
Sponsored AI assistant. Recommendations may be paid.