xkcoding/spring-boot-demo · error · ElasticsearchException

删除索引 {index} 数据id {id} 失败

Error message

删除索引 {index} 数据id {id} 失败

What it means

Thrown by deleteRequest when client.delete() raises an IOException during deletion of a single document by id. Only IOException is caught; if the document or index does not exist, the RestHighLevelClient throws a RuntimeException (ElasticsearchStatusException) which bypasses this handler. The original cause is discarded.

Source

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

            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 + "} 失败");
        }
    }

    /**
     * search all
     *
     * @param index elasticsearch index name
     * @return {@link SearchResponse}
     * @author fxbin
     */
    protected SearchResponse search(String index) {
        SearchRequest searchRequest = new SearchRequest(index);
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        searchSourceBuilder.query(QueryBuilders.matchAllQuery());
        searchRequest.source(searchSourceBuilder);
        SearchResponse searchResponse = null;
        try {
            searchResponse = client.search(searchRequest, COMMON_OPTIONS);

View on GitHub (pinned to 87a142f960)

Solutions

  1. Verify cluster connectivity and index existence before calling deleteRequest.
  2. Chain the cause: new ElasticsearchException("...", e) for diagnostic visibility.
  3. Add a catch for ElasticsearchStatusException to handle 'not found' gracefully if idempotent deletes are desired.
  4. Ensure the document id is non-null and correctly formatted.

Example fix

// before
} catch (IOException e) {
    throw new ElasticsearchException("删除索引 {" + index + "} 数据id {" + id + "} 失败");
}

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

Strategy: validation

Validate before calling

// Verify document exists before delete (optional, for idempotency)
GetRequest getRequest = new GetRequest(index, id);
try {
    if (!client.exists(getRequest, COMMON_OPTIONS)) {
        log.warn("Document {} not found in index {}, skipping delete", id, index);
        return;
    }
} catch (IOException e) {
    log.error("Cannot verify document existence", e);
}

Try / catch

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

Prevention

When it happens

Trigger: Calling deleteRequest(index, id) when: the cluster is unreachable (IOException); the document id does not exist (RuntimeException, not caught here); the index does not exist (RuntimeException).

Common situations: Deleting a document that was already removed; cluster offline during a cleanup operation; network timeout; attempting deletion against a read-only index.

Related errors


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