elastic / elasticsearch-java

Official Elasticsearch Java Client
Apache License 2.0
11 stars 243 forks source link

BulkOperation and IndexRequest have different behaviour with same custom JsonpSerializer serializer #172

Open fmarazita opened 2 years ago

fmarazita commented 2 years ago

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:

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 there is something strange on on method BulkRequest _serializables.

Thanks in advance Fabrizio

fmarazita commented 2 years ago

Hi any news?