Overview
The OpenSearch migration system provides a zero-downtime approach to reindexing products (courses and consultations) into new index versions. It uses an alias-based architecture where applications query aliases instead of direct indices, allowing seamless index swaps.Architecture Components
1. OpenSearchController (opensearch.controller.ts)
The main API entry point for triggering migrations and reindexing operations.
Key Endpoints:
One-Click Product Migration
Other Useful Endpoints
GET /opensearch/index-status- View current index versions and alias mappingsPOST /opensearch/reindex-batch- Batch reindex without creating new versionsPOST /opensearch/reindex-document- Reindex a single document by IDPOST /opensearch/migrate/:index/:version- Migrate a specific index to a versionPOST /opensearch/drop-all- Development only: drop all indices
Migration Process Flow
Step-by-Step: What Happens When You Click “Migrate”
Detailed Component Breakdown
2. ProductIndexMigrationService (product-index-migration.service.ts)
Purpose: Enqueues migration jobs into BullMQ queue.
Key Method:
3. ProductMigrationWorker (product-migration.worker.ts)
Purpose: Background worker that orchestrates the entire migration process.
Key Responsibilities:
-
Version Resolution
-
Create New Index
-
Batch Processing
-
Verification
-
Alias Switch (Zero-Downtime)
4. ElasticService (elastic.service.ts)
Core Methods:
createNewIndexVersion(index, version)
Creates a new versioned index with proper mappings.
switchAliasToNew(alias, newIndex, context)
Atomically switches alias from old index to new index.
verifyReindex(sourceIndex, destIndex)
Ensures document counts match between old and new indices.
Index Naming Convention
Application Code: Always queries the alias (e.g.,
course), never the versioned index directly.
Migration Strategies
Strategy 1: Full Migration (All Products)
- Auto-increments version for both courses and consultations
- Creates
course_v2andconsultation_v2 - Reindexes all documents
- Switches aliases atomically
Strategy 2: Incremental Migration (Updated Products Only)
- Only migrates products updated after the specified date
- Useful for quick updates without full reindex
Strategy 3: Single Product Type
- Only migrates courses to version 3
- Consultations remain unchanged
Monitoring Migration Progress
Check Index Status
Safety Features
1. Verification Before Alias Switch
The system verifies document counts match before switching aliases:2. Atomic Alias Updates
OpenSearch’supdateAliases API ensures atomic operations—no downtime.
3. Job Tracking
All migrations are tracked via BullMQ jobs with progress updates and logs.4. Metadata Tracking
TheElasticIndexMetadataService records:
- Current active version per environment
- Migration timestamps
- Document counts
- Job IDs
Common Use Cases
Use Case 1: Schema Change
You updated the OpenSearch mapping for courses. Solution:Use Case 2: Data Corruption
Some documents are corrupted in the current index. Solution:Use Case 3: Performance Optimization
You want to reindex with better settings (shards, replicas). Solution:- Update mapping in code
- Trigger migration
- Old index remains until you manually delete it
Configuration
Alias Mapping (Config)
Environment Variables
Best Practices
-
Always Use Aliases in Application Code
- Monitor Job Progress Use BullMQ dashboard or logs to track migration jobs.
- Test in Staging First Always test migrations in staging before production.
-
Clean Up Old Indices
After verifying new index works, manually delete old versions:
-
Use Incremental Updates for Large Datasets
For millions of documents, use
updatedSinceto avoid full reindex.
Troubleshooting
Issue: “Reindex verification failed”
Cause: Document counts don’t match between old and new indices. Solution:- Check for errors in child reindex jobs
- Verify database queries aren’t filtered incorrectly
- Retry migration
Issue: “Index already exists”
Cause: Trying to create a version that already exists. Solution:- Increment version number manually
- Or delete the existing versioned index first
Issue: Migration stuck
Cause: Child jobs failing or queue issues. Solution:- Check BullMQ queue health
- Review worker logs
- Restart workers if needed
Summary: One-Click Migration Flow
- Client sends
POST /elastic/products/migrate { "type": "all" } - Controller enqueues job and returns
jobId - Worker creates new versioned indices (
course_v2,consultation_v2) - Worker dispatches batch reindex jobs (parallel processing)
- Worker waits for all batches to complete
- Worker verifies document counts match
- Worker atomically switches aliases to new indices
- Worker records metadata and completes job
- Application continues querying aliases with zero downtime