Java 8.1 bulk request


I'm having trouble doing a bulk index request using the java 8.1.1 client. I get an unclear error message and I'm basically stuck.

        String test= "{ \"test\",\"test\"}";
        final InputStream jsonInputStream = new ByteArrayInputStream(test.getBytes());
        final ElasticsearchClient client = instanceManager.getClient();
        final BulkResponse response = client.bulk(br -> br.operations(
                bo -> bo.index(io -> io.withJson(jsonInputStream).index("test").id("1"))

I get the following cryptic error upon execution:

java.lang.IllegalArgumentException: Class class co.elastic.clients.elasticsearch.core.bulk.IndexOperation cannot be read from JSON
	at co.elastic.clients.util.WithJsonObjectBuilderBase.withJson(
	at co.elastic.clients.json.WithJson.withJson(

I assume that there is something wrong with my json, but the documentation unfortunately doesn't list a java example.

Can someone please help in providing a working example?

I saw that the error occurs because the mapper doesn't deserialize the class because it doesn't have the @JsonpDeserializable annotation.
Maybe you have to mount an object instead of reading a json.

try this:

    var operations = BulkOperation.of(bo -> bo.create(CreateOperation.of(co->
    BulkRequest bulkRequest = BulkRequest.of(br ->br.operations(operations).index("index_alias"));
    final BulkResponse response = client.bulk(bulkRequest);

There is no 'Product' builder in the 8.1.1 API it seems, what import are you using?

In my particular case; I'm getting json files delivered which represent a document, but it would mean I'd have to parse them and rebuild them using the builder.

We've recently added documentation for sending bulk requests with the Java API client.

There indeed an issue in bulk request operations that causes withJson to fail (the nd-json encoding is a special case that was missing a test). I opened Bulk request operations fail on withJson · Issue #251 · elastic/elasticsearch-java · GitHub for this.

A workaround is to use Jackson's com.fasterxml.jackson.databind.util.RawValue for the bulk operation's document.

Dear, I'm facing the same error while trying to index a JSON with withJson method. I tried to use RawValue but still having an error when sending the request to the server:

BulkRequest.Builder br = new BulkRequest.Builder();
String json = "{my_json_value}";
br.operations(op -> op.index(idx -> idx.index(index).id(id).document(new RawValue(json))));
BulkRequest request =;
BulkResponse response = getClient().bulk(request);

The error is:

co.elastic.clients.Elasticsearch._types.ElasticsearchException: [es/bulk] failed: [illegal_argument_exception] Malformed action/metadata line [3], expected START_OBJECT but found [VALUE_STRING]

So I think that RawValue doesn't work...

Well, I finally get it with following code :wink:

String json = "my_json_value";
Reader reader = new StringReader(json);

JsonpMapper jsonpMapper = getClient()._transport().jsonpMapper();
JsonProvider jsonProvider = jsonpMapper.jsonProvider();
JsonData jd = JsonData.from(jsonProvider.createParser(reader), jsonpMapper);

BulkRequest.Builder br = new BulkRequest.Builder();
br.operations(op -> op.index(idx -> idx.index(index).id(id).document(jd)));

BulkRequest request =;
BulkResponse response = getClient().bulk(request);
1 Like

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