elasticsearch-java icon indicating copy to clipboard operation
elasticsearch-java copied to clipboard

Allow serializing requests independently from the transport

Open patpatpat123 opened this issue 2 years ago • 2 comments

Description

Hello, Elastic java team,

I wanted to reach out to ask for a possible enhancement request, please.

This project proposes a very nice abstraction through its wrapper.

For instance, the BulkRequest:

List<Product> products = fetchProducts();

BulkRequest.Builder br = new BulkRequest.Builder();

for (Product product : products) {
    br.operations(op -> op           
        .index(idx -> idx            
            .index("products")       
            .id(product.getSku())
            .document(product)
        )
    );
}

Would it be possible to enhance this in order to get the http request payload directly, please?

Something like bulkRequest.getRequestBody() which would allow getting the request and its body, without executing, instead of BulkResponse result = esClient.bulk(br.build()); which would execute the request.

This would allow the possibility to enrich the content of the request, and most of all, to be able to just get the request body and send it with other http client, such as Okhttp, Netty, etc... without being coupled with the default RestClient of this repo.

patpatpat123 avatar Mar 29 '23 18:03 patpatpat123

We generally want to avoid code that is specific for a particular request, as the need may actually apply to all of them.

Currently the serialization is handled within the Transport implementation. Bulk requests belong to a family of requests that are sent as nd-json (one json document per line). This is handled by RestClientTransport.writeNdJson() that you can copy in your code to use a different client.

A possible evolution is to extract this code in a separate class so that it can called not only by the transport class, but also any other application code. I've renamed the issue to reflect this.

swallez avatar Apr 03 '23 16:04 swallez

"A possible evolution is to extract this code in a separate class so that it can called not only by the transport class, but also any other application code."

-> this is what I intended to say. Thank you for the correction. Looking forward to seeing this in the future, many thanks

patpatpat123 avatar Apr 04 '23 02:04 patpatpat123