{"record":{"id":"8bf56c70a7e85df4","repo":"xkcoding/spring-boot-demo","slug":"index-object","errorCode":null,"errorMessage":"更新索引 {index} 数据 {object} 失败","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":124,"sourceCode":"     */\n    protected static IndexRequest buildIndexRequest(String index, String id, Object object) {\n        return new IndexRequest(index).id(id).source(BeanUtil.beanToMap(object), XContentType.JSON);\n    }\n\n    /**\n     * exec updateRequest\n     *\n     * @param index  elasticsearch index name\n     * @param id     Document id\n     * @param object request object\n     * @author fxbin\n     */\n    protected void updateRequest(String index, String id, Object object) {\n        try {\n            UpdateRequest updateRequest = new UpdateRequest(index, id).doc(BeanUtil.beanToMap(object), XContentType.JSON);\n            client.update(updateRequest, COMMON_OPTIONS);\n        } catch (IOException e) {\n            throw new ElasticsearchException(\"更新索引 {\" + index + \"} 数据 {\" + object + \"} 失败\");\n        }\n    }\n\n    /**\n     * exec deleteRequest\n     *\n     * @param index elasticsearch index name\n     * @param id    Document id\n     * @author fxbin\n     */\n    protected void deleteRequest(String index, String id) {\n        try {\n            DeleteRequest deleteRequest = new DeleteRequest(index, id);\n            client.delete(deleteRequest, COMMON_OPTIONS);\n        } catch (IOException e) {\n            throw new ElasticsearchException(\"删除索引 {\" + index + \"} 数据id {\" + id + \"} 失败\");\n        }\n    }","sourceCodeStart":106,"sourceCodeEnd":142,"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#L106-L142","documentation":"Thrown by updateRequest when client.update() raises an IOException during a partial document update via UpdateRequest.doc(). The object is converted to a Map with BeanUtil.beanToMap and serialized as JSON. An additional failure mode not covered by this catch: BeanUtil.beanToMap can produce empty or malformed maps if the object has no readable properties, causing an ES-side validation error (RuntimeException). The original IOException is not chained.","triggerScenarios":"Calling updateRequest(index, id, object) when: the cluster is unreachable (IOException); the index or document id does not exist (ES returns 404 — surfaces as RuntimeException); the object has null fields that BeanUtil skips, yielding an empty doc and an ES validation error.","commonSituations":"Updating a document in a non-existent index; network timeout on large document payloads; object with only null fields after beanToMap produces an empty partial doc; cluster connection pool saturated.","solutions":["Verify the index and document id exist before calling updateRequest (use client.get() to check).","Ensure the passed object has at least one non-null field so BeanUtil.beanToMap yields a non-empty doc.","Chain the cause: new ElasticsearchException(\"...\", e).","Confirm cluster connectivity and that the 30MB heap buffer (COMMON_OPTIONS) is sufficient for the payload."],"exampleFix":"// before\n} catch (IOException e) {\n    throw new ElasticsearchException(\"更新索引 {\" + index + \"} 数据 {\" + object + \"} 失败\");\n}\n\n// after\n} catch (IOException e) {\n    throw new ElasticsearchException(\"更新索引 {\" + index + \"} 数据 {\" + object + \"} 失败\", e);\n}","handlingStrategy":"validation","validationCode":"// Validate object has non-null fields before update\nMap<String, Object> docMap = BeanUtil.beanToMap(object);\nif (docMap.isEmpty() || docMap.values().stream().allMatch(Objects::isNull)) {\n    throw new IllegalArgumentException(\"Cannot update with an empty or null-only document\");\n}\n// Verify index and document exist\nGetRequest getRequest = new GetRequest(index, id);\nif (!client.exists(getRequest, COMMON_OPTIONS)) {\n    throw new IllegalStateException(\"Document \" + id + \" not found in index \" + index);\n}","typeGuard":null,"tryCatchPattern":"try {\n    updateRequest(index, id, object);\n} catch (ElasticsearchException e) {\n    log.error(\"Update failed for index={}, id={}: {}\", index, id, e.getMessage());\n    throw e;\n}","preventionTips":["Ensure the object has at least one non-null field before converting to a doc map.","Verify the document exists before attempting a partial update.","Use upsert (docAsUpsert) if you want create-on-update semantics."],"tags":["elasticsearch","network","io-exception","bean-mapping","rest-high-level-client"],"backgroundTag":null,"analyzedSha":"87a142f9604c1a5365b4d24d22c2c11c26a9d5ab","analyzedAt":"2026-08-14T01:16:58.217Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}