xkcoding/spring-boot-demo · error · ElasticsearchException

更新索引 {index} 数据 {object} 失败

Error message

更新索引 {index} 数据 {object} 失败

What it means

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.

Source

Thrown at demo-elasticsearch-rest-high-level-client/src/main/java/com/xkcoding/elasticsearch/service/base/BaseElasticsearchService.java:124

     */
    protected static IndexRequest buildIndexRequest(String index, String id, Object object) {
        return new IndexRequest(index).id(id).source(BeanUtil.beanToMap(object), XContentType.JSON);
    }

    /**
     * exec updateRequest
     *
     * @param index  elasticsearch index name
     * @param id     Document id
     * @param object request object
     * @author fxbin
     */
    protected void updateRequest(String index, String id, Object object) {
        try {
            UpdateRequest updateRequest = new UpdateRequest(index, id).doc(BeanUtil.beanToMap(object), XContentType.JSON);
            client.update(updateRequest, COMMON_OPTIONS);
        } catch (IOException e) {
            throw new ElasticsearchException("更新索引 {" + index + "} 数据 {" + object + "} 失败");
        }
    }

    /**
     * exec deleteRequest
     *
     * @param index elasticsearch index name
     * @param id    Document id
     * @author fxbin
     */
    protected void deleteRequest(String index, String id) {
        try {
            DeleteRequest deleteRequest = new DeleteRequest(index, id);
            client.delete(deleteRequest, COMMON_OPTIONS);
        } catch (IOException e) {
            throw new ElasticsearchException("删除索引 {" + index + "} 数据id {" + id + "} 失败");
        }
    }

View on GitHub (pinned to 87a142f960)

Solutions

  1. Verify the index and document id exist before calling updateRequest (use client.get() to check).
  2. Ensure the passed object has at least one non-null field so BeanUtil.beanToMap yields a non-empty doc.
  3. Chain the cause: new ElasticsearchException("...", e).
  4. Confirm cluster connectivity and that the 30MB heap buffer (COMMON_OPTIONS) is sufficient for the payload.

Example fix

// before
} catch (IOException e) {
    throw new ElasticsearchException("更新索引 {" + index + "} 数据 {" + object + "} 失败");
}

// after
} catch (IOException e) {
    throw new ElasticsearchException("更新索引 {" + index + "} 数据 {" + object + "} 失败", e);
}
Defensive patterns

Strategy: validation

Validate before calling

// Validate object has non-null fields before update
Map<String, Object> docMap = BeanUtil.beanToMap(object);
if (docMap.isEmpty() || docMap.values().stream().allMatch(Objects::isNull)) {
    throw new IllegalArgumentException("Cannot update with an empty or null-only document");
}
// Verify index and document exist
GetRequest getRequest = new GetRequest(index, id);
if (!client.exists(getRequest, COMMON_OPTIONS)) {
    throw new IllegalStateException("Document " + id + " not found in index " + index);
}

Try / catch

try {
    updateRequest(index, id, object);
} catch (ElasticsearchException e) {
    log.error("Update failed for index={}, id={}: {}", index, id, e.getMessage());
    throw e;
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of xkcoding/spring-boot-demo@87a142f960 (2026-08-14). Data as JSON: /api/errors/8bf56c70a7e85df4. Report an issue: GitHub.