gofr-dev/gofr · error
elasticsearch: %w
Error message
elasticsearch: %w
What it means
This error wraps the underlying failure from Elasticsearch's Search API when the migrator queries the gofr_migrations index to find the latest applied migration version. The %w wrapping preserves the original client error, so the root cause (connection refused, index missing, bad query) is in the wrapped chain. It is thrown by getLastMigration during the migration bootstrap phase.
Source
Thrown at pkg/gofr/migration/elasticsearch.go:100
err = c.Elasticsearch.CreateIndex(context.Background(), elasticsearchMigrationIndex, settings)
if err != nil {
return fmt.Errorf("failed to create migration index: %w", err)
}
c.Debugf("Created Elasticsearch migration index: %s", elasticsearchMigrationIndex)
}
return em.migrator.checkAndCreateMigrationTable(c)
}
// getLastMigration retrieves the latest migration version from Elasticsearch.
func (em elasticsearchMigrator) getLastMigration(c *container.Container) (int64, error) {
var lastMigration int64
result, err := c.Elasticsearch.Search(context.Background(), []string{elasticsearchMigrationIndex}, getLastElasticsearchMigrationQuery())
if err != nil {
return -1, fmt.Errorf("elasticsearch: %w", err)
}
lastMigration = extractLastMigrationVersion(result)
c.Debugf("Elasticsearch last migration fetched value is: %v", lastMigration)
lm2, err := em.migrator.getLastMigration(c)
if err != nil {
return -1, err
}
return max(lastMigration, lm2), nil
}
// extractLastMigrationVersion extracts the latest migration version from the Elasticsearch search result.
func extractLastMigrationVersion(result map[string]any) int64 {
hits, ok := result["hits"].(map[string]any)
if !ok {
return 0View on GitHub (pinned to 187eb24962)
Solutions
- Verify Elasticsearch connectivity (host, port, credentials) in container config
- Create/allow the gofr_migrations index or enable ignore_unavailable on the search
- Check ES cluster health and logs for the wrapped root cause
- Retry after network issues; migrations can safely re-run getLastMigration
Example fix
// before
result, err := c.Elasticsearch.Search(ctx, []string{"gofr_migrations"}, query) // err surfaced as elasticsearch: ...
// after
if _, err := c.Elasticsearch.Ping(ctx); err != nil { return fmt.Errorf("elasticsearch unreachable, check config: %w", err) }
result, err := c.Elasticsearch.Search(ctx, []string{"gofr_migrations"}, query) Defensive patterns
Strategy: try-catch
Validate before calling
if c.Elasticsearch == nil { return errors.New("elasticsearch datasource not configured") }
if err := c.Elasticsearch.Ping(context.Background()); err != nil { return fmt.Errorf("elasticsearch unreachable: %w", err) } Try / catch
err := migrator.Run(c)
if err != nil && strings.Contains(err.Error(), "elasticsearch:") {
log.Printf("ES migration lookup failed, root cause: %v", errors.Unwrap(err))
} Prevention
- Add a startup health check that pings Elasticsearch before running migrations
- Ensure the gofr_migrations index exists (or queries tolerate missing indices)
- Use connection settings with timeout and retry for ES
- Alert on ES cluster health before deployments
When it happens
Trigger: c.Elasticsearch.Search() on the elasticsearchMigrationIndex returns an error: ES unreachable, index missing without ignore_unavailable, auth failure, or malformed query response.
Common situations: Elasticsearch not running or wrong host/port in config; missing credentials; cluster red state; index deleted manually while migrations table expected.
Related errors
- failed to record migration: %w
- mongo: %w
- invalid elasticsearch response
- error encoding operation
- %w: settings: %w
AI-assisted analysis of gofr-dev/gofr@187eb24962 (2026-09-01).
Data as JSON: /api/errors/adeea52a916fe0dc.
Report an issue: GitHub.