{"record":{"id":"ec79961ba58aa1b6","repo":"xkcoding/spring-boot-demo","slug":"index","errorCode":null,"errorMessage":"创建索引 {index} 失败","messagePattern":"创建索引 (.+?) 失败","errorType":"exception","errorClass":"ElasticsearchException","httpStatus":null,"severity":"error","filePath":"demo-elasticsearch-rest-high-level-client/src/main/java/com/xkcoding/elasticsearch/service/base/BaseElasticsearchService.java","lineNumber":69,"sourceCode":"\n    /**\n     * create elasticsearch index (asyc)\n     *\n     * @param index elasticsearch index\n     * @author fxbin\n     */\n    protected void createIndexRequest(String index) {\n        try {\n            CreateIndexRequest request = new CreateIndexRequest(index);\n            // Settings for this index\n            request.settings(Settings.builder().put(\"index.number_of_shards\", elasticsearchProperties.getIndex().getNumberOfShards()).put(\"index.number_of_replicas\", elasticsearchProperties.getIndex().getNumberOfReplicas()));\n\n            CreateIndexResponse createIndexResponse = client.indices().create(request, COMMON_OPTIONS);\n\n            log.info(\" whether all of the nodes have acknowledged the request : {}\", createIndexResponse.isAcknowledged());\n            log.info(\" Indicates whether the requisite number of shard copies were started for each shard in the index before timing out :{}\", createIndexResponse.isShardsAcknowledged());\n        } catch (IOException e) {\n            throw new ElasticsearchException(\"创建索引 {\" + index + \"} 失败\");\n        }\n    }\n\n    /**\n     * delete elasticsearch index\n     *\n     * @param index elasticsearch index name\n     * @author fxbin\n     */\n    protected void deleteIndexRequest(String index) {\n        DeleteIndexRequest deleteIndexRequest = buildDeleteIndexRequest(index);\n        try {\n            client.indices().delete(deleteIndexRequest, COMMON_OPTIONS);\n        } catch (IOException e) {\n            throw new ElasticsearchException(\"删除索引 {\" + index + \"} 失败\");\n        }\n    }\n","sourceCodeStart":51,"sourceCodeEnd":87,"githubUrl":"https://github.com/xkcoding/spring-boot-demo/blob/87a142f9604c1a5365b4d24d22c2c11c26a9d5ab/demo-elasticsearch-rest-high-level-client/src/main/java/com/xkcoding/elasticsearch/service/base/BaseElasticsearchService.java#L51-L87","documentation":"Thrown by createIndexRequest when RestHighLevelClient.indices().create() raises an IOException — the underlying HTTP call to the Elasticsearch cluster failed at the transport layer. The catch wraps only IOException, so any RuntimeException (e.g. ElasticsearchStatusException for a pre-existing index or invalid settings) propagates uncaught. The original exception `e` is also discarded (not passed as cause), so the root cause stack trace is lost.","triggerScenarios":"Calling createIndexRequest(indexName) when the ES cluster is unreachable (wrong host/port in demo.data.elasticsearch.clusterNodes), network partition, connect/socket timeout (default 1000ms/30000ms), or when the clusterNode list is empty. Also triggered by auth misconfiguration when security is enabled on the cluster but account credentials are unset.","commonSituations":"Elasticsearch cluster not started or on a different host than configured; firewall blocking ports 9200/9300; x-pack security enabled but username/password properties left null; RestClient connection pool exhausted (maxConnectTotal=30).","solutions":["Verify the cluster is reachable: curl the cluster node URL (schema://host:9200) and confirm a 200 response.","Check application.yml: demo.data.elasticsearch.clusterNodes must list correct host:port entries; set account.username/password if x-pack security is on.","Pass the caught exception as the cause so the real error surfaces: new ElasticsearchException(\"...\", e).","Widen the catch to include RuntimeException or ElasticsearchStatusException to handle ES-level errors (index already exists) gracefully."],"exampleFix":"// before\n} catch (IOException e) {\n    throw new ElasticsearchException(\"创建索引 {\" + index + \"} 失败\");\n}\n\n// after\n} catch (IOException e) {\n    throw new ElasticsearchException(\"创建索引 {\" + index + \"} 失败\", e);\n} catch (ElasticsearchStatusException e) {\n    throw new ElasticsearchException(\"创建索引 {\" + index + \"} 失败: \" + e.getDetailedMessage(), e);\n}","handlingStrategy":"validation","validationCode":"// Verify cluster connectivity before creating an index\nboolean reachable = false;\ntry {\n    org.elasticsearch.client.indices.GetIndexRequest existsReq =\n        new org.elasticsearch.client.indices.GetIndexRequest(index);\n    boolean exists = client.indices().exists(existsReq, COMMON_OPTIONS);\n    if (exists) {\n        // index already exists — skip creation or handle accordingly\n        return;\n    }\n    reachable = true;\n} catch (IOException e) {\n    log.error(\"ES cluster unreachable before createIndex\", e);\n}\nif (!reachable) return;","typeGuard":null,"tryCatchPattern":"try {\n    createIndexRequest(index);\n} catch (ElasticsearchException e) {\n    // ElasticsearchException is a RuntimeException — inspect e.getMessage() for the index name\n    log.error(\"Index creation failed for {}: {}\", index, e.getMessage());\n    throw e; // or handle with fallback\n}","preventionTips":["Validate clusterNodes configuration on startup with a health check.","Use a circuit breaker (e.g., Resilience4j) around ES client calls to fail fast when the cluster is down.","Always pass the caught exception as the cause to preserve the stack trace for debugging."],"tags":["elasticsearch","network","configuration","io-exception","rest-high-level-client"],"backgroundTag":null,"analyzedSha":"87a142f9604c1a5365b4d24d22c2c11c26a9d5ab","analyzedAt":"2026-08-14T01:16:58.217Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}