{"record":{"id":"a59e44d73ac3f37f","repo":"gofr-dev/gofr","slug":"w-executing-search-w","errorCode":null,"errorMessage":"%w: executing search: %w","messagePattern":"%w: executing search: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"critical","filePath":"pkg/gofr/datasource/elasticsearch/elasticsearch.go","lineNumber":206,"sourceCode":"\t}\n\n\tstart := time.Now()\n\n\ttracedCtx, span := c.addTrace(ctx, \"search\", indices, \"\")\n\n\tbody, err := json.Marshal(query)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"%w: query: %w\", errMarshaling, err)\n\t}\n\n\treq := esapi.SearchRequest{\n\t\tIndex: indices,\n\t\tBody:  bytes.NewReader(body),\n\t}\n\n\tres, err := req.Do(tracedCtx, c.client)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"%w: executing search: %w\", errOperation, err)\n\t}\n\n\tdefer res.Body.Close()\n\n\tif res.IsError() {\n\t\treturn nil, fmt.Errorf(\"%w: %s\", errResponse, res.String())\n\t}\n\n\tvar result map[string]any\n\tif err := json.NewDecoder(res.Body).Decode(&result); err != nil {\n\t\treturn nil, fmt.Errorf(\"%w: %w\", errParsingResponse, err)\n\t}\n\n\tc.sendOperationStats(start, \"SEARCH\", indices, \"\", query, span)\n\n\treturn result, nil\n}\n","sourceCodeStart":188,"sourceCodeEnd":224,"githubUrl":"https://github.com/gofr-dev/gofr/blob/187eb24962502e91f1fee856230670958b66e89c/pkg/gofr/datasource/elasticsearch/elasticsearch.go#L188-L224","documentation":"This is errOperation wrapped around the transport error from esapi.SearchRequest.Do. The search request failed before an HTTP response was produced: unreachable cluster, connection reset, TLS failure, or context deadline exceeded. The inner wrapped error distinguishes the transport cause.","triggerScenarios":"Calling Search when the ES cluster is unreachable or down, DNS fails, the port is wrong, Connect() never succeeded, the context times out before the query completes (common with expensive queries), or a load balancer drops the connection.","commonSituations":"Developers hit this in production when ES is restarted or under load and expensive queries exceed context deadlines, in k8s when service DNS/name is wrong, or in tests where the ES container isn't ready.","solutions":["Check cluster reachability (curl the endpoint) and Config.Addresses correctness.","Inspect the inner wrapped error: connection refused vs timeout vs TLS to target the fix.","Increase the context deadline or optimize the query (fewer fields, pagination, aggregations tuning) if timing out.","Add retry-with-backoff for transient connection resets.","Verify startup Connect()/HealthCheck succeeded before issuing searches."],"exampleFix":"// before\nres, err := client.Search(ctx, []string{\"orders\"}, bigAggregationQuery) // context deadline exceeded\n// after\nqctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)\ndefer cancel()\nres, err := client.Search(qctx, []string{\"orders\"}, optimizedQuery)","handlingStrategy":"retry","validationCode":"ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)\ndefer cancel()\nif _, err := client.HealthCheck(ctx); err != nil {\n    return nil, fmt.Errorf(\"elasticsearch unavailable: %w\", err)\n}","typeGuard":null,"tryCatchPattern":"var result map[string]any\nvar err error\nfor attempt := 0; attempt < 3; attempt++ {\n    qctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)\n    result, err = client.Search(qctx, indices, query)\n    cancel()\n    if err == nil {\n        break\n    }\n    if strings.Contains(err.Error(), \"executing search\") { // transport-level\n        time.Sleep(backoff(attempt))\n        continue\n    }\n    break // non-transport errors are not retryable\n}","preventionTips":["Size context deadlines to your slowest query; profile expensive aggregations.","Add retry with exponential backoff for transient connection resets.","Verify DNS/service names in containerized environments (k8s service vs pod).","Check HealthCheck at startup and on a monitor loop to detect outages early."],"tags":["elasticsearch","network","timeout","search"],"backgroundTag":"connection-refused","analyzedSha":"187eb24962502e91f1fee856230670958b66e89c","analyzedAt":"2026-09-01T20:34:54.554Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}