alibaba/spring-ai-alibaba · error · RuntimeException

批量索引失败

Error message

批量索引失败

What it means

ElasticsearchClientWrapper.bulkIndex() performs a BulkRequest and wraps any IOException in a RuntimeException "批量索引失败" ("Bulk indexing failed"). This signals a transport-level failure to send the bulk request or read the response; partial per-item failures inside a successful response are only logged, not thrown.

Source

Thrown at spring-ai-alibaba-admin/spring-ai-alibaba-admin-server-start/src/main/java/com/alibaba/cloud/ai/studio/admin/repository/impl/ElasticsearchClientWrapper.java:61

            for (Map<String, Object> doc : documents) {
                bulkBuilder.operations(op -> op
                    .index(idx -> idx
                        .index(index)
                        .document(doc)
                    )
                );
            }
            
            BulkResponse result = elasticsearchClient.bulk(bulkBuilder.build());
            
            if (result.errors()) {
                log.error("批量索引部分失败: {}", result.items());
            } else {
                log.info("批量索引成功: {} 条文档", documents.size());
            }
            
        } catch (IOException e) {
            throw new RuntimeException("批量索引失败", e);
        }
    }

    /**
     * 检查索引是否存在
     */
    public boolean indexExists(String indexName) {
        try {
            ExistsRequest existsRequest = ExistsRequest.of(e -> e.index(indexName));
            return elasticsearchClient.indices().exists(existsRequest).value();
        } catch (IOException e) {
            log.error("检查索引是否存在失败: {}", indexName, e);
            return false;
        }
    }

    /**
     * 转换SearchResponse到Map列表

View on GitHub (pinned to f82da0b50f)

Solutions

  1. Check the wrapped IOException cause for the real network/timeout error.
  2. Verify cluster health and that the node is reachable during the bulk operation.
  3. Reduce bulk batch size or split large payloads to stay under http.max_content_length.
  4. Increase request timeouts and add retry-with-backoff around bulkIndex.
  5. Note the code only throws on IOException — also inspect result.items() logs for per-document failures.

Example fix

// before
catch (IOException e) { throw new RuntimeException("批量索引失败", e); }
// after
catch (IOException e) { throw new RuntimeException("批量索引失败: " + e.getMessage(), e); } // plus retry wrapper for transient failures
Defensive patterns

Strategy: retry

Validate before calling

// keep batches under limits
if (documents.size() > 1000) { throw new IllegalArgumentException("batch too large, split it"); }

Try / catch

try { wrapper.bulkIndex(index, docs); }
catch (RuntimeException e) {
    if (e.getCause() instanceof IOException && attempt < MAX) { backoff(); retry(docs); }
    else throw e;
}

Prevention

When it happens

Trigger: Calling bulkIndex() with documents when the ES connection drops mid-request, the request times out on large payloads, or the node is unreachable while executing client.bulk().

Common situations: Bulk-loading a large dataset during a network hiccup; ES node restart mid-write; request payload exceeding http.max_content_length; timeout from slow indexing under load.

Understand the failure class

Background: 'Something went wrong' / 'Request failed (500)' / 'HTTP error! status: 404' — what failed HTTP requests actually mean and how to find the real cause — this error's family across 28 libraries.

Related errors


AI-assisted analysis of alibaba/spring-ai-alibaba@f82da0b50f (2026-09-09). Data as JSON: /api/errors/686d082bf04e49f4. Report an issue: GitHub.