Missing RequiredPropertyException: 缺少必需属性 'BulkRequest.operations' Elasticsearch



我正面临着MissingRequiredPropertyException:缺少所需的属性'BulkRequest。操作的例外。我知道它是什么。但是我不知道如何解决它。

if (Some Condition) {
// Keep adding the optimised objects to bulk requests so can write it back later in one call.
bulkRequestBuilder.operations(op -> op.index(idx -> idx
.id(esKey)
.document(JSON OBJECT)));
} else {
log.info("I am not building any bulk request.", esKey);
}

之后,我想把这个批量请求写入Elastic search。在编写时,我需要检查批量请求内部是否有任何操作。所以我在做下面的事情。

BulkRequest bulkRequest = bulkRequestBuilder.build();
if (!bulkRequest.operations().isEmpty()) {
BulkResponse bulkResponse = repo.saveToIndexByBulkRequest(bulkRequest);
}

在上面的代码中,.build()抛出了MissingRequiredPropertyException。当批量请求中没有任何操作时,它将抛出该异常。

在构建之前,我如何检查它内部是否有任何操作?

如果你需要更多的信息,请告诉我。

对于这个特殊的场景,您可以使用异常处理并像这样解决:

try {
BulkRequest bulkRequest = bulkRequestBuilder.build();
if (!bulkRequest.operations().isEmpty()) {
BulkResponse bulkResponse = repo.save(bulkRequest);
if (bulkResponse.errors()) {
throw new CustomException(CustomMessage);
}
}
} catch (MissingRequiredPropertyException exception) {
// either throw exception or continue based on requirement.
log.error("Bulk RequestBuilder has no operations in it.");
}

最新更新