# Java 8.1 bulk request

**URL:** <https://discuss.elastic.co/t/java-8-1-bulk-request/302423>\
**Category:** Elasticsearch\
**Created:** [April 14, 2022, 11:57am UTC](https://discuss.elastic.co/t/java-8-1-bulk-request/302423 "2022-04-14T11:57:08Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![3xil3](https://avatars.discourse-cdn.com/v4/letter/3/f9ae1b/32.png) [@3xil3](https://discuss.elastic.co/u/3xil3)\
**Post date:** [April 14, 2022, 11:57am UTC](https://discuss.elastic.co/t/java-8-1-bulk-request/302423/1 "2022-04-14T11:57:08Z")

</div>

Hi,

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.

```auto
        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:

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

```

I assume that there is something wrong with my json, but the [documentation](https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-bulk.html) unfortunately doesn't list a java example.

Can someone please help in providing a working example?

---

<div class="post-metadata">

**Author:** ![RabBit\_BR](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rabbit_br/32/82261_2.png) [@RabBit\_BR](https://discuss.elastic.co/u/RabBit_BR)\
**Post date:** [April 14, 2022, 2:01pm UTC](https://discuss.elastic.co/t/java-8-1-bulk-request/302423/2 "2022-04-14T14:01:39Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![RabBit\_BR](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rabbit_br/32/82261_2.png) [@RabBit\_BR](https://discuss.elastic.co/u/RabBit_BR)\
**Post date:** [April 14, 2022, 2:26pm UTC](https://discuss.elastic.co/t/java-8-1-bulk-request/302423/3 "2022-04-14T14:26:32Z")

</div>

try this:

```auto
    var operations = BulkOperation.of(bo -> bo.create(CreateOperation.of(co->
        co.document(Product.builder().title("none").build()))));
    BulkRequest bulkRequest = BulkRequest.of(br ->br.operations(operations).index("index_alias"));
    final BulkResponse response = client.bulk(bulkRequest);

```

---

<div class="post-metadata">

**Author:** ![3xil3](https://avatars.discourse-cdn.com/v4/letter/3/f9ae1b/32.png) [@3xil3](https://discuss.elastic.co/u/3xil3)\
**Post date:** [April 19, 2022, 7:51am UTC](https://discuss.elastic.co/t/java-8-1-bulk-request/302423/4 "2022-04-19T07:51:39Z")

</div>

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.

---

<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:** [April 25, 2022, 1:04pm UTC](https://discuss.elastic.co/t/java-8-1-bulk-request/302423/5 "2022-04-25T13:04:36Z")

</div>

We've recently added [documentation for sending bulk requests](https://www.elastic.co/guide/en/elasticsearch/client/java-api-client/current/indexing-bulk.html) 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](https://github.com/elastic/elasticsearch-java/issues/251) for this.

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

---

<div class="post-metadata">

**Author:** ![marcgise](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/marcgise/32/83944_2.png) [@marcgise](https://discuss.elastic.co/u/marcgise)\
**Post date:** [April 26, 2022, 9:17pm UTC](https://discuss.elastic.co/t/java-8-1-bulk-request/302423/6 "2022-04-26T21:17:02Z")

</div>

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 = br.build();  
> 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...  
Thanks

---

<div class="post-metadata">

**Author:** ![marcgise](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/marcgise/32/83944_2.png) [@marcgise](https://discuss.elastic.co/u/marcgise)\
**Post date:** [April 26, 2022, 10:40pm UTC](https://discuss.elastic.co/t/java-8-1-bulk-request/302423/7 "2022-04-26T22:40:34Z")

</div>

Well, I finally get it with following code 😉

```auto
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 = br.build();
BulkResponse response = getClient().bulk(request);

```

---

<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:** [May 24, 2022, 10:41pm UTC](https://discuss.elastic.co/t/java-8-1-bulk-request/302423/8 "2022-05-24T22:41:28Z")

</div>

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