{"record":{"id":"adeea52a916fe0dc","repo":"gofr-dev/gofr","slug":"elasticsearch-w","errorCode":null,"errorMessage":"elasticsearch: %w","messagePattern":"elasticsearch: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/gofr/migration/elasticsearch.go","lineNumber":100,"sourceCode":"\n\t\terr = c.Elasticsearch.CreateIndex(context.Background(), elasticsearchMigrationIndex, settings)\n\t\tif err != nil {\n\t\t\treturn fmt.Errorf(\"failed to create migration index: %w\", err)\n\t\t}\n\n\t\tc.Debugf(\"Created Elasticsearch migration index: %s\", elasticsearchMigrationIndex)\n\t}\n\n\treturn em.migrator.checkAndCreateMigrationTable(c)\n}\n\n// getLastMigration retrieves the latest migration version from Elasticsearch.\nfunc (em elasticsearchMigrator) getLastMigration(c *container.Container) (int64, error) {\n\tvar lastMigration int64\n\n\tresult, err := c.Elasticsearch.Search(context.Background(), []string{elasticsearchMigrationIndex}, getLastElasticsearchMigrationQuery())\n\tif err != nil {\n\t\treturn -1, fmt.Errorf(\"elasticsearch: %w\", err)\n\t}\n\n\tlastMigration = extractLastMigrationVersion(result)\n\tc.Debugf(\"Elasticsearch last migration fetched value is: %v\", lastMigration)\n\n\tlm2, err := em.migrator.getLastMigration(c)\n\tif err != nil {\n\t\treturn -1, err\n\t}\n\n\treturn max(lastMigration, lm2), nil\n}\n\n// extractLastMigrationVersion extracts the latest migration version from the Elasticsearch search result.\nfunc extractLastMigrationVersion(result map[string]any) int64 {\n\thits, ok := result[\"hits\"].(map[string]any)\n\tif !ok {\n\t\treturn 0","sourceCodeStart":82,"sourceCodeEnd":118,"githubUrl":"https://github.com/gofr-dev/gofr/blob/187eb24962502e91f1fee856230670958b66e89c/pkg/gofr/migration/elasticsearch.go#L82-L118","documentation":"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.","triggerScenarios":"c.Elasticsearch.Search() on the elasticsearchMigrationIndex returns an error: ES unreachable, index missing without ignore_unavailable, auth failure, or malformed query response.","commonSituations":"Elasticsearch not running or wrong host/port in config; missing credentials; cluster red state; index deleted manually while migrations table expected.","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"],"exampleFix":"// before\nresult, err := c.Elasticsearch.Search(ctx, []string{\"gofr_migrations\"}, query) // err surfaced as elasticsearch: ...\n// after\nif _, err := c.Elasticsearch.Ping(ctx); err != nil { return fmt.Errorf(\"elasticsearch unreachable, check config: %w\", err) }\nresult, err := c.Elasticsearch.Search(ctx, []string{\"gofr_migrations\"}, query)","handlingStrategy":"try-catch","validationCode":"if c.Elasticsearch == nil { return errors.New(\"elasticsearch datasource not configured\") }\nif err := c.Elasticsearch.Ping(context.Background()); err != nil { return fmt.Errorf(\"elasticsearch unreachable: %w\", err) }","typeGuard":null,"tryCatchPattern":"err := migrator.Run(c)\nif err != nil && strings.Contains(err.Error(), \"elasticsearch:\") {\n    log.Printf(\"ES migration lookup failed, root cause: %v\", errors.Unwrap(err))\n}","preventionTips":["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"],"tags":["elasticsearch","migration","error-wrapping"],"backgroundTag":"elasticsearch-search-failed","analyzedSha":"187eb24962502e91f1fee856230670958b66e89c","analyzedAt":"2026-09-01T20:34:54.554Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}