简体   繁体   中英

Catch a specific Elasticsearch exception from a BulkRequest

I use Java to index some documents with a BulkRequest into Elasticsearch 1.4.2.

Some of these docs only need to be written when they are not already in the index, so I set the CREATE-opType like this:

indexRequestBuilder.opType(IndexRequest.OpType.CREATE)

Now the docs which were already in the index fail in the BulkResponse.

Error message bulkItemResponse.getFailureMessage() :

DocumentAlreadyExistsException[...]

I want to ignore this class of exception but retry writing the docs for all other type of exceptions.

So how can I catch just the DocumentAlreadyExistsException ?

I can get the Failure with bulkItemResponse.getFailure() , but I cannot find any information about the type of the Exception beside the error message.

I could look in the error-message for the exception name, but this may be rather fragile with new Elasticsearch versions:

if(bulkItemResponse.getFailureMessage().startsWith("DocumentAlreadyExistsException[")

Is there a better way?

This cant be possible. The bulk request is actaully executed on the server side and not client side. And hence all it can do is to sent the stacktrace back and not the Exception object.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM