I have Elasticsearch version 8.5.3 and many documents in my index.
A document looks like this:
{
"myArray": [
{
"myGrocery": {
"myId": "aString",
"apple": "aString",
"banana": aNumber,
"vegetable": {
"garlic": "aString",
"onion": "aString",
"mushroom": "aString",
}
}
}
]
}
I have the new Elasticsearch Java Client in my repo (not the old High Level Rest Client). I map the response directly to my DTO like:
import co.elastic.clients.elasticsearch.ElasticsearchClient;
var searchResponse = elasticSearchClient.search(searchRequest, MyDto.class);
Now the problem is that, I have "banana" field which must be between [0-9]. This is defined in my openapi specification as ENUM. "banana" field is a byte type in Elasticsearch.
If there is a "banana" field bigger than 9 in my documents I get this error.
co.elastic.clients.transport.TransportException: node: https://xxx.domain.com:9200/, status: 200, [es/search] Failed to decode response
at co.elastic.clients.transport.ElasticsearchTransportBase.decodeTransportResponse(ElasticsearchTransportBase.java:404)
at co.elastic.clients.transport.ElasticsearchTransportBase.getApiResponse(ElasticsearchTransportBase.java:363)
...
...
Caused by: com.fasterxml.jackson.databind.exc.InvalidFormatException: Cannot deserialize value of type `myclasspath.dto.MyDto` from String "10": not one of the values accepted for Enum class: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
at [Source: REDACTED (`StreamReadFeature.INCLUDE_SOURCE_IN_LOCATION` disabled); line: 1, column: 244] (through reference chain: myclasspath.dto.MyDto["banana"])
But I want to throw an error to the clients and inform them which "banana" is out of range. Therefore I need to deserialize the elasticsearch response. How can I do it?