# Bulk request how to?

**URL:** <https://discuss.elastic.co/t/bulk-request-how-to/305096>\
**Category:** Elasticsearch\
**Created:** [May 18, 2022, 4:13pm UTC](https://discuss.elastic.co/t/bulk-request-how-to/305096 "2022-05-18T16:13:21Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![frankmontyne](https://avatars.discourse-cdn.com/v4/letter/f/c6cbf5/32.png) [@frankmontyne](https://discuss.elastic.co/u/frankmontyne)\
**Post date:** [May 18, 2022, 4:13pm UTC](https://discuss.elastic.co/t/bulk-request-how-to/305096/1 "2022-05-18T16:13:21Z")

</div>

Hi I'm a bit confused on how to perform a bulk request with the new Java API client.

To index one document I use the following code that works without problems. Since we are using GSON I pass the json string representation of the document in a StringReader (country.toESJsonReader()):

```auto
	IndexResponse indexResponse = elasticSearch.getESClient().index(builder -> 
		builder
			.index(aliasName)
			.id(country.id())
			.withJson(country.toESJsonReader())
			.waitForActiveShards(asBuilder -> asBuilder.count(1))
			.refresh(Refresh.True));

```

When indexing multiple documents I use the following code that throws the following exception:  
"Class class co.elastic.clients.Elasticsearch.core.bulk.CreateOperation cannot be read from JSON"

```auto
	// Create bulk request.
	BulkRequest.Builder bulkRequestBuilder = new BulkRequest.Builder()
		.waitForActiveShards(asBuilder -> asBuilder.count(1))
		.refresh(Refresh.True);

	// Add countries to bulk request.
	List<BulkOperation> bulkOperations = new ArrayList<>();
	for (Country country : countries) {
		bulkOperations.add(new CreateOperation.Builder<String>()
			.index(aliasName)
			.id(country.id())
			.withJson(country.toESJsonReader())
			.build()
			._toBulkOperation());					
	}
	bulkRequestBuilder.operations(bulkOperations);
				
	// Perform bulk request.
	BulkResponse bulkResponse = elasticSearch.getESClient().bulk(bulkRequestBuilder.build());

```

Any idea what I am doing wrong here?

Thanks

---

<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:** [May 18, 2022, 4:35pm UTC](https://discuss.elastic.co/t/bulk-request-how-to/305096/2 "2022-05-18T16:35:45Z")

</div>

Hi.  
Read this [post](https://discuss.elastic.co/t/java-8-1-bulk-request/302423), it seems to be similar to your problem and managed to solve it by another approach.

---

<div class="post-metadata">

**Author:** ![frankmontyne](https://avatars.discourse-cdn.com/v4/letter/f/c6cbf5/32.png) [@frankmontyne](https://discuss.elastic.co/u/frankmontyne)\
**Post date:** [May 18, 2022, 6:20pm UTC](https://discuss.elastic.co/t/bulk-request-how-to/305096/3 "2022-05-18T18:20:50Z")

</div>

Hi Andre,

Thank you very much! That did the trick. It's apparently a bug in the API. Hopefully it will be fixed soon.

---

<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:** [June 15, 2022, 6:20pm UTC](https://discuss.elastic.co/t/bulk-request-how-to/305096/4 "2022-06-15T18:20:54Z")

</div>

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