Deserialize an elastic search hit to Hit<ObjectNode>

997 Views Asked by At

I'm trying to create a Hit<ObjectNode> from a json string returned by Elastic (for unit-testing purposes). When I try to deserialize this using .withJson I get a ClassCastException with the following message: "class co.elastic.clients.json.JsonDataImpl cannot be cast to class com.fasterxml.jackson.databind.node.ObjectNode (co.elastic.clients.json.JsonDataImpl and com.fasterxml.jackson.databind.node.ObjectNode are in unnamed module of loader 'app')"

Full code:

String hitJson = "{\"_index\":\"my-index\",\"_id\":\"ID:MYID\",\"_score\":1.0,\"_source\":{\"productCode\":\"MYID\",\"productName\":\"my product\",\"EffDate\":\"01/01/1900\",\"ExpDate\":\"12/31/9999\",\"Status\":\"Active\",\"productId\":1234567,\"_type\":\"my type\",\"type\":\"my type\",\"updateTime\":\"2021-07-08T11:40:18Z\"}}";
InputStream hitInput = new ByteArrayInputStream(hitJson.getBytes());

Hit<ObjectNode> aHit = Hit.of(builder -> builder.withJson(hitInput));
try {
    ObjectNode source = aHit.source();
} catch (Exception ex) {
    String msg = ex.getMessage();
}

I can see that JsonDataImpl does have all the properties, but I'm not sure why it can't deserialize it to ObjectNode. I'm not sure where to go from here - all my other attemps to use .withJson have succeeded without issue.

Formatted json response for reference:

{
    "_index": "my-index",
    "_id": "ID:MYID",
    "_score": 1.0,
    "_source": {
        "productCode": "MYID",
        "productName": "my product",
        "EffDate": "01/01/1900",
        "ExpDate": "12/31/9999",
        "Status": "Active",
        "productId": 1234567,
        "_type": "my type",
        "type": "my type",
        "updateTime": "2021-07-08T11:40:18Z"
    }
}
0

There are 0 best solutions below