# How to deserialize SearchResponse properly in Elasticsearch Java Client

**URL:** <https://discuss.elastic.co/t/how-to-deserialize-searchresponse-properly-in-elasticsearch-java-client/366904>\
**Category:** Elasticsearch\
**Tags:** language-clients\
**Created:** [September 21, 2024, 7:44am UTC](https://discuss.elastic.co/t/how-to-deserialize-searchresponse-properly-in-elasticsearch-java-client/366904 "2024-09-21T07:44:44Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![PodarcisMuralis](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/podarcismuralis/32/122606_2.png) [@PodarcisMuralis](https://discuss.elastic.co/u/PodarcisMuralis)\
**Post date:** [September 21, 2024, 7:44am UTC](https://discuss.elastic.co/t/how-to-deserialize-searchresponse-properly-in-elasticsearch-java-client/366904/1 "2024-09-21T07:44:44Z")

</div>

I have Elasticsearch version 8.5.3 and many documents in my index.  
A document looks like this:

```auto
{
  "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:

```auto
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.

```auto
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?

---

<div class="post-metadata">

**Author:** ![RabBit\_BR](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rabbit_br/32/82261_2.png) [@RabBit\_BR](https://discuss.elastic.co/u/RabBit_BR)\
**Post date:** [September 22, 2024, 4:15pm UTC](https://discuss.elastic.co/t/how-to-deserialize-searchresponse-properly-in-elasticsearch-java-client/366904/2 "2024-09-22T16:15:14Z")

</div>

Hi @PodarcisMuralis

Did you try add config DeserializationFeature.READ\_UNKNOWN\_ENUM\_VALUES\_AS\_NULL?
