# Elasticsearch-java - BulkOperation and IndexRequest have different behaviour with same custom JsonpSerializer

**URL:** <https://discuss.elastic.co/t/elasticsearch-java-bulkoperation-and-indexrequest-have-different-behaviour-with-same-custom-jsonpserializer/298496>\
**Category:** Elasticsearch\
**Tags:** language-clients\
**Created:** [March 1, 2022, 10:46am UTC](https://discuss.elastic.co/t/elasticsearch-java-bulkoperation-and-indexrequest-have-different-behaviour-with-same-custom-jsonpserializer/298496 "2022-03-01T10:46:49Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![fmarazita](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/fmarazita/32/102452_2.png) [@fmarazita](https://discuss.elastic.co/u/fmarazita)\
**Post date:** [March 1, 2022, 10:46am UTC](https://discuss.elastic.co/t/elasticsearch-java-bulkoperation-and-indexrequest-have-different-behaviour-with-same-custom-jsonpserializer/298496/1 "2022-03-01T10:46:49Z")

</div>

Hi,

It seems that BulkOperation and IndexRequest with same tDocumentSerialization have different behaviour.  
Elastic version: 7.16.3

I created a test with the same upsert operation, with and without using bulk operation. The business logic is very simple:

```auto
public IndexRequest<B> indexRequest(String index, B bean) {
        CustomSerializer<D, B, K> serialize = new CustomSerializer<>(...); //-> where CustomSerializer implements JsonpSerializer<B>
       return new IndexRequest.Builder<B>()
                    .index(index)
                    .id(createElasticId(bean))
                    .tDocumentSerializer(serialize)
                    .document(bean)
                    .build();
    }

public BulkOperation bulkIndexRequest(String index, B bean){
     CustomSerializer<D, B, K> serialize = new CustomSerializer<>(...); //-> where CustomSerializer implements JsonpSerializer<B>
    return new BulkOperation.Builder()
            .index(new IndexOperation.Builder<B>()
                    .index(index)
                    .id(createElasticId(bean))
                    .tDocumentSerializer(serialize)
                    .document(bean)
                    .build())
            .build();
}

public BulkResponse sendBulkRequest(List<BulkOperation> indexRequestList) {
   return restClient.bulk(new BulkRequest.Builder().operations(indexRequestList).build()) //--> restClient is a ElasticsearchClient instance
}

public IndexResponse sendIndexRequest(IndexRequest<B> indexRequest) {
    return restClient.index(indexRequest); //--> restClient is a ElasticsearchClient instance
}

```

In the first case the serialization works, the client sent the serialized document to elastic and I am happy.  
The second one failed, No serializer found for class:

`Caused by: jakarta.json.JsonException: Jackson exception at co.elastic.clients.json.jackson.JacksonUtils.convertException(JacksonUtils.java:39) at co.elastic.clients.json.jackson.JacksonJsonpMapper.serialize(JacksonJsonpMapper.java:88) at co.elastic.clients.transport.rest_client.RestClientTransport.writeNdJson(RestClientTransport.java:237) at co.elastic.clients.transport.rest_client.RestClientTransport.writeNdJson(RestClientTransport.java:234) at co.elastic.clients.transport.rest_client.RestClientTransport.prepareLowLevelRequest(RestClientTransport.java:211) at co.elastic.clients.transport.rest_client.RestClientTransport.performRequest(RestClientTransport.java:145) at co.elastic.clients.elasticsearch.ElasticsearchClient.bulk(ElasticsearchClient.java:310) at it.list.lookout.lka.elastic.ElasticDAO.sendBulkRequest(ElasticDAO.java:240) ... 55 more Caused by: com.fasterxml.jackson.databind.exc.InvalidDefinitionException: **No serializer found for class**... and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference chain: `

Maybe I am wrong but there is something strange on on method BulkRequest \_serializables.

Thanks in advance  
Fabrizio

---

<div class="post-metadata">

**Author:** ![fmarazita](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/fmarazita/32/102452_2.png) [@fmarazita](https://discuss.elastic.co/u/fmarazita)\
**Post date:** [March 14, 2022, 8:57am UTC](https://discuss.elastic.co/t/elasticsearch-java-bulkoperation-and-indexrequest-have-different-behaviour-with-same-custom-jsonpserializer/298496/2 "2022-03-14T08:57:03Z")

</div>

Any news?

---

<div class="post-metadata">

**Author:** ![swallez](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/swallez/32/11972_2.png) [@swallez](https://discuss.elastic.co/u/swallez)\
**Post date:** [March 17, 2022, 11:10am UTC](https://discuss.elastic.co/t/elasticsearch-java-bulkoperation-and-indexrequest-have-different-behaviour-with-same-custom-jsonpserializer/298496/3 "2022-03-17T11:10:20Z")

</div>

There is indeed an issue with `IndexOperation` which is serialized in a specific way (bulk requests are sent as nd-json with operation metadata and document sent as separate json documents) and it effectively does not use `tDocumentSerializer`. We're going to fix this.

However, the general recommendation would be to avoid using these `tDocumentSerializer` properties and let Jackson's object mapper handle serialization of your application objects. Providing your own serializer involves some rather low details the Java client's underlying framework that in practice can be easily handled by a regular JSON object mapper as Jackson. We're actually considering deprecating these serializer properties and remove them at some point.

That being said, the exception shows that Jackson doesn't know how to serialize your bean because it cannot fin any properties. You should investigate in this direction so that you don't need to set `tDocumentSerializer`.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [April 14, 2022, 11:10am UTC](https://discuss.elastic.co/t/elasticsearch-java-bulkoperation-and-indexrequest-have-different-behaviour-with-same-custom-jsonpserializer/298496/4 "2022-04-14T11:10:23Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
